[FFmpeg-devel] [PATCH 8/8] avidec: demux ASS and SRT tracks

Aurelien Jacobs aurel
Wed Jul 21 11:53:27 CEST 2010


---
 libavformat/avidec.c |  119 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 117 insertions(+), 2 deletions(-)

diff --git a/libavformat/avidec.c b/libavformat/avidec.c
index 05cfd98..bd9eddb 100644
--- a/libavformat/avidec.c
+++ b/libavformat/avidec.c
@@ -24,6 +24,7 @@
 
 #include "libavutil/intreadwrite.h"
 #include "libavutil/bswap.h"
+#include "libavcodec/bytestream.h"
 #include "avformat.h"
 #include "avi.h"
 #include "dv.h"
@@ -49,6 +50,9 @@ typedef struct AVIStream {
     uint32_t pal[256];
     int has_pal;
     int dshow_block_align;            ///< block align variable used to emulate bugs in the MS dshow demuxer
+
+    AVFormatContext *sub_ctx;
+    AVPacket sub_pkt;
 } AVIStream;
 
 typedef struct {
@@ -467,8 +471,7 @@ static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
                 codec_type = AVMEDIA_TYPE_AUDIO;
                 break;
             case MKTAG('t', 'x', 't', 's'):
-                //FIXME
-                codec_type = AVMEDIA_TYPE_DATA; //AVMEDIA_TYPE_SUB ?  FIXME
+                codec_type = AVMEDIA_TYPE_SUBTITLE;
                 break;
             case MKTAG('d', 'a', 't', 's'):
                 codec_type = AVMEDIA_TYPE_DATA;
@@ -599,6 +602,10 @@ static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
                         ast->dshow_block_align = 0;
                     }
                     break;
+                case AVMEDIA_TYPE_SUBTITLE:
+                    st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
+                    st->codec->codec_id   = CODEC_ID_PROBE;
+                    break;
                 default:
                     st->codec->codec_type = AVMEDIA_TYPE_DATA;
                     st->codec->codec_id= CODEC_ID_NONE;
@@ -688,6 +695,84 @@ static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
     return 0;
 }
 
+static int read_gab2_sub(AVStream *st, AVPacket *pkt) {
+    if (!strcmp(pkt->data, "GAB2") && AV_RL16(pkt->data+5) == 2) {
+        uint8_t tmp, desc[256], *d = desc;
+        const uint8_t *ptr = pkt->data+7;
+        unsigned int size, name_size = bytestream_get_le32(&ptr);
+        int i, score = AVPROBE_SCORE_MAX / 2;
+        AVIStream *ast = st->priv_data;
+        AVInputFormat *sub_demuxer;
+        AVRational time_base;
+        ByteIOContext *pb;
+        AVProbeData pd;
+
+        if (name_size > pkt->size-17)
+            return 0;
+
+        for (i=0; i<name_size; i+=2)
+            PUT_UTF8(bytestream_get_le16(&ptr), tmp,
+                     if(d < desc+sizeof(desc)-1)  *d++ = tmp;);
+        *d = 0;
+        if (*desc)
+            av_metadata_set2(&st->metadata, "title", desc, 0);
+
+        ptr += 2;
+        size = bytestream_get_le32(&ptr);
+        size = FFMIN(size, pkt->size+pkt->data-ptr);
+        memmove(pkt->data, ptr, size);
+        pkt->size = size;
+
+        pd = (AVProbeData) { .buf = pkt->data, .buf_size = pkt->size };
+        if (!(sub_demuxer = av_probe_input_format2(&pd, 1, &score)))
+            return 0;
+
+        pb = av_alloc_put_byte(pkt->data, pkt->size, 0, NULL, NULL, NULL, NULL);
+        if (!av_open_input_stream(&ast->sub_ctx, pb, "", sub_demuxer, NULL))
+            av_read_packet(ast->sub_ctx, &ast->sub_pkt);
+        *st->codec = *ast->sub_ctx->streams[0]->codec;
+        ast->sub_ctx->streams[0]->codec->extradata = NULL;
+        time_base = ast->sub_ctx->streams[0]->time_base;
+        av_set_pts_info(st, 64, time_base.num, time_base.den);
+        memset(pkt, 0, sizeof(*pkt));
+        return 1;
+    }
+    return 0;
+}
+
+static AVStream *get_subtitle_pkt(AVFormatContext *s, AVStream *next_st,
+                                  AVPacket *pkt)
+{
+    AVIStream *ast, *next_ast = next_st->priv_data;
+    int64_t ts, next_ts, ts_min = INT64_MAX;
+    AVStream *st, *sub_st = NULL;
+    int i;
+
+    next_ts = av_rescale_q(next_ast->frame_offset, next_st->time_base,
+                           AV_TIME_BASE_Q);
+
+    for (i=0; i<s->nb_streams; i++) {
+        st  = s->streams[i];
+        ast = st->priv_data;
+        if (st->discard < AVDISCARD_ALL && ast->sub_pkt.data) {
+            ts = av_rescale_q(ast->sub_pkt.dts, st->time_base, AV_TIME_BASE_Q);
+            if (ts <= next_ts && ts < ts_min) {
+                ts_min = ts;
+                sub_st = st;
+            }
+        }
+    }
+
+    if (sub_st) {
+        ast = sub_st->priv_data;
+        *pkt = ast->sub_pkt;
+        pkt->stream_index = sub_st->index;
+        if (av_read_packet(ast->sub_ctx, &ast->sub_pkt) < 0)
+            ast->sub_pkt.data = NULL;
+    }
+    return sub_st;
+}
+
 static int get_stream_idx(int *d){
     if(    d[0] >= '0' && d[0] <= '9'
         && d[1] >= '0' && d[1] <= '9'){
@@ -776,6 +861,9 @@ resync:
         AVIStream *ast= st->priv_data;
         int size, err;
 
+        if(get_subtitle_pkt(s, st, pkt))
+            return 0;
+
         if(ast->sample_size <= 1) // minorityreport.AVI block_align=1024 sample_size=1 IMA-ADPCM
             size= INT_MAX;
         else if(ast->sample_size < 32)
@@ -810,6 +898,12 @@ resync:
             pkt->flags |= AV_PKT_FLAG_KEY;
             if (size < 0)
                 av_free_packet(pkt);
+        } else if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE
+                   && !st->codec->codec_tag && read_gab2_sub(st, pkt)) {
+            ast->frame_offset++;
+            avi->stream_index = -1;
+            ast->remaining = 0;
+            goto resync;
         } else {
             /* XXX: How to handle B-frames in AVI? */
             pkt->dts = ast->frame_offset;
@@ -1092,6 +1186,20 @@ static int avi_load_index(AVFormatContext *s)
     return ret;
 }
 
+static int seek_subtitle(AVStream *st, AVStream *st2, int64_t timestamp)
+{
+    AVIStream *ast2 = st2->priv_data;
+    if (ast2->sub_ctx) {
+        int64_t ts2 = av_rescale_q(timestamp, st->time_base, st2->time_base);
+        av_free_packet(&ast2->sub_pkt);
+        if (avformat_seek_file(ast2->sub_ctx, 0, INT64_MIN, ts2, ts2, 0) >= 0 ||
+            avformat_seek_file(ast2->sub_ctx, 0, ts2, ts2, INT64_MAX, 0) >= 0)
+            av_read_packet(ast2->sub_ctx, &ast2->sub_pkt);
+        return 1;
+    }
+    return 0;
+}
+
 static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
 {
     AVIContext *avi = s->priv_data;
@@ -1141,6 +1249,9 @@ static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp
         ast2->packet_size=
         ast2->remaining= 0;
 
+        if (seek_subtitle(st, st2, timestamp))
+            continue;
+
         if (st2->nb_index_entries <= 0)
             continue;
 
@@ -1178,7 +1289,11 @@ static int avi_read_close(AVFormatContext *s)
 
     for(i=0;i<s->nb_streams;i++) {
         AVStream *st = s->streams[i];
+        AVIStream *ast = st->priv_data;
         av_free(st->codec->palctrl);
+        if (ast->sub_ctx)
+            av_close_input_file(ast->sub_ctx);
+        av_free_packet(&ast->sub_pkt);
     }
 
     if (avi->dv_demux)




More information about the ffmpeg-devel mailing list