Revision 37287d41

View differences:

libavformat/avidec.c
24 24

  
25 25
#include "libavutil/intreadwrite.h"
26 26
#include "libavutil/bswap.h"
27
#include "libavcodec/bytestream.h"
27 28
#include "avformat.h"
28 29
#include "avi.h"
29 30
#include "dv.h"
......
49 50
    uint32_t pal[256];
50 51
    int has_pal;
51 52
    int dshow_block_align;            ///< block align variable used to emulate bugs in the MS dshow demuxer
53

  
54
    AVFormatContext *sub_ctx;
55
    AVPacket sub_pkt;
56
    uint8_t *sub_buffer;
52 57
} AVIStream;
53 58

  
54 59
typedef struct {
......
467 472
                codec_type = AVMEDIA_TYPE_AUDIO;
468 473
                break;
469 474
            case MKTAG('t', 'x', 't', 's'):
470
                //FIXME
471
                codec_type = AVMEDIA_TYPE_DATA; //AVMEDIA_TYPE_SUB ?  FIXME
475
                codec_type = AVMEDIA_TYPE_SUBTITLE;
472 476
                break;
473 477
            case MKTAG('d', 'a', 't', 's'):
474 478
                codec_type = AVMEDIA_TYPE_DATA;
......
599 603
                        ast->dshow_block_align = 0;
600 604
                    }
601 605
                    break;
606
                case AVMEDIA_TYPE_SUBTITLE:
607
                    st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
608
                    st->codec->codec_id   = CODEC_ID_PROBE;
609
                    break;
602 610
                default:
603 611
                    st->codec->codec_type = AVMEDIA_TYPE_DATA;
604 612
                    st->codec->codec_id= CODEC_ID_NONE;
......
688 696
    return 0;
689 697
}
690 698

  
699
static int read_gab2_sub(AVStream *st, AVPacket *pkt) {
700
    if (!strcmp(pkt->data, "GAB2") && AV_RL16(pkt->data+5) == 2) {
701
        uint8_t desc[256], *d = desc;
702
        uint8_t *end, *ptr = pkt->data+7;
703
        unsigned int size, desc_len = bytestream_get_le32(&ptr);
704
        int score = AVPROBE_SCORE_MAX / 2;
705
        AVIStream *ast = st->priv_data;
706
        AVInputFormat *sub_demuxer;
707
        AVRational time_base;
708
        ByteIOContext *pb;
709
        AVProbeData pd;
710

  
711
        if (desc_len > FFMAX(pkt->size-17, 0))
712
            return 0;
713

  
714
        end = ptr + desc_len;
715
        while (ptr < end-1) {
716
            uint8_t tmp;
717
            uint32_t ch;
718
            GET_UTF16(ch, ptr < end-1 ? bytestream_get_le16(&ptr) : 0, break;);
719
            PUT_UTF8(ch, tmp, if(d-desc < sizeof(desc)-1)  *d++ = tmp;);
720
        }
721
        *d = 0;
722
        if (*desc)
723
            av_metadata_set2(&st->metadata, "title", desc, 0);
724

  
725
        ptr = end + 2;
726
        size = bytestream_get_le32(&ptr);
727
        size = FFMIN(size, pkt->size+pkt->data-ptr);
728

  
729
        pd = (AVProbeData) { .buf = ptr, .buf_size = size };
730
        if (!(sub_demuxer = av_probe_input_format2(&pd, 1, &score)))
731
            return 0;
732

  
733
        pb = av_alloc_put_byte(ptr, size, 0, NULL, NULL, NULL, NULL);
734
        if (!av_open_input_stream(&ast->sub_ctx, pb, "", sub_demuxer, NULL)) {
735
            av_read_packet(ast->sub_ctx, &ast->sub_pkt);
736
            *st->codec = *ast->sub_ctx->streams[0]->codec;
737
            ast->sub_ctx->streams[0]->codec->extradata = NULL;
738
            time_base = ast->sub_ctx->streams[0]->time_base;
739
            av_set_pts_info(st, 64, time_base.num, time_base.den);
740
        }
741
        ast->sub_buffer = pkt->data;
742
        memset(pkt, 0, sizeof(*pkt));
743
        return 1;
744
    }
745
    return 0;
746
}
747

  
748
static AVStream *get_subtitle_pkt(AVFormatContext *s, AVStream *next_st,
749
                                  AVPacket *pkt)
750
{
751
    AVIStream *ast, *next_ast = next_st->priv_data;
752
    int64_t ts, next_ts, ts_min = INT64_MAX;
753
    AVStream *st, *sub_st = NULL;
754
    int i;
755

  
756
    next_ts = av_rescale_q(next_ast->frame_offset, next_st->time_base,
757
                           AV_TIME_BASE_Q);
758

  
759
    for (i=0; i<s->nb_streams; i++) {
760
        st  = s->streams[i];
761
        ast = st->priv_data;
762
        if (st->discard < AVDISCARD_ALL && ast->sub_pkt.data) {
763
            ts = av_rescale_q(ast->sub_pkt.dts, st->time_base, AV_TIME_BASE_Q);
764
            if (ts <= next_ts && ts < ts_min) {
765
                ts_min = ts;
766
                sub_st = st;
767
            }
768
        }
769
    }
770

  
771
    if (sub_st) {
772
        ast = sub_st->priv_data;
773
        *pkt = ast->sub_pkt;
774
        pkt->stream_index = sub_st->index;
775
        if (av_read_packet(ast->sub_ctx, &ast->sub_pkt) < 0)
776
            ast->sub_pkt.data = NULL;
777
    }
778
    return sub_st;
779
}
780

  
691 781
static int get_stream_idx(int *d){
692 782
    if(    d[0] >= '0' && d[0] <= '9'
693 783
        && d[1] >= '0' && d[1] <= '9'){
......
776 866
        AVIStream *ast= st->priv_data;
777 867
        int size, err;
778 868

  
869
        if(get_subtitle_pkt(s, st, pkt))
870
            return 0;
871

  
779 872
        if(ast->sample_size <= 1) // minorityreport.AVI block_align=1024 sample_size=1 IMA-ADPCM
780 873
            size= INT_MAX;
781 874
        else if(ast->sample_size < 32)
......
810 903
            pkt->flags |= AV_PKT_FLAG_KEY;
811 904
            if (size < 0)
812 905
                av_free_packet(pkt);
906
        } else if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE
907
                   && !st->codec->codec_tag && read_gab2_sub(st, pkt)) {
908
            ast->frame_offset++;
909
            avi->stream_index = -1;
910
            ast->remaining = 0;
911
            goto resync;
813 912
        } else {
814 913
            /* XXX: How to handle B-frames in AVI? */
815 914
            pkt->dts = ast->frame_offset;
......
1092 1191
    return ret;
1093 1192
}
1094 1193

  
1194
static void seek_subtitle(AVStream *st, AVStream *st2, int64_t timestamp)
1195
{
1196
    AVIStream *ast2 = st2->priv_data;
1197
    int64_t ts2 = av_rescale_q(timestamp, st->time_base, st2->time_base);
1198
    av_free_packet(&ast2->sub_pkt);
1199
    if (avformat_seek_file(ast2->sub_ctx, 0, INT64_MIN, ts2, ts2, 0) >= 0 ||
1200
        avformat_seek_file(ast2->sub_ctx, 0, ts2, ts2, INT64_MAX, 0) >= 0)
1201
        av_read_packet(ast2->sub_ctx, &ast2->sub_pkt);
1202
}
1203

  
1095 1204
static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
1096 1205
{
1097 1206
    AVIContext *avi = s->priv_data;
......
1141 1250
        ast2->packet_size=
1142 1251
        ast2->remaining= 0;
1143 1252

  
1253
        if (ast2->sub_ctx) {
1254
            seek_subtitle(st, st2, timestamp);
1255
            continue;
1256
        }
1257

  
1144 1258
        if (st2->nb_index_entries <= 0)
1145 1259
            continue;
1146 1260

  
......
1178 1292

  
1179 1293
    for(i=0;i<s->nb_streams;i++) {
1180 1294
        AVStream *st = s->streams[i];
1295
        AVIStream *ast = st->priv_data;
1181 1296
        av_free(st->codec->palctrl);
1297
        if (ast->sub_ctx) {
1298
            av_freep(ast->sub_ctx->pb);
1299
            av_close_input_stream(ast->sub_ctx);
1300
        }
1301
        av_free(ast->sub_buffer);
1302
        av_free_packet(&ast->sub_pkt);
1182 1303
    }
1183 1304

  
1184 1305
    if (avi->dv_demux)

Also available in: Unified diff