[FFmpeg-devel] [PATCH v7 02/12] fftools/play, probe: Adjust for subtitle changes

Soft Works softworkz at hotmail.com
Sat Sep 18 06:52:43 EEST 2021


Signed-off-by: softworkz <softworkz at hotmail.com>
---
 fftools/ffplay.c  | 50 +++++++++++++++++++++++------------------------
 fftools/ffprobe.c | 49 ++++++++++++++++++++++++++++++++++------------
 2 files changed, 62 insertions(+), 37 deletions(-)

diff --git a/fftools/ffplay.c b/fftools/ffplay.c
index ccea0e4578..08e5d53f9c 100644
--- a/fftools/ffplay.c
+++ b/fftools/ffplay.c
@@ -152,7 +152,6 @@ typedef struct Clock {
 /* Common struct for handling all types of decoded data and allocated render buffers. */
 typedef struct Frame {
     AVFrame *frame;
-    AVSubtitle sub;
     int serial;
     double pts;           /* presentation timestamp for the frame */
     double duration;      /* estimated duration of the frame */
@@ -586,7 +585,7 @@ static int decoder_init(Decoder *d, AVCodecContext *avctx, PacketQueue *queue, S
     return 0;
 }
 
-static int decoder_decode_frame(Decoder *d, AVFrame *frame, AVSubtitle *sub) {
+static int decoder_decode_frame(Decoder *d, AVFrame *frame) {
     int ret = AVERROR(EAGAIN);
 
     for (;;) {
@@ -654,7 +653,7 @@ static int decoder_decode_frame(Decoder *d, AVFrame *frame, AVSubtitle *sub) {
 
         if (d->avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
             int got_frame = 0;
-            ret = avcodec_decode_subtitle2(d->avctx, sub, &got_frame, d->pkt);
+            ret = avcodec_decode_subtitle3(d->avctx, frame, &got_frame, d->pkt);
             if (ret < 0) {
                 ret = AVERROR(EAGAIN);
             } else {
@@ -683,7 +682,6 @@ static void decoder_destroy(Decoder *d) {
 static void frame_queue_unref_item(Frame *vp)
 {
     av_frame_unref(vp->frame);
-    avsubtitle_free(&vp->sub);
 }
 
 static int frame_queue_init(FrameQueue *f, PacketQueue *pktq, int max_size, int keep_last)
@@ -981,7 +979,7 @@ static void video_image_display(VideoState *is)
         if (frame_queue_nb_remaining(&is->subpq) > 0) {
             sp = frame_queue_peek(&is->subpq);
 
-            if (vp->pts >= sp->pts + ((float) sp->sub.start_display_time / 1000)) {
+            if (vp->pts >= sp->pts + ((float) sp->frame->subtitle_start_time / 1000)) {
                 if (!sp->uploaded) {
                     uint8_t* pixels[4];
                     int pitch[4];
@@ -993,8 +991,8 @@ static void video_image_display(VideoState *is)
                     if (realloc_texture(&is->sub_texture, SDL_PIXELFORMAT_ARGB8888, sp->width, sp->height, SDL_BLENDMODE_BLEND, 1) < 0)
                         return;
 
-                    for (i = 0; i < sp->sub.num_rects; i++) {
-                        AVSubtitleRect *sub_rect = sp->sub.rects[i];
+                    for (i = 0; i < sp->frame->num_subtitle_areas; i++) {
+                        AVSubtitleArea *sub_rect = sp->frame->subtitle_areas[i];
 
                         sub_rect->x = av_clip(sub_rect->x, 0, sp->width );
                         sub_rect->y = av_clip(sub_rect->y, 0, sp->height);
@@ -1041,13 +1039,15 @@ static void video_image_display(VideoState *is)
         int i;
         double xratio = (double)rect.w / (double)sp->width;
         double yratio = (double)rect.h / (double)sp->height;
-        for (i = 0; i < sp->sub.num_rects; i++) {
-            SDL_Rect *sub_rect = (SDL_Rect*)sp->sub.rects[i];
-            SDL_Rect target = {.x = rect.x + sub_rect->x * xratio,
-                               .y = rect.y + sub_rect->y * yratio,
-                               .w = sub_rect->w * xratio,
-                               .h = sub_rect->h * yratio};
-            SDL_RenderCopy(renderer, is->sub_texture, sub_rect, &target);
+        for (i = 0; i < sp->frame->num_subtitle_areas; i++) {
+            AVSubtitleArea *area = sp->frame->subtitle_areas[i];
+            SDL_Rect sub_rect = { .x = area->x, .y = area->y,
+                                  .w = area->w, .h = area->h};
+            SDL_Rect target = {.x = rect.x + sub_rect.x * xratio,
+                               .y = rect.y + sub_rect.y * yratio,
+                               .w = sub_rect.w * xratio,
+                               .h = sub_rect.h * yratio};
+            SDL_RenderCopy(renderer, is->sub_texture, &sub_rect, &target);
         }
 #endif
     }
@@ -1651,13 +1651,13 @@ retry:
                         sp2 = NULL;
 
                     if (sp->serial != is->subtitleq.serial
-                            || (is->vidclk.pts > (sp->pts + ((float) sp->sub.end_display_time / 1000)))
-                            || (sp2 && is->vidclk.pts > (sp2->pts + ((float) sp2->sub.start_display_time / 1000))))
+                            || (is->vidclk.pts > (sp->pts + ((float) sp->frame->subtitle_end_time / 1000)))
+                            || (sp2 && is->vidclk.pts > (sp2->pts + ((float) sp2->frame->subtitle_start_time / 1000))))
                     {
                         if (sp->uploaded) {
                             int i;
-                            for (i = 0; i < sp->sub.num_rects; i++) {
-                                AVSubtitleRect *sub_rect = sp->sub.rects[i];
+                            for (i = 0; i < sp->frame->num_subtitle_areas; i++) {
+                                AVSubtitleRect *sub_rect = sp->frame->subtitle_areas[i];
                                 uint8_t *pixels;
                                 int pitch, j;
 
@@ -1774,7 +1774,7 @@ static int get_video_frame(VideoState *is, AVFrame *frame)
 {
     int got_picture;
 
-    if ((got_picture = decoder_decode_frame(&is->viddec, frame, NULL)) < 0)
+    if ((got_picture = decoder_decode_frame(&is->viddec, frame)) < 0)
         return -1;
 
     if (got_picture) {
@@ -2048,7 +2048,7 @@ static int audio_thread(void *arg)
         return AVERROR(ENOMEM);
 
     do {
-        if ((got_frame = decoder_decode_frame(&is->auddec, frame, NULL)) < 0)
+        if ((got_frame = decoder_decode_frame(&is->auddec, frame)) < 0)
             goto the_end;
 
         if (got_frame) {
@@ -2246,14 +2246,14 @@ static int subtitle_thread(void *arg)
         if (!(sp = frame_queue_peek_writable(&is->subpq)))
             return 0;
 
-        if ((got_subtitle = decoder_decode_frame(&is->subdec, NULL, &sp->sub)) < 0)
+        if ((got_subtitle = decoder_decode_frame(&is->subdec, sp->frame)) < 0)
             break;
 
         pts = 0;
 
-        if (got_subtitle && sp->sub.format == 0) {
-            if (sp->sub.pts != AV_NOPTS_VALUE)
-                pts = sp->sub.pts / (double)AV_TIME_BASE;
+        if (got_subtitle && sp->frame->format == AV_SUBTITLE_FMT_BITMAP) {
+            if (sp->frame->subtitle_pts != AV_NOPTS_VALUE)
+                pts = sp->frame->subtitle_pts / (double)AV_TIME_BASE;
             sp->pts = pts;
             sp->serial = is->subdec.pkt_serial;
             sp->width = is->subdec.avctx->width;
@@ -2263,7 +2263,7 @@ static int subtitle_thread(void *arg)
             /* now we can update the picture count */
             frame_queue_push(&is->subpq);
         } else if (got_subtitle) {
-            avsubtitle_free(&sp->sub);
+            av_frame_free(&sp->frame);
         }
     }
     return 0;
diff --git a/fftools/ffprobe.c b/fftools/ffprobe.c
index d8e968321e..880f05a6c2 100644
--- a/fftools/ffprobe.c
+++ b/fftools/ffprobe.c
@@ -2208,22 +2208,43 @@ static void show_packet(WriterContext *w, InputFile *ifile, AVPacket *pkt, int p
     fflush(stdout);
 }
 
-static void show_subtitle(WriterContext *w, AVSubtitle *sub, AVStream *stream,
+static void show_subtitle(WriterContext *w, AVFrame *sub, AVStream *stream,
                           AVFormatContext *fmt_ctx)
 {
     AVBPrint pbuf;
+    const char *s;
 
     av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
 
     writer_print_section_header(w, SECTION_ID_SUBTITLE);
 
     print_str ("media_type",         "subtitle");
-    print_ts  ("pts",                 sub->pts);
-    print_time("pts_time",            sub->pts, &AV_TIME_BASE_Q);
-    print_int ("format",              sub->format);
-    print_int ("start_display_time",  sub->start_display_time);
-    print_int ("end_display_time",    sub->end_display_time);
-    print_int ("num_rects",           sub->num_rects);
+    print_ts  ("pts",                 sub->subtitle_pts);
+    print_time("pts_time",            sub->subtitle_pts, &AV_TIME_BASE_Q);
+
+    // Remain compatible with previous outputs
+    switch (sub->format) {
+    case AV_SUBTITLE_FMT_BITMAP:
+        print_int ("format",         0);
+        break;
+    case AV_SUBTITLE_FMT_TEXT:
+        print_int ("format",         1);
+        break;
+    case AV_SUBTITLE_FMT_ASS:
+        print_int ("format",         1);
+        break;
+    default:
+        print_int ("format",         -1);
+        break;
+    }
+
+    s = av_get_subtitle_fmt_name(sub->format);
+    if (s) print_str    ("format_str", s);
+    else   print_str_opt("format_str", "unknown");
+
+    print_int ("start_display_time",  sub->subtitle_start_time);
+    print_int ("end_display_time",    sub->subtitle_end_time);
+    print_int ("num_subtitle_rects",           sub->num_subtitle_areas);
 
     writer_print_section_footer(w);
 
@@ -2388,7 +2409,7 @@ static av_always_inline int process_frame(WriterContext *w,
     AVFormatContext *fmt_ctx = ifile->fmt_ctx;
     AVCodecContext *dec_ctx = ifile->streams[pkt->stream_index].dec_ctx;
     AVCodecParameters *par = ifile->streams[pkt->stream_index].st->codecpar;
-    AVSubtitle sub;
+    AVFrame *sub = NULL;
     int ret = 0, got_frame = 0;
 
     clear_log(1);
@@ -2416,8 +2437,12 @@ static av_always_inline int process_frame(WriterContext *w,
             break;
 
         case AVMEDIA_TYPE_SUBTITLE:
-            if (*packet_new)
-                ret = avcodec_decode_subtitle2(dec_ctx, &sub, &got_frame, pkt);
+            if (*packet_new) {
+                sub = av_frame_alloc();
+                if (!sub)
+                    return AVERROR(ENOMEM);
+                ret = avcodec_decode_subtitle3(dec_ctx, sub, &got_frame, pkt);
+            }
             *packet_new = 0;
             break;
         default:
@@ -2434,11 +2459,11 @@ static av_always_inline int process_frame(WriterContext *w,
         nb_streams_frames[pkt->stream_index]++;
         if (do_show_frames)
             if (is_sub)
-                show_subtitle(w, &sub, ifile->streams[pkt->stream_index].st, fmt_ctx);
+                show_subtitle(w, sub, ifile->streams[pkt->stream_index].st, fmt_ctx);
             else
                 show_frame(w, frame, ifile->streams[pkt->stream_index].st, fmt_ctx);
         if (is_sub)
-            avsubtitle_free(&sub);
+            av_frame_free(&sub);
     }
     return got_frame || *packet_new;
 }
-- 
2.30.2.windows.1



More information about the ffmpeg-devel mailing list