History

lavf: move demuxing-specific fields from FFFormatContext to FormatContextInternal

Anton Khirnov committed 2 years ago

Commit cb80ec01 parent 6d05e7e5 files · +85 −68Browse files

Unified · Split

5 files
  1. libavformat/avformat.c +7 −5
  2. libavformat/avformat_internal.h +31 −0
  3. libavformat/demux.c +40 −32
  4. libavformat/demux_utils.c +7 −6
  5. libavformat/internal.h +0 −25
libavformat/avformat.c+7−5
@@ -137,12 +137,13 @@ void ff_remove_stream_group(AVFormatContext *s, AVStreamGroup *stg) · 136 unmodified lines
137137 /* XXX: suppress the packet queue */
138138 void ff_flush_packet_queue(AVFormatContext *s)
139139 {
140− FFFormatContext *const si = ffformatcontext(s);
141− avpriv_packet_list_free(&si->parse_queue);
140+ FormatContextInternal *const fci = ff_fc_internal(s);
141+ FFFormatContext *const si = &fci->fc;
142+ avpriv_packet_list_free(&fci->parse_queue);
142143 avpriv_packet_list_free(&si->packet_buffer);
143− avpriv_packet_list_free(&si->raw_packet_buffer);
144+ avpriv_packet_list_free(&fci->raw_packet_buffer);
144145
145− si->raw_packet_buffer_size = 0;
146+ fci->raw_packet_buffer_size = 0;
146147 }
147148
148149 void avformat_free_context(AVFormatContext *s)
@@ -191,7 +192,8 @@ void avformat_free_context(AVFormatContext *s) · 42 unmodified lines
191192 av_packet_free(&si->parse_pkt);
192193 av_freep(&s->streams);
193194 av_freep(&s->stream_groups);
194− ff_flush_packet_queue(s);
195+ if (s->iformat)
196+ ff_flush_packet_queue(s);
195197 av_freep(&s->url);
196198 av_free(s);
197199 }
libavformat/avformat_internal.h+31−0
@@ -63,6 +63,37 @@ typedef struct FormatContextInternal { · 62 unmodified lines
6363 int missing_ts_warning;
6464 #endif
6565 };
66+
67+ // demuxing only
68+ struct {
69+ /**
70+ * Raw packets from the demuxer, prior to parsing and decoding.
71+ * This buffer is used for buffering packets until the codec can
72+ * be identified, as parsing cannot be done without knowing the
73+ * codec.
74+ */
75+ PacketList raw_packet_buffer;
76+
77+ /**
78+ * Sum of the size of packets in raw_packet_buffer, in bytes.
79+ */
80+ int raw_packet_buffer_size;
81+
82+ /**
83+ * Packets split by the parser get queued here.
84+ */
85+ PacketList parse_queue;
86+
87+ /**
88+ * Contexts and child contexts do not contain a metadata option
89+ */
90+ int metafree;
91+
92+ /**
93+ * Set if chapter ids are strictly monotonic.
94+ */
95+ int chapter_ids_monotonic;
96+ };
6697 };
6798
6899 #if FF_API_LAVF_SHORTEST
libavformat/demux.c+40−32
@@ -216,6 +216,7 @@ static int update_stream_avctx(AVFormatContext *s) · 215 unmodified lines
216216 int avformat_open_input(AVFormatContext **ps, const char *filename,
217217 const AVInputFormat *fmt, AVDictionary **options)
218218 {
219+ FormatContextInternal *fci;
219220 AVFormatContext *s = *ps;
220221 FFFormatContext *si;
221222 AVDictionary *tmp = NULL;
@@ -224,7 +225,8 @@ int avformat_open_input(AVFormatContext **ps, const char *filename, · 2 unmodified lines
224225
225226 if (!s && !(s = avformat_alloc_context()))
226227 return AVERROR(ENOMEM);
227− si = ffformatcontext(s);
228+ fci = ff_fc_internal(s);
229+ si = &fci->fc;
228230 if (!s->av_class) {
229231 av_log(NULL, AV_LOG_ERROR, "Input context has not been properly allocated by avformat_alloc_context() and is not NULL either\n");
230232 return AVERROR(EINVAL);
@@ -337,7 +339,7 @@ int avformat_open_input(AVFormatContext **ps, const char *filename, · 106 unmodified lines
337339 if (s->pb && !si->data_offset)
338340 si->data_offset = avio_tell(s->pb);
339341
340− si->raw_packet_buffer_size = 0;
342+ fci->raw_packet_buffer_size = 0;
341343
342344 update_stream_avctx(s);
343345
@@ -411,7 +413,7 @@ static void force_codec_ids(AVFormatContext *s, AVStream *st) · 67 unmodified lines
411413
412414 static int probe_codec(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
413415 {
414− FFFormatContext *const si = ffformatcontext(s);
416+ FormatContextInternal *const fci = ff_fc_internal(s);
415417 FFStream *const sti = ffstream(st);
416418
417419 if (sti->request_probe > 0) {
@@ -441,8 +443,8 @@ no_packet: · 23 unmodified lines
441443 }
442444 }
443445
444− end = si->raw_packet_buffer_size >= s->probesize
445− || sti->probe_packets <= 0;
446+ end = fci->raw_packet_buffer_size >= s->probesize ||
447+ sti->probe_packets <= 0;
446448
447449 if (end || av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)) {
448450 int score = set_codec_from_probe_data(s, st, pd);
@@ -564,7 +566,7 @@ static void update_timestamps(AVFormatContext *s, AVStream *st, AVPacket *pkt) · 115 unmodified lines
564566 */
565567 static int handle_new_packet(AVFormatContext *s, AVPacket *pkt, int allow_passthrough)
566568 {
567− FFFormatContext *const si = ffformatcontext(s);
569+ FormatContextInternal *const fci = ff_fc_internal(s);
568570 AVStream *st;
569571 FFStream *sti;
570572 int err;
@@ -588,17 +590,17 @@ static int handle_new_packet(AVFormatContext *s, AVPacket *pkt, int allow_passth · 17 unmodified lines
588590
589591 update_timestamps(s, st, pkt);
590592
591− if (sti->request_probe <= 0 && allow_passthrough && !si->raw_packet_buffer.head)
593+ if (sti->request_probe <= 0 && allow_passthrough && !fci->raw_packet_buffer.head)
592594 return 0;
593595
594− err = avpriv_packet_list_put(&si->raw_packet_buffer, pkt, NULL, 0);
596+ err = avpriv_packet_list_put(&fci->raw_packet_buffer, pkt, NULL, 0);
595597 if (err < 0) {
596598 av_packet_unref(pkt);
597599 return err;
598600 }
599601
600− pkt = &si->raw_packet_buffer.tail->pkt;
601− si->raw_packet_buffer_size += pkt->size;
602+ pkt = &fci->raw_packet_buffer.tail->pkt;
603+ fci->raw_packet_buffer_size += pkt->size;
602604
603605 err = probe_codec(s, st, pkt);
604606 if (err < 0)
@@ -616,7 +618,7 @@ int ff_buffer_packet(AVFormatContext *s, AVPacket *pkt) · 11 unmodified lines
616618
617619 int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
618620 {
619− FFFormatContext *const si = ffformatcontext(s);
621+ FormatContextInternal *const fci = ff_fc_internal(s);
620622 int err;
621623
622624 #if FF_API_INIT_PACKET
@@ -630,16 +632,16 @@ FF_ENABLE_DEPRECATION_WARNINGS · 7 unmodified lines
630632 #endif
631633
632634 for (;;) {
633− PacketListEntry *pktl = si->raw_packet_buffer.head;
635+ PacketListEntry *pktl = fci->raw_packet_buffer.head;
634636
635637 if (pktl) {
636638 AVStream *const st = s->streams[pktl->pkt.stream_index];
637− if (si->raw_packet_buffer_size >= s->probesize)
639+ if (fci->raw_packet_buffer_size >= s->probesize)
638640 if ((err = probe_codec(s, st, NULL)) < 0)
639641 return err;
640642 if (ffstream(st)->request_probe <= 0) {
641− avpriv_packet_list_get(&si->raw_packet_buffer, pkt);
642− si->raw_packet_buffer_size -= pkt->size;
643+ avpriv_packet_list_get(&fci->raw_packet_buffer, pkt);
644+ fci->raw_packet_buffer_size -= pkt->size;
643645 return 0;
644646 }
645647 }
@@ -766,11 +768,12 @@ static int has_decode_delay_been_guessed(AVStream *st) · 120 unmodified lines
766768 static PacketListEntry *get_next_pkt(AVFormatContext *s, AVStream *st,
767769 PacketListEntry *pktl)
768770 {
769− FFFormatContext *const si = ffformatcontext(s);
771+ FormatContextInternal *const fci = ff_fc_internal(s);
772+ FFFormatContext *const si = &fci->fc;
770773 if (pktl->next)
771774 return pktl->next;
772775 if (pktl == si->packet_buffer.tail)
773− return si->parse_queue.head;
776+ return fci->parse_queue.head;
774777 return NULL;
775778 }
776779
@@ -850,10 +853,11 @@ static void update_dts_from_pts(AVFormatContext *s, int stream_index, · 73 unmodified lines
850853 static void update_initial_timestamps(AVFormatContext *s, int stream_index,
851854 int64_t dts, int64_t pts, AVPacket *pkt)
852855 {
853− FFFormatContext *const si = ffformatcontext(s);
856+ FormatContextInternal *const fci = ff_fc_internal(s);
857+ FFFormatContext *const si = &fci->fc;
854858 AVStream *const st = s->streams[stream_index];
855859 FFStream *const sti = ffstream(st);
856− PacketListEntry *pktl = si->packet_buffer.head ? si->packet_buffer.head : si->parse_queue.head;
860+ PacketListEntry *pktl = si->packet_buffer.head ? si->packet_buffer.head : fci->parse_queue.head;
857861
858862 uint64_t shift;
859863
@@ -903,9 +907,10 @@ static void update_initial_timestamps(AVFormatContext *s, int stream_index, · 43 unmodified lines
903907 static void update_initial_durations(AVFormatContext *s, AVStream *st,
904908 int stream_index, int64_t duration)
905909 {
906− FFFormatContext *const si = ffformatcontext(s);
910+ FormatContextInternal *const fci = ff_fc_internal(s);
911+ FFFormatContext *const si = &fci->fc;
907912 FFStream *const sti = ffstream(st);
908− PacketListEntry *pktl = si->packet_buffer.head ? si->packet_buffer.head : si->parse_queue.head;
913+ PacketListEntry *pktl = si->packet_buffer.head ? si->packet_buffer.head : fci->parse_queue.head;
909914 int64_t cur_dts = RELATIVE_TS_BASE;
910915
911916 if (sti->first_dts != AV_NOPTS_VALUE) {
@@ -931,7 +936,7 @@ static void update_initial_durations(AVFormatContext *s, AVStream *st, · 19 unmodified lines
931936 av_log(s, AV_LOG_DEBUG, "first_dts %s but no packet with dts in the queue\n", av_ts2str(sti->first_dts));
932937 return;
933938 }
934− pktl = si->packet_buffer.head ? si->packet_buffer.head : si->parse_queue.head;
939+ pktl = si->packet_buffer.head ? si->packet_buffer.head : fci->parse_queue.head;
935940 sti->first_dts = cur_dts;
936941 } else if (sti->cur_dts != RELATIVE_TS_BASE)
937942 return;
@@ -963,7 +968,8 @@ static void compute_pkt_fields(AVFormatContext *s, AVStream *st, · 25 unmodified lines
963968 AVCodecParserContext *pc, AVPacket *pkt,
964969 int64_t next_dts, int64_t next_pts)
965970 {
966− FFFormatContext *const si = ffformatcontext(s);
971+ FormatContextInternal *const fci = ff_fc_internal(s);
972+ FFFormatContext *const si = &fci->fc;
967973 FFStream *const sti = ffstream(st);
968974 int num, den, presentation_delayed, delay;
969975 int64_t offset;
@@ -1048,7 +1054,7 @@ static void compute_pkt_fields(AVFormatContext *s, AVStream *st, · 78 unmodified lines
10481054 }
10491055 }
10501056
1051− if (pkt->duration > 0 && (si->packet_buffer.head || si->parse_queue.head))
1057+ if (pkt->duration > 0 && (si->packet_buffer.head || fci->parse_queue.head))
10521058 update_initial_durations(s, st, pkt->stream_index, pkt->duration);
10531059
10541060 /* Correct timestamps with byte offset if demuxers only have timestamps
@@ -1156,7 +1162,8 @@ static void compute_pkt_fields(AVFormatContext *s, AVStream *st, · 101 unmodified lines
11561162 static int parse_packet(AVFormatContext *s, AVPacket *pkt,
11571163 int stream_index, int flush)
11581164 {
1159− FFFormatContext *const si = ffformatcontext(s);
1165+ FormatContextInternal *const fci = ff_fc_internal(s);
1166+ FFFormatContext *const si = &fci->fc;
11601167 AVPacket *out_pkt = si->parse_pkt;
11611168 AVStream *st = s->streams[stream_index];
11621169 FFStream *const sti = ffstream(st);
@@ -1249,7 +1256,7 @@ static int parse_packet(AVFormatContext *s, AVPacket *pkt, · 86 unmodified lines
12491256
12501257 compute_pkt_fields(s, st, sti->parser, out_pkt, next_dts, next_pts);
12511258
1252− ret = avpriv_packet_list_put(&si->parse_queue,
1259+ ret = avpriv_packet_list_put(&fci->parse_queue,
12531260 out_pkt, NULL, 0);
12541261 if (ret < 0)
12551262 goto fail;
@@ -1324,11 +1331,12 @@ static int extract_extradata(FFFormatContext *si, AVStream *st, const AVPacket * · 68 unmodified lines
13241331
13251332 static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
13261333 {
1327− FFFormatContext *const si = ffformatcontext(s);
1334+ FormatContextInternal *const fci = ff_fc_internal(s);
1335+ FFFormatContext *const si = &fci->fc;
13281336 int ret, got_packet = 0;
13291337 AVDictionary *metadata = NULL;
13301338
1331− while (!got_packet && !si->parse_queue.head) {
1339+ while (!got_packet && !fci->parse_queue.head) {
13321340 AVStream *st;
13331341 FFStream *sti;
13341342
@@ -1456,8 +1464,8 @@ static int read_frame_internal(AVFormatContext *s, AVPacket *pkt) · 121 unmodified lines
14561464 }
14571465 }
14581466
1459− if (!got_packet && si->parse_queue.head)
1460− ret = avpriv_packet_list_get(&si->parse_queue, pkt);
1467+ if (!got_packet && fci->parse_queue.head)
1468+ ret = avpriv_packet_list_get(&fci->parse_queue, pkt);
14611469
14621470 if (ret >= 0) {
14631471 AVStream *const st = s->streams[pkt->stream_index];
@@ -1508,7 +1516,7 @@ static int read_frame_internal(AVFormatContext *s, AVPacket *pkt) · 44 unmodified lines
15081516 #endif
15091517 }
15101518
1511− if (!si->metafree) {
1519+ if (!fci->metafree) {
15121520 int metaret = av_opt_get_dict_val(s, "metadata", AV_OPT_SEARCH_CHILDREN, &metadata);
15131521 if (metadata) {
15141522 s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
@@ -1516,7 +1524,7 @@ static int read_frame_internal(AVFormatContext *s, AVPacket *pkt) · 1 unmodified line
15161524 av_dict_free(&metadata);
15171525 av_opt_set_dict_val(s, "metadata", NULL, AV_OPT_SEARCH_CHILDREN);
15181526 }
1519− si->metafree = metaret == AVERROR_OPTION_NOT_FOUND;
1527+ fci->metafree = metaret == AVERROR_OPTION_NOT_FOUND;
15201528 }
15211529
15221530 if (s->debug & FF_FDEBUG_TS)
libavformat/demux_utils.c+7−6
@@ -25,6 +25,7 @@ · 24 unmodified lines
2525 #include "libavcodec/bytestream.h"
2626 #include "libavcodec/packet_internal.h"
2727 #include "avformat.h"
28+#include "avformat_internal.h"
2829 #include "avio_internal.h"
2930 #include "demux.h"
3031 #include "internal.h"
@@ -42,7 +43,7 @@ void avpriv_stream_set_need_parsing(AVStream *st, enum AVStreamParseType type) · 11 unmodified lines
4243 AVChapter *avpriv_new_chapter(AVFormatContext *s, int64_t id, AVRational time_base,
4344 int64_t start, int64_t end, const char *title)
4445 {
45− FFFormatContext *const si = ffformatcontext(s);
46+ FormatContextInternal *const fci = ff_fc_internal(s);
4647 AVChapter *chapter = NULL;
4748 int ret;
4849
@@ -52,13 +53,13 @@ AVChapter *avpriv_new_chapter(AVFormatContext *s, int64_t id, AVRational time_ba · 3 unmodified lines
5253 }
5354
5455 if (!s->nb_chapters) {
55− si->chapter_ids_monotonic = 1;
56− } else if (!si->chapter_ids_monotonic || s->chapters[s->nb_chapters-1]->id >= id) {
56+ fci->chapter_ids_monotonic = 1;
57+ } else if (!fci->chapter_ids_monotonic || s->chapters[s->nb_chapters-1]->id >= id) {
5758 for (unsigned i = 0; i < s->nb_chapters; i++)
5859 if (s->chapters[i]->id == id)
5960 chapter = s->chapters[i];
6061 if (!chapter)
61− si->chapter_ids_monotonic = 0;
62+ fci->chapter_ids_monotonic = 0;
6263 }
6364
6465 if (!chapter) {
@@ -92,7 +93,7 @@ void av_format_inject_global_side_data(AVFormatContext *s) · 27 unmodified lines
9293
9394 int avformat_queue_attached_pictures(AVFormatContext *s)
9495 {
95− FFFormatContext *const si = ffformatcontext(s);
96+ FormatContextInternal *const fci = ff_fc_internal(s);
9697 int ret;
9798 for (unsigned i = 0; i < s->nb_streams; i++)
9899 if (s->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC &&
@@ -104,7 +105,7 @@ int avformat_queue_attached_pictures(AVFormatContext *s) · 5 unmodified lines
104105 continue;
105106 }
106107
107− ret = avpriv_packet_list_put(&si->raw_packet_buffer,
108+ ret = avpriv_packet_list_put(&fci->raw_packet_buffer,
108109 &s->streams[i]->attached_pic,
109110 av_packet_ref, 0);
110111 if (ret < 0)
libavformat/internal.h+0−25
@@ -88,17 +88,6 @@ typedef struct FFFormatContext { · 87 unmodified lines
8888 /* av_seek_frame() support */
8989 int64_t data_offset; /**< offset of the first packet */
9090
91− /**
92− * Raw packets from the demuxer, prior to parsing and decoding.
93− * This buffer is used for buffering packets until the codec can
94− * be identified, as parsing cannot be done without knowing the
95− * codec.
96− */
97− PacketList raw_packet_buffer;
98− /**
99− * Packets split by the parser get queued here.
100− */
101− PacketList parse_queue;
10291 /**
10392 * The generic code uses this as a temporary packet
10493 * to parse packets or for muxing, especially flushing.
@@ -120,10 +109,6 @@ typedef struct FFFormatContext { · 15 unmodified lines
120109 * permanent ones).
121110 */
122111 AVPacket *pkt;
123− /**
124− * Sum of the size of packets in raw_packet_buffer, in bytes.
125− */
126− int raw_packet_buffer_size;
127112
128113 #if FF_API_AVSTREAM_SIDE_DATA
129114 int inject_global_side_data;
@@ -140,16 +125,6 @@ typedef struct FFFormatContext { · 10 unmodified lines
140125 * Prefer the codec framerate for avg_frame_rate computation.
141126 */
142127 int prefer_codec_framerate;
143−
144− /**
145− * Set if chapter ids are strictly monotonic.
146− */
147− int chapter_ids_monotonic;
148−
149− /**
150− * Contexts and child contexts do not contain a metadata option
151− */
152− int metafree;
153128 } FFFormatContext;
154129
155130 static av_always_inline FFFormatContext *ffformatcontext(AVFormatContext *s)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t