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.
Unified · Split
| @@ -209,12 +209,23 @@ static int init_input(AVFormatContext *s, const char *filename, · 208 unmodified lines | ||
| 209 | 209 | s, 0, s->probesize); |
| 210 | 210 | } |
| 211 | 211 | |
| 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) | |
| 214 | 214 | { |
| 215 | 215 | AVPacketList *pktl = av_mallocz(sizeof(AVPacketList)); |
| 216 | + int ret; | |
| 217 | + | |
| 216 | 218 | 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 | + } | |
| 218 | 229 | |
| 219 | 230 | if (*packet_buffer) |
| 220 | 231 | (*plast_pktl)->next = pktl; |
| @@ -223,23 +234,21 @@ static AVPacket *add_to_pktbuf(AVPacketList **packet_buffer, AVPacket *pkt, · 2 unmodified lines | ||
| 223 | 234 | |
| 224 | 235 | /* Add the packet in the buffered packet list. */ |
| 225 | 236 | *plast_pktl = pktl; |
| 226 | − pktl->pkt = *pkt; | |
| 227 | − return &pktl->pkt; | |
| 237 | + return 0; | |
| 228 | 238 | } |
| 229 | 239 | |
| 230 | 240 | static int queue_attached_pictures(AVFormatContext *s) |
| 231 | 241 | { |
| 232 | − int i; | |
| 242 | + int i, ret; | |
| 233 | 243 | for (i = 0; i < s->nb_streams; i++) |
| 234 | 244 | if (s->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC && |
| 235 | 245 | 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); | |
| 240 | 246 | |
| 241 | − add_to_pktbuf(&s->internal->raw_packet_buffer, ©, | |
| 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; | |
| 243 | 252 | } |
| 244 | 253 | return 0; |
| 245 | 254 | } |
| @@ -451,8 +460,10 @@ int ff_read_packet(AVFormatContext *s, AVPacket *pkt) · 205 unmodified lines | ||
| 451 | 460 | !st->probe_packets)) |
| 452 | 461 | return ret; |
| 453 | 462 | |
| 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; | |
| 456 | 467 | s->internal->raw_packet_buffer_remaining_size -= pkt->size; |
| 457 | 468 | |
| 458 | 469 | 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 | ||
| 837 | 848 | 0, 0, AVINDEX_KEYFRAME); |
| 838 | 849 | } |
| 839 | 850 | |
| 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))) { | |
| 848 | 854 | av_packet_unref(&out_pkt); |
| 849 | − ret = AVERROR(ENOMEM); | |
| 850 | 855 | goto fail; |
| 851 | 856 | } |
| 852 | 857 | } |
| @@ -1022,9 +1027,10 @@ int av_read_frame(AVFormatContext *s, AVPacket *pkt) · 169 unmodified lines | ||
| 1022 | 1027 | return ret; |
| 1023 | 1028 | } |
| 1024 | 1029 | |
| 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; | |
| 1028 | 1034 | } |
| 1029 | 1035 | } |
| 1030 | 1036 | |
| @@ -2181,12 +2187,12 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options) · 1150 unmodified lines | ||
| 2181 | 2187 | break; |
| 2182 | 2188 | } |
| 2183 | 2189 | |
| 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) | |
| 2190 | 2196 | goto find_stream_info_err; |
| 2191 | 2197 | } |
| 2192 | 2198 | |