void-packages/srcpkgs/ccextractor/patches/0001-fix-ffmpeg-5-and-tesse...

169 lines
5.8 KiB
Diff

From b1cbfcea9b9c687143bf0d80bc179b563e99d025 Mon Sep 17 00:00:00 2001
From: Prateek Sunal <prtksunal@gmail.com>
Date: Thu, 9 Mar 2023 01:44:53 +0530
Subject: [PATCH] fix: ffmpeg 5 and tesseract 5 compatibility (#1479)
* fix: replace deprecated `codec` property with `codecpar`
* fix: replace deprecated method `avcodec_decode_video2` with `avcodec_receive_frame` and `avcodec_send_packet`
* Update CHANGES.TXT
* fix: remove deprecated `av_register_all` function
* fix: formatting
* fix: add support for tesseract 5
* fix: tesseract v5
* fix: hardsubx codec context error
* fix: lint const warning
---
docs/CHANGES.TXT | 1 +
src/lib_ccx/ffmpeg_intgr.c | 7 +++---
src/lib_ccx/hardsubx.c | 25 ++++++++++++------
src/lib_ccx/hardsubx_decoder.c | 27 ++++++++------------
src/lib_ccx/ocr.c | 46 ++++++++++++----------------------
5 files changed, 47 insertions(+), 59 deletions(-)
diff --git a/src/lib_ccx/ffmpeg_intgr.c b/src/lib_ccx/ffmpeg_intgr.c
index be988890..e6f21e20 100644
--- a/src/lib_ccx/ffmpeg_intgr.c
+++ b/src/lib_ccx/ffmpeg_intgr.c
@@ -66,7 +66,6 @@ void *init_ffmpeg(const char *path)
struct ffmpeg_ctx *ctx;
AVCodec *dec = NULL;
avcodec_register_all();
- av_register_all();
if (ccx_options.debug_mask & CCX_DMT_VERBOSE)
av_log_set_level(AV_LOG_INFO);
@@ -133,7 +132,6 @@ int ff_get_ccframe(void *arg, unsigned char *data, int maxlen)
struct ffmpeg_ctx *ctx = arg;
int len = 0;
int ret = 0;
- int got_frame;
AVPacket packet;
ret = av_read_frame(ctx->ifmt, &packet);
@@ -151,12 +149,13 @@ int ff_get_ccframe(void *arg, unsigned char *data, int maxlen)
return AVERROR(EAGAIN);
}
- ret = avcodec_decode_video2(ctx->dec_ctx, ctx->frame, &got_frame, &packet);
+ avcodec_send_packet(ctx->codec_ctx, &ctx->packet);
+ ret = avcodec_receive_frame(ctx->dec_ctx, ctx->frame);
if (ret < 0)
{
av_log(NULL, AV_LOG_ERROR, "unable to decode packet\n");
}
- else if (!got_frame)
+ else if (ret != 0)
{
return AVERROR(EAGAIN);
}
diff --git a/src/lib_ccx/hardsubx.c b/src/lib_ccx/hardsubx.c
index 411377f9..fb844c91 100644
--- a/src/lib_ccx/hardsubx.c
+++ b/src/lib_ccx/hardsubx.c
@@ -12,9 +12,6 @@
int hardsubx_process_data(struct lib_hardsubx_ctx *ctx)
{
- // Get the required media attributes and initialize structures
- av_register_all();
-
if (avformat_open_input(&ctx->format_ctx, ctx->inputfile[0], NULL, NULL) != 0)
{
fatal(EXIT_READ_ERROR, "Error reading input file!\n");
@@ -32,7 +29,7 @@
ctx->video_stream_id = -1;
for (int i = 0; i < ctx->format_ctx->nb_streams; i++)
{
- if (ctx->format_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
+ if (ctx->format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
{
ctx->video_stream_id = i;
break;
@@ -43,8 +40,21 @@
fatal(EXIT_READ_ERROR, "Video Stream not found!\n");
}
- ctx->codec_ctx = ctx->format_ctx->streams[ctx->video_stream_id]->codec;
- ctx->codec = avcodec_find_decoder(ctx->codec_ctx->codec_id);
+ ctx->codec_ctx = avcodec_alloc_context3(NULL);
+ if (!ctx->codec_ctx)
+ {
+ fatal(EXIT_NOT_ENOUGH_MEMORY, "Could not allocate codec context!\n");
+ }
+
+ // Assign codec parameters to codec context
+ if (avcodec_parameters_to_context(ctx->codec_ctx, ctx->format_ctx->streams[ctx->video_stream_id]->codecpar) < 0)
+ {
+ fatal(EXIT_READ_ERROR, "Could not initialize codec context!\n");
+ }
+
+ // Find decoder for the codec context
+ ctx->codec = (AVCodec *)avcodec_find_decoder(ctx->codec_ctx->codec_id);
+
if (ctx->codec == NULL)
{
fatal(EXIT_READ_ERROR, "Input codec is not supported!\n");
diff --git a/src/lib_ccx/hardsubx_decoder.c b/src/lib_ccx/hardsubx_decoder.c
index bd9b0980..b5dfaec4 100644
--- a/src/lib_ccx/hardsubx_decoder.c
+++ b/src/lib_ccx/hardsubx_decoder.c
@@ -371,7 +371,6 @@
int hardsubx_process_frames_tickertext(struct lib_hardsubx_ctx *ctx, struct encoder_ctx *enc_ctx)
{
// Search for ticker text at the bottom of the screen, such as in Russia TV1 or stock prices
- int got_frame;
int cur_sec = 0, total_sec, progress;
int frame_number = 0;
char *ticker_text = NULL;
@@ -382,8 +381,8 @@
{
frame_number++;
//Decode the video stream packet
- avcodec_decode_video2(ctx->codec_ctx, ctx->frame, &got_frame, &ctx->packet);
- if (got_frame && frame_number % 1000 == 0)
+ avcodec_send_packet(ctx->codec_ctx, &ctx->packet);
+ if (avcodec_receive_frame(ctx->codec_ctx, ctx->frame) == 0 && frame_number % 1000 == 0)
{
// sws_scale is used to convert the pixel format to RGB24 from all other cases
sws_scale(
@@ -434,9 +433,8 @@
frame_number++;
//Decode the video stream packet
- avcodec_decode_video2(ctx->codec_ctx, ctx->frame, &got_frame, &ctx->packet);
-
- if (got_frame && frame_number % 25 == 0)
+ avcodec_send_packet(ctx->codec_ctx, &ctx->packet);
+ if (avcodec_receive_frame(ctx->codec_ctx, ctx->frame) == 0 && frame_number % 25 == 0)
{
float diff = (float)convert_pts_to_ms(ctx->packet.pts - prev_packet_pts, ctx->format_ctx->streams[ctx->video_stream_id]->time_base);
if (fabsf(diff) < 1000 * ctx->min_sub_duration) //If the minimum duration of a subtitle line is exceeded, process packet
@@ -548,7 +546,6 @@
{
// Do a binary search over the input video for faster processing
// printf("Duration: %d\n", (int)ctx->format_ctx->duration);
- int got_frame;
int seconds_time = 0;
for (seconds_time = 0; seconds_time < 20; seconds_time++)
{
@@ -568,8 +565,9 @@
{
if (ctx->packet.stream_index == ctx->video_stream_id)
{
- avcodec_decode_video2(ctx->codec_ctx, ctx->frame, &got_frame, &ctx->packet);
- if (got_frame)
+ avcodec_send_packet(ctx->codec_ctx, &ctx->packet);
+ if (avcodec_receive_frame(ctx->codec_ctx, ctx->frame) == 0)
+
{
// printf("%d\n", seek_time);
if (ctx->packet.pts < seek_time)