History

avformat: Rework add_to_pktbuf

Make it return an error and check its return value when it is used. Simplify the usage by calling `av_packet_ref` internally when needed.

Luca Barbato committed 11 years ago

Commit d5845331 parent ce70f281 file · +38 −32Browse files

Unified · Split

libavformat/utils.c+38−32
@@ -209,12 +209,23 @@ static int init_input(AVFormatContext *s, const char *filename, · 208 unmodified lines
209209 s, 0, s->probesize);
210210 }
211211
212−static AVPacket *add_to_pktbuf(AVPacketList **packet_buffer, AVPacket *pkt,
213− AVPacketList **plast_pktl)
212+static int add_to_pktbuf(AVPacketList **packet_buffer, AVPacket *pkt,
213+ AVPacketList **plast_pktl, int ref)
214214 {
215215 AVPacketList *pktl = av_mallocz(sizeof(AVPacketList));
216+ int ret;
217+
216218 if (!pktl)
217− return NULL;
219+ return AVERROR(ENOMEM);
220+
221+ if (ref) {
222+ if ((ret = av_packet_ref(&pktl->pkt, pkt)) < 0) {
223+ av_free(pktl);
224+ return ret;
225+ }
226+ } else {
227+ pktl->pkt = *pkt;
228+ }
218229
219230 if (*packet_buffer)
220231 (*plast_pktl)->next = pktl;
@@ -223,23 +234,21 @@ static AVPacket *add_to_pktbuf(AVPacketList **packet_buffer, AVPacket *pkt, · 2 unmodified lines
223234
224235 /* Add the packet in the buffered packet list. */
225236 *plast_pktl = pktl;
226− pktl->pkt = *pkt;
227− return &pktl->pkt;
237+ return 0;
228238 }
229239
230240 static int queue_attached_pictures(AVFormatContext *s)
231241 {
232− int i;
242+ int i, ret;
233243 for (i = 0; i < s->nb_streams; i++)
234244 if (s->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC &&
235245 s->streams[i]->discard < AVDISCARD_ALL) {
236− AVPacket copy = s->streams[i]->attached_pic;
237− copy.buf = av_buffer_ref(copy.buf);
238− if (!copy.buf)
239− return AVERROR(ENOMEM);
240246
241− add_to_pktbuf(&s->internal->raw_packet_buffer, &copy,
242− &s->internal->raw_packet_buffer_end);
247+ ret = add_to_pktbuf(&s->internal->raw_packet_buffer,
248+ &s->streams[i]->attached_pic,
249+ &s->internal->raw_packet_buffer_end, 1);
250+ if (ret < 0)
251+ return ret;
243252 }
244253 return 0;
245254 }
@@ -451,8 +460,10 @@ int ff_read_packet(AVFormatContext *s, AVPacket *pkt) · 205 unmodified lines
451460 !st->probe_packets))
452461 return ret;
453462
454− add_to_pktbuf(&s->internal->raw_packet_buffer, pkt,
455− &s->internal->raw_packet_buffer_end);
463+ err = add_to_pktbuf(&s->internal->raw_packet_buffer, pkt,
464+ &s->internal->raw_packet_buffer_end, 0);
465+ if (err)
466+ return err;
456467 s->internal->raw_packet_buffer_remaining_size -= pkt->size;
457468
458469 if ((err = probe_codec(s, st, pkt)) < 0)
@@ -837,16 +848,10 @@ static int parse_packet(AVFormatContext *s, AVPacket *pkt, int stream_index) · 378 unmodified lines
837848 0, 0, AVINDEX_KEYFRAME);
838849 }
839850
840− if (out_pkt.data == pkt->data && out_pkt.size == pkt->size) {
841− out_pkt.buf = pkt->buf;
842− pkt->buf = NULL;
843− }
844− if ((ret = av_dup_packet(&out_pkt)) < 0)
845− goto fail;
846−
847− if (!add_to_pktbuf(&s->internal->parse_queue, &out_pkt, &s->internal->parse_queue_end)) {
851+ if ((ret = add_to_pktbuf(&s->internal->parse_queue, &out_pkt,
852+ &s->internal->parse_queue_end,
853+ 1))) {
848854 av_packet_unref(&out_pkt);
849− ret = AVERROR(ENOMEM);
850855 goto fail;
851856 }
852857 }
@@ -1022,9 +1027,10 @@ int av_read_frame(AVFormatContext *s, AVPacket *pkt) · 169 unmodified lines
10221027 return ret;
10231028 }
10241029
1025− if (av_dup_packet(add_to_pktbuf(&s->internal->packet_buffer, pkt,
1026− &s->internal->packet_buffer_end)) < 0)
1027− return AVERROR(ENOMEM);
1030+ ret = add_to_pktbuf(&s->internal->packet_buffer, pkt,
1031+ &s->internal->packet_buffer_end, 1);
1032+ if (ret < 0)
1033+ return ret;
10281034 }
10291035 }
10301036
@@ -2181,12 +2187,12 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options) · 1150 unmodified lines
21812187 break;
21822188 }
21832189
2184− if (ic->flags & AVFMT_FLAG_NOBUFFER) {
2185− pkt = &pkt1;
2186− } else {
2187− pkt = add_to_pktbuf(&ic->internal->packet_buffer, &pkt1,
2188− &ic->internal->packet_buffer_end);
2189− if ((ret = av_dup_packet(pkt)) < 0)
2190+ pkt = &pkt1;
2191+
2192+ if (!(ic->flags & AVFMT_FLAG_NOBUFFER)) {
2193+ ret = add_to_pktbuf(&ic->internal->packet_buffer, pkt,
2194+ &ic->internal->packet_buffer_end, 0);
2195+ if (ret < 0)
21902196 goto find_stream_info_err;
21912197 }
21922198

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