+static int read_gab2_sub(AVStream *st, AVPacket *pkt) {
+ if (!strcmp(pkt->data, "GAB2") && AV_RL16(pkt->data+5) == 2) {
+ uint8_t desc[256];
+ int score = AVPROBE_SCORE_MAX / 2, ret;
+ AVIStream *ast = st->priv_data;
+ AVInputFormat *sub_demuxer;
+ AVRational time_base;
+ AVIOContext *pb = avio_alloc_context( pkt->data + 7,
+ pkt->size - 7,
+ 0, NULL, NULL, NULL, NULL);
+ AVProbeData pd;
+ unsigned int desc_len = avio_rl32(pb);
+
+ if (desc_len > pb->buf_end - pb->buf_ptr)
+ goto error;
+
+ ret = avio_get_str16le(pb, desc_len, desc, sizeof(desc));
+ avio_seek(pb, desc_len - ret, SEEK_CUR);
+ if (*desc)
+ av_metadata_set2(&st->metadata, "title", desc, 0);
+
+ avio_rl16(pb); /* flags? */
+ avio_rl32(pb); /* data size */
+
+ pd = (AVProbeData) { .buf = pb->buf_ptr, .buf_size = pb->buf_end - pb->buf_ptr };
+ if (!(sub_demuxer = av_probe_input_format2(&pd, 1, &score)))
+ goto error;
+
+ 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);
+ }
+ ast->sub_buffer = pkt->data;
+ memset(pkt, 0, sizeof(*pkt));
+ return 1;
+error:
+ av_freep(&pb);
+ }
+ 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 && 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;
+}
+