static int sami_read_header(AVFormatContext *s) { SAMIContext *sami = s->priv_data; AVStream *st = avformat_new_stream(s, NULL); AVBPrint buf, hdr_buf; char c = 0; int res = 0, got_first_sync_point = 0; FFTextReader tr; ff_text_init_avio(&tr, s->pb); if (!st) return AVERROR(ENOMEM); avpriv_set_pts_info(st, 64, 1, 1000); st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; st->codec->codec_id = AV_CODEC_ID_SAMI; av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED); av_bprint_init(&hdr_buf, 0, AV_BPRINT_SIZE_UNLIMITED); while (!ff_text_eof(&tr)) { AVPacket *sub; const int64_t pos = ff_text_pos(&tr) - (c != 0); int is_sync, n = ff_smil_extract_next_text_chunk(&tr, &buf, &c); if (n == 0) break; is_sync = !av_strncasecmp(buf.str, "<SYNC", 5); if (is_sync) got_first_sync_point = 1; if (!got_first_sync_point) { av_bprintf(&hdr_buf, "%s", buf.str); } else { sub = ff_subtitles_queue_insert(&sami->q, buf.str, buf.len, !is_sync); if (!sub) { res = AVERROR(ENOMEM); goto end; } if (is_sync) { const char *p = ff_smil_get_attr_ptr(buf.str, "Start"); sub->pos = pos; sub->pts = p ? strtol(p, NULL, 10) : 0; sub->duration = -1; } } av_bprint_clear(&buf); } res = avpriv_bprint_to_extradata(st->codec, &hdr_buf); if (res < 0) goto end; ff_subtitles_queue_finalize(&sami->q); end: av_bprint_finalize(&buf, NULL); return res; }
static int realtext_read_header(AVFormatContext *s) { RealTextContext *rt = s->priv_data; AVStream *st = avformat_new_stream(s, NULL); AVBPrint buf; char c = 0; int res = 0, duration = read_ts("60"); // default duration is 60 seconds if (!st) return AVERROR(ENOMEM); avpriv_set_pts_info(st, 64, 1, 100); st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; st->codec->codec_id = AV_CODEC_ID_REALTEXT; av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED); while (!url_feof(s->pb)) { AVPacket *sub; const int64_t pos = avio_tell(s->pb) - (c != 0); int n = ff_smil_extract_next_chunk(s->pb, &buf, &c); if (n == 0) break; if (!av_strncasecmp(buf.str, "<window", 7)) { /* save header to extradata */ const char *p = ff_smil_get_attr_ptr(buf.str, "duration"); if (p) duration = read_ts(p); st->codec->extradata = av_strdup(buf.str); if (!st->codec->extradata) { res = AVERROR(ENOMEM); goto end; } st->codec->extradata_size = buf.len + 1; } else { /* if we just read a <time> tag, introduce a new event, otherwise merge * with the previous one */ int merge = !av_strncasecmp(buf.str, "<time", 5) ? 0 : 1; sub = ff_subtitles_queue_insert(&rt->q, buf.str, buf.len, merge); if (!sub) { res = AVERROR(ENOMEM); goto end; } if (!merge) { const char *begin = ff_smil_get_attr_ptr(buf.str, "begin"); const char *end = ff_smil_get_attr_ptr(buf.str, "end"); sub->pos = pos; sub->pts = begin ? read_ts(begin) : 0; sub->duration = end ? (read_ts(end) - sub->pts) : duration; } } av_bprint_clear(&buf); } ff_subtitles_queue_finalize(&rt->q); end: av_bprint_finalize(&buf, NULL); return res; }