FFmpeg
avpacket.c
Go to the documentation of this file.
1 /*
2  * AVPacket functions for libavcodec
3  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <string.h>
23 
24 #include "libavutil/avassert.h"
25 #include "libavutil/common.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/mathematics.h"
28 #include "libavutil/mem.h"
29 
30 #include "bytestream.h"
31 #include "internal.h"
32 #include "packet.h"
33 #include "packet_internal.h"
34 
35 #if FF_API_INIT_PACKET
37 {
38  pkt->pts = AV_NOPTS_VALUE;
39  pkt->dts = AV_NOPTS_VALUE;
40  pkt->pos = -1;
41  pkt->duration = 0;
42 #if FF_API_CONVERGENCE_DURATION
44  pkt->convergence_duration = 0;
46 #endif
47  pkt->flags = 0;
48  pkt->stream_index = 0;
49  pkt->buf = NULL;
50  pkt->side_data = NULL;
51  pkt->side_data_elems = 0;
52 }
53 #endif
54 
56 {
57  memset(pkt, 0, sizeof(*pkt));
58 
59  pkt->pts = AV_NOPTS_VALUE;
60  pkt->dts = AV_NOPTS_VALUE;
61  pkt->pos = -1;
62 }
63 
65 {
66  AVPacket *pkt = av_mallocz(sizeof(AVPacket));
67  if (!pkt)
68  return pkt;
69 
71 
72  return pkt;
73 }
74 
76 {
77  if (!pkt || !*pkt)
78  return;
79 
80  av_packet_unref(*pkt);
81  av_freep(pkt);
82 }
83 
84 static int packet_alloc(AVBufferRef **buf, int size)
85 {
86  int ret;
88  return AVERROR(EINVAL);
89 
91  if (ret < 0)
92  return ret;
93 
94  memset((*buf)->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
95 
96  return 0;
97 }
98 
100 {
101  AVBufferRef *buf = NULL;
102  int ret = packet_alloc(&buf, size);
103  if (ret < 0)
104  return ret;
105 
106  get_packet_defaults(pkt);
107  pkt->buf = buf;
108  pkt->data = buf->data;
109  pkt->size = size;
110 
111  return 0;
112 }
113 
115 {
116  if (pkt->size <= size)
117  return;
118  pkt->size = size;
119  memset(pkt->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
120 }
121 
122 int av_grow_packet(AVPacket *pkt, int grow_by)
123 {
124  int new_size;
125  av_assert0((unsigned)pkt->size <= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
126  if ((unsigned)grow_by >
127  INT_MAX - (pkt->size + AV_INPUT_BUFFER_PADDING_SIZE))
128  return AVERROR(ENOMEM);
129 
130  new_size = pkt->size + grow_by + AV_INPUT_BUFFER_PADDING_SIZE;
131  if (pkt->buf) {
132  size_t data_offset;
133  uint8_t *old_data = pkt->data;
134  if (pkt->data == NULL) {
135  data_offset = 0;
136  pkt->data = pkt->buf->data;
137  } else {
138  data_offset = pkt->data - pkt->buf->data;
139  if (data_offset > INT_MAX - new_size)
140  return AVERROR(ENOMEM);
141  }
142 
143  if (new_size + data_offset > pkt->buf->size ||
144  !av_buffer_is_writable(pkt->buf)) {
145  int ret = av_buffer_realloc(&pkt->buf, new_size + data_offset);
146  if (ret < 0) {
147  pkt->data = old_data;
148  return ret;
149  }
150  pkt->data = pkt->buf->data + data_offset;
151  }
152  } else {
153  pkt->buf = av_buffer_alloc(new_size);
154  if (!pkt->buf)
155  return AVERROR(ENOMEM);
156  if (pkt->size > 0)
157  memcpy(pkt->buf->data, pkt->data, pkt->size);
158  pkt->data = pkt->buf->data;
159  }
160  pkt->size += grow_by;
161  memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
162 
163  return 0;
164 }
165 
167 {
168  if (size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
169  return AVERROR(EINVAL);
170 
173  if (!pkt->buf)
174  return AVERROR(ENOMEM);
175 
176  pkt->data = data;
177  pkt->size = size;
178 
179  return 0;
180 }
181 
182 #if FF_API_AVPACKET_OLD_API
184 #define ALLOC_MALLOC(data, size) data = av_malloc(size)
185 #define ALLOC_BUF(data, size) \
186 do { \
187  av_buffer_realloc(&pkt->buf, size); \
188  data = pkt->buf ? pkt->buf->data : NULL; \
189 } while (0)
190 
191 #define DUP_DATA(dst, src, size, padding, ALLOC) \
192  do { \
193  void *data; \
194  if (padding) { \
195  if ((unsigned)(size) > \
196  (unsigned)(size) + AV_INPUT_BUFFER_PADDING_SIZE) \
197  goto failed_alloc; \
198  ALLOC(data, size + AV_INPUT_BUFFER_PADDING_SIZE); \
199  } else { \
200  ALLOC(data, size); \
201  } \
202  if (!data) \
203  goto failed_alloc; \
204  memcpy(data, src, size); \
205  if (padding) \
206  memset((uint8_t *)data + size, 0, \
207  AV_INPUT_BUFFER_PADDING_SIZE); \
208  dst = data; \
209  } while (0)
210 
211 /* Makes duplicates of data, side_data, but does not copy any other fields */
212 static int copy_packet_data(AVPacket *pkt, const AVPacket *src, int dup)
213 {
214  pkt->data = NULL;
215  pkt->side_data = NULL;
216  pkt->side_data_elems = 0;
217  if (pkt->buf) {
218  AVBufferRef *ref = av_buffer_ref(src->buf);
219  if (!ref)
220  return AVERROR(ENOMEM);
221  pkt->buf = ref;
222  pkt->data = ref->data;
223  } else {
224  DUP_DATA(pkt->data, src->data, pkt->size, 1, ALLOC_BUF);
225  }
226  if (src->side_data_elems && dup) {
227  pkt->side_data = src->side_data;
228  pkt->side_data_elems = src->side_data_elems;
229  }
230  if (src->side_data_elems && !dup) {
231  return av_copy_packet_side_data(pkt, src);
232  }
233  return 0;
234 
235 failed_alloc:
236  av_packet_unref(pkt);
237  return AVERROR(ENOMEM);
238 }
239 
241 {
242  if (src->side_data_elems) {
243  int i;
244  DUP_DATA(pkt->side_data, src->side_data,
245  src->side_data_elems * sizeof(*src->side_data), 0, ALLOC_MALLOC);
246  if (src != pkt) {
247  memset(pkt->side_data, 0,
248  src->side_data_elems * sizeof(*src->side_data));
249  }
250  for (i = 0; i < src->side_data_elems; i++) {
251  DUP_DATA(pkt->side_data[i].data, src->side_data[i].data,
252  src->side_data[i].size, 1, ALLOC_MALLOC);
253  pkt->side_data[i].size = src->side_data[i].size;
254  pkt->side_data[i].type = src->side_data[i].type;
255  }
256  }
257  pkt->side_data_elems = src->side_data_elems;
258  return 0;
259 
260 failed_alloc:
261  av_packet_unref(pkt);
262  return AVERROR(ENOMEM);
263 }
264 
266 {
267  AVPacket tmp_pkt;
268 
269  if (!pkt->buf && pkt->data) {
270  tmp_pkt = *pkt;
271  return copy_packet_data(pkt, &tmp_pkt, 1);
272  }
273  return 0;
274 }
275 
277 {
278  *dst = *src;
279  return copy_packet_data(dst, src, 0);
280 }
282 #endif
283 
285 {
286  int i;
287  for (i = 0; i < pkt->side_data_elems; i++)
288  av_freep(&pkt->side_data[i].data);
289  av_freep(&pkt->side_data);
290  pkt->side_data_elems = 0;
291 }
292 
293 #if FF_API_AVPACKET_OLD_API
296 {
297  if (pkt) {
298  if (pkt->buf)
299  av_buffer_unref(&pkt->buf);
300  pkt->data = NULL;
301  pkt->size = 0;
302 
304  }
305 }
307 #endif
308 
310  uint8_t *data, size_t size)
311 {
313  int i, elems = pkt->side_data_elems;
314 
315  for (i = 0; i < elems; i++) {
316  AVPacketSideData *sd = &pkt->side_data[i];
317 
318  if (sd->type == type) {
319  av_free(sd->data);
320  sd->data = data;
321  sd->size = size;
322  return 0;
323  }
324  }
325 
326  if ((unsigned)elems + 1 > AV_PKT_DATA_NB)
327  return AVERROR(ERANGE);
328 
329  tmp = av_realloc(pkt->side_data, (elems + 1) * sizeof(*tmp));
330  if (!tmp)
331  return AVERROR(ENOMEM);
332 
333  pkt->side_data = tmp;
334  pkt->side_data[elems].data = data;
335  pkt->side_data[elems].size = size;
336  pkt->side_data[elems].type = type;
337  pkt->side_data_elems++;
338 
339  return 0;
340 }
341 
342 
345 {
346  int ret;
347  uint8_t *data;
348 
349 #if FF_API_BUFFER_SIZE_T
350  if ((unsigned)size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
351 #else
352  if (size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
353 #endif
354  return NULL;
356  if (!data)
357  return NULL;
358 
359  ret = av_packet_add_side_data(pkt, type, data, size);
360  if (ret < 0) {
361  av_freep(&data);
362  return NULL;
363  }
364 
365  return data;
366 }
367 
370 {
371  int i;
372 
373  for (i = 0; i < pkt->side_data_elems; i++) {
374  if (pkt->side_data[i].type == type) {
375  if (size)
376  *size = pkt->side_data[i].size;
377  return pkt->side_data[i].data;
378  }
379  }
380  if (size)
381  *size = 0;
382  return NULL;
383 }
384 
386 {
387  switch(type) {
388  case AV_PKT_DATA_PALETTE: return "Palette";
389  case AV_PKT_DATA_NEW_EXTRADATA: return "New Extradata";
390  case AV_PKT_DATA_PARAM_CHANGE: return "Param Change";
391  case AV_PKT_DATA_H263_MB_INFO: return "H263 MB Info";
392  case AV_PKT_DATA_REPLAYGAIN: return "Replay Gain";
393  case AV_PKT_DATA_DISPLAYMATRIX: return "Display Matrix";
394  case AV_PKT_DATA_STEREO3D: return "Stereo 3D";
395  case AV_PKT_DATA_AUDIO_SERVICE_TYPE: return "Audio Service Type";
396  case AV_PKT_DATA_QUALITY_STATS: return "Quality stats";
397  case AV_PKT_DATA_FALLBACK_TRACK: return "Fallback track";
398  case AV_PKT_DATA_CPB_PROPERTIES: return "CPB properties";
399  case AV_PKT_DATA_SKIP_SAMPLES: return "Skip Samples";
400  case AV_PKT_DATA_JP_DUALMONO: return "JP Dual Mono";
401  case AV_PKT_DATA_STRINGS_METADATA: return "Strings Metadata";
402  case AV_PKT_DATA_SUBTITLE_POSITION: return "Subtitle Position";
403  case AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL: return "Matroska BlockAdditional";
404  case AV_PKT_DATA_WEBVTT_IDENTIFIER: return "WebVTT ID";
405  case AV_PKT_DATA_WEBVTT_SETTINGS: return "WebVTT Settings";
406  case AV_PKT_DATA_METADATA_UPDATE: return "Metadata Update";
407  case AV_PKT_DATA_MPEGTS_STREAM_ID: return "MPEGTS Stream ID";
408  case AV_PKT_DATA_MASTERING_DISPLAY_METADATA: return "Mastering display metadata";
409  case AV_PKT_DATA_CONTENT_LIGHT_LEVEL: return "Content light level metadata";
410  case AV_PKT_DATA_SPHERICAL: return "Spherical Mapping";
411  case AV_PKT_DATA_A53_CC: return "A53 Closed Captions";
412  case AV_PKT_DATA_ENCRYPTION_INIT_INFO: return "Encryption initialization data";
413  case AV_PKT_DATA_ENCRYPTION_INFO: return "Encryption info";
414  case AV_PKT_DATA_AFD: return "Active Format Description data";
415  case AV_PKT_DATA_PRFT: return "Producer Reference Time";
416  case AV_PKT_DATA_ICC_PROFILE: return "ICC Profile";
417  case AV_PKT_DATA_DOVI_CONF: return "DOVI configuration record";
418  case AV_PKT_DATA_S12M_TIMECODE: return "SMPTE ST 12-1:2014 timecode";
419  }
420  return NULL;
421 }
422 
423 #if FF_API_MERGE_SD_API
424 
425 #define FF_MERGE_MARKER 0x8c4d9d108e25e9feULL
426 
428  if(pkt->side_data_elems){
429  AVBufferRef *buf;
430  int i;
431  uint8_t *p;
432  uint64_t size= pkt->size + 8LL + AV_INPUT_BUFFER_PADDING_SIZE;
433  AVPacket old= *pkt;
434  for (i=0; i<old.side_data_elems; i++) {
435  size += old.side_data[i].size + 5LL;
436  }
437  if (size > INT_MAX)
438  return AVERROR(EINVAL);
439  buf = av_buffer_alloc(size);
440  if (!buf)
441  return AVERROR(ENOMEM);
442  pkt->buf = buf;
443  pkt->data = p = buf->data;
444  pkt->size = size - AV_INPUT_BUFFER_PADDING_SIZE;
445  bytestream_put_buffer(&p, old.data, old.size);
446  for (i=old.side_data_elems-1; i>=0; i--) {
447  bytestream_put_buffer(&p, old.side_data[i].data, old.side_data[i].size);
448  bytestream_put_be32(&p, old.side_data[i].size);
449  *p++ = old.side_data[i].type | ((i==old.side_data_elems-1)*128);
450  }
451  bytestream_put_be64(&p, FF_MERGE_MARKER);
452  av_assert0(p-pkt->data == pkt->size);
453  memset(p, 0, AV_INPUT_BUFFER_PADDING_SIZE);
454  av_packet_unref(&old);
455  pkt->side_data_elems = 0;
456  pkt->side_data = NULL;
457  return 1;
458  }
459  return 0;
460 }
461 
463  if (!pkt->side_data_elems && pkt->size >12 && AV_RB64(pkt->data + pkt->size - 8) == FF_MERGE_MARKER){
464  int i;
465  unsigned int size;
466  uint8_t *p;
467 
468  p = pkt->data + pkt->size - 8 - 5;
469  for (i=1; ; i++){
470  size = AV_RB32(p);
471  if (size>INT_MAX - 5 || p - pkt->data < size)
472  return 0;
473  if (p[4]&128)
474  break;
475  if (p - pkt->data < size + 5)
476  return 0;
477  p-= size+5;
478  }
479 
480  if (i > AV_PKT_DATA_NB)
481  return AVERROR(ERANGE);
482 
483  pkt->side_data = av_malloc_array(i, sizeof(*pkt->side_data));
484  if (!pkt->side_data)
485  return AVERROR(ENOMEM);
486 
487  p= pkt->data + pkt->size - 8 - 5;
488  for (i=0; ; i++){
489  size= AV_RB32(p);
492  pkt->side_data[i].size = size;
493  pkt->side_data[i].type = p[4]&127;
494  if (!pkt->side_data[i].data)
495  return AVERROR(ENOMEM);
496  memcpy(pkt->side_data[i].data, p-size, size);
497  pkt->size -= size + 5;
498  if(p[4]&128)
499  break;
500  p-= size+5;
501  }
502  pkt->size -= 8;
503  pkt->side_data_elems = i+1;
504  return 1;
505  }
506  return 0;
507 }
508 #endif
509 
510 #if FF_API_BUFFER_SIZE_T
512 #else
514 #endif
515 {
516  uint8_t *data = NULL;
517  *size = 0;
518 
519  if (!dict)
520  return NULL;
521 
522  for (int pass = 0; pass < 2; pass++) {
523  const AVDictionaryEntry *t = NULL;
524  size_t total_length = 0;
525 
526  while ((t = av_dict_get(dict, "", t, AV_DICT_IGNORE_SUFFIX))) {
527  for (int i = 0; i < 2; i++) {
528  const char *str = i ? t->value : t->key;
529  const size_t len = strlen(str) + 1;
530 
531  if (pass)
532  memcpy(data + total_length, str, len);
533 #if FF_API_BUFFER_SIZE_T
534  else if (len > INT_MAX - total_length)
535 #else
536  else if (len > SIZE_MAX - total_length)
537 #endif
538  return NULL;
539  total_length += len;
540  }
541  }
542  if (pass)
543  break;
544  data = av_malloc(total_length);
545  if (!data)
546  return NULL;
547  *size = total_length;
548  }
549 
550  return data;
551 }
552 
553 #if FF_API_BUFFER_SIZE_T
554 int av_packet_unpack_dictionary(const uint8_t *data, int size, AVDictionary **dict)
555 #else
556 int av_packet_unpack_dictionary(const uint8_t *data, size_t size,
557  AVDictionary **dict)
558 #endif
559 {
560  const uint8_t *end;
561  int ret;
562 
563  if (!dict || !data || !size)
564  return 0;
565  end = data + size;
566  if (size && end[-1])
567  return AVERROR_INVALIDDATA;
568  while (data < end) {
569  const uint8_t *key = data;
570  const uint8_t *val = data + strlen(key) + 1;
571 
572  if (val >= end || !*key)
573  return AVERROR_INVALIDDATA;
574 
575  ret = av_dict_set(dict, key, val, 0);
576  if (ret < 0)
577  return ret;
578  data = val + strlen(val) + 1;
579  }
580 
581  return 0;
582 }
583 
585  buffer_size_t size)
586 {
587  int i;
588 
589  for (i = 0; i < pkt->side_data_elems; i++) {
590  if (pkt->side_data[i].type == type) {
591  if (size > pkt->side_data[i].size)
592  return AVERROR(ENOMEM);
593  pkt->side_data[i].size = size;
594  return 0;
595  }
596  }
597  return AVERROR(ENOENT);
598 }
599 
601 {
602  int i;
603 
604  dst->pts = src->pts;
605  dst->dts = src->dts;
606  dst->pos = src->pos;
607  dst->duration = src->duration;
608 #if FF_API_CONVERGENCE_DURATION
610  dst->convergence_duration = src->convergence_duration;
612 #endif
613  dst->flags = src->flags;
614  dst->stream_index = src->stream_index;
615 
616  dst->side_data = NULL;
617  dst->side_data_elems = 0;
618  for (i = 0; i < src->side_data_elems; i++) {
620  buffer_size_t size = src->side_data[i].size;
621  uint8_t *src_data = src->side_data[i].data;
622  uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
623 
624  if (!dst_data) {
626  return AVERROR(ENOMEM);
627  }
628  memcpy(dst_data, src_data, size);
629  }
630 
631  return 0;
632 }
633 
635 {
637  av_buffer_unref(&pkt->buf);
638  get_packet_defaults(pkt);
639 }
640 
642 {
643  int ret;
644 
645  dst->buf = NULL;
646 
647  ret = av_packet_copy_props(dst, src);
648  if (ret < 0)
649  goto fail;
650 
651  if (!src->buf) {
652  ret = packet_alloc(&dst->buf, src->size);
653  if (ret < 0)
654  goto fail;
655  av_assert1(!src->size || src->data);
656  if (src->size)
657  memcpy(dst->buf->data, src->data, src->size);
658 
659  dst->data = dst->buf->data;
660  } else {
661  dst->buf = av_buffer_ref(src->buf);
662  if (!dst->buf) {
663  ret = AVERROR(ENOMEM);
664  goto fail;
665  }
666  dst->data = src->data;
667  }
668 
669  dst->size = src->size;
670 
671  return 0;
672 fail:
673  av_packet_unref(dst);
674  return ret;
675 }
676 
678 {
680 
681  if (!ret)
682  return ret;
683 
684  if (av_packet_ref(ret, src))
685  av_packet_free(&ret);
686 
687  return ret;
688 }
689 
691 {
692  *dst = *src;
693  get_packet_defaults(src);
694 }
695 
697 {
698  int ret;
699 
700  if (pkt->buf)
701  return 0;
702 
703  ret = packet_alloc(&pkt->buf, pkt->size);
704  if (ret < 0)
705  return ret;
706  av_assert1(!pkt->size || pkt->data);
707  if (pkt->size)
708  memcpy(pkt->buf->data, pkt->data, pkt->size);
709 
710  pkt->data = pkt->buf->data;
711 
712  return 0;
713 }
714 
716 {
717  AVBufferRef *buf = NULL;
718  int ret;
719 
720  if (pkt->buf && av_buffer_is_writable(pkt->buf))
721  return 0;
722 
723  ret = packet_alloc(&buf, pkt->size);
724  if (ret < 0)
725  return ret;
726  av_assert1(!pkt->size || pkt->data);
727  if (pkt->size)
728  memcpy(buf->data, pkt->data, pkt->size);
729 
730  av_buffer_unref(&pkt->buf);
731  pkt->buf = buf;
732  pkt->data = buf->data;
733 
734  return 0;
735 }
736 
738 {
739  if (pkt->pts != AV_NOPTS_VALUE)
740  pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
741  if (pkt->dts != AV_NOPTS_VALUE)
742  pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
743  if (pkt->duration > 0)
744  pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
745 #if FF_API_CONVERGENCE_DURATION
747  if (pkt->convergence_duration > 0)
748  pkt->convergence_duration = av_rescale_q(pkt->convergence_duration, src_tb, dst_tb);
750 #endif
751 }
752 
753 int avpriv_packet_list_put(PacketList **packet_buffer,
754  PacketList **plast_pktl,
755  AVPacket *pkt,
756  int (*copy)(AVPacket *dst, const AVPacket *src),
757  int flags)
758 {
759  PacketList *pktl = av_mallocz(sizeof(PacketList));
760  int ret;
761 
762  if (!pktl)
763  return AVERROR(ENOMEM);
764 
765  if (copy) {
766  ret = copy(&pktl->pkt, pkt);
767  if (ret < 0) {
768  av_free(pktl);
769  return ret;
770  }
771  } else {
772  ret = av_packet_make_refcounted(pkt);
773  if (ret < 0) {
774  av_free(pktl);
775  return ret;
776  }
777  av_packet_move_ref(&pktl->pkt, pkt);
778  }
779 
780  if (*packet_buffer)
781  (*plast_pktl)->next = pktl;
782  else
783  *packet_buffer = pktl;
784 
785  /* Add the packet in the buffered packet list. */
786  *plast_pktl = pktl;
787  return 0;
788 }
789 
791  PacketList **pkt_buffer_end,
792  AVPacket *pkt)
793 {
794  PacketList *pktl;
795  if (!*pkt_buffer)
796  return AVERROR(EAGAIN);
797  pktl = *pkt_buffer;
798  *pkt = pktl->pkt;
799  *pkt_buffer = pktl->next;
800  if (!pktl->next)
801  *pkt_buffer_end = NULL;
802  av_freep(&pktl);
803  return 0;
804 }
805 
806 void avpriv_packet_list_free(PacketList **pkt_buf, PacketList **pkt_buf_end)
807 {
808  PacketList *tmp = *pkt_buf;
809 
810  while (tmp) {
811  PacketList *pktl = tmp;
812  tmp = pktl->next;
813  av_packet_unref(&pktl->pkt);
814  av_freep(&pktl);
815  }
816  *pkt_buf = NULL;
817  *pkt_buf_end = NULL;
818 }
819 
820 int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
821 {
822  uint8_t *side_data;
823  buffer_size_t side_data_size;
824  int i;
825 
826  side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, &side_data_size);
827  if (!side_data) {
828  side_data_size = 4+4+8*error_count;
830  side_data_size);
831  }
832 
833  if (!side_data || side_data_size < 4+4+8*error_count)
834  return AVERROR(ENOMEM);
835 
836  AV_WL32(side_data , quality );
837  side_data[4] = pict_type;
838  side_data[5] = error_count;
839  for (i = 0; i<error_count; i++)
840  AV_WL64(side_data+8 + 8*i , error[i]);
841 
842  return 0;
843 }
844 
845 int ff_side_data_set_prft(AVPacket *pkt, int64_t timestamp)
846 {
848  uint8_t *side_data;
849  buffer_size_t side_data_size;
850 
851  side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_PRFT, &side_data_size);
852  if (!side_data) {
853  side_data_size = sizeof(AVProducerReferenceTime);
854  side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_PRFT, side_data_size);
855  }
856 
857  if (!side_data || side_data_size < sizeof(AVProducerReferenceTime))
858  return AVERROR(ENOMEM);
859 
860  prft = (AVProducerReferenceTime *)side_data;
861  prft->wallclock = timestamp;
862  prft->flags = 0;
863 
864  return 0;
865 }
#define NULL
Definition: coverity.c:32
#define DUP_DATA(dst, src, size, padding, ALLOC)
Definition: avpacket.c:191
Producer Reference Time data corresponding to the AVProducerReferenceTime struct, usually exported by...
Definition: packet.h:268
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
AVPacketSideDataType
Definition: packet.h:40
static void copy(const float *p1, float *p2, const int length)
A list of zero terminated key/value strings.
Definition: packet.h:209
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:125
The optional first identifier line of a WebVTT cue.
Definition: packet.h:196
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:134
int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
Definition: avpacket.c:820
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
Memory handling functions.
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:389
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:114
Subtitle event position.
Definition: packet.h:183
#define ALLOC_MALLOC(data, size)
Definition: avpacket.c:184
This side data should be associated with a video stream and contains Stereoscopic 3D information in f...
Definition: packet.h:114
ATSC A53 Part 4 Closed Captions.
Definition: packet.h:242
int size
Definition: packet.h:370
This side data contains an integer value representing the stream index of a "fallback" track...
Definition: packet.h:140
#define ALLOC_BUF(data, size)
Definition: avpacket.c:185
GLint GLenum type
Definition: opengl_enc.c:104
const char * key
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
int avpriv_packet_list_get(PacketList **pkt_buffer, PacketList **pkt_buffer_end, AVPacket *pkt)
Remove the oldest AVPacket in the list and return it.
Definition: avpacket.c:790
AVPacket * pkt
Definition: movenc.c:59
static void error(const char *err)
size_t size
Definition: packet.h:311
An AV_PKT_DATA_H263_MB_INFO side data packet contains a number of structures with info about macroblo...
Definition: packet.h:93
Mastering display metadata (based on SMPTE-2086:2014).
Definition: packet.h:222
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, buffer_size_t size)
Definition: avpacket.c:343
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:75
uint8_t
#define av_malloc(s)
int av_packet_split_side_data(AVPacket *pkt)
Definition: avpacket.c:462
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:387
This side data contains a 3x3 transformation matrix describing an affine transformation that needs to...
Definition: packet.h:108
int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
Initialize a reference-counted packet from av_malloc()ed data.
Definition: avpacket.c:166
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:91
int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type, buffer_size_t size)
Definition: avpacket.c:584
int av_packet_merge_side_data(AVPacket *pkt)
Definition: avpacket.c:427
int64_t wallclock
A UTC timestamp, in microseconds, since Unix epoch (e.g, av_gettime()).
Definition: avcodec.h:507
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
uint8_t * data
Definition: packet.h:369
This side data is encryption initialization data.
Definition: packet.h:249
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition: avpacket.c:690
void avpriv_packet_list_free(PacketList **pkt_buf, PacketList **pkt_buf_end)
Wipe the list and unref all the packets in it.
Definition: avpacket.c:806
uint8_t * data
Definition: packet.h:307
ptrdiff_t size
Definition: opengl_enc.c:100
struct PacketList * next
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: avpacket.c:641
int av_packet_make_writable(AVPacket *pkt)
Create a writable reference for the data described by a given packet, avoiding data copy if possible...
Definition: avpacket.c:715
void av_buffer_default_free(void *opaque, uint8_t *data)
Default free callback, which calls av_free() on the buffer data.
Definition: buffer.c:62
MPEGTS stream ID as uint8_t, this is required to pass the stream ID information from the demuxer to t...
Definition: packet.h:215
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
#define src
Definition: vp8dsp.c:255
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:99
#define AV_WL64(p, v)
Definition: intreadwrite.h:440
DOVI configuration ref: dolby-vision-bitstreams-within-the-iso-base-media-file-format-v2.1.2, section 2.2 dolby-vision-bitstreams-in-mpeg-2-transport-stream-multiplex-v1.2, section 3.3 Tags are stored in struct AVDOVIDecoderConfigurationRecord.
Definition: packet.h:283
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette...
Definition: packet.h:46
AVPacket * av_packet_clone(const AVPacket *src)
Create a new packet that references the same data as src.
Definition: avpacket.c:677
void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
Convert valid timing fields (timestamps / durations) in a packet from one timebase to another...
Definition: avpacket.c:737
int buffer_size_t
Definition: internal.h:306
An AV_PKT_DATA_PARAM_CHANGE side data packet is laid out as follows:
Definition: packet.h:72
FF_ENABLE_DEPRECATION_WARNINGS void av_packet_free_side_data(AVPacket *pkt)
Convenience function to free all the side data stored.
Definition: avpacket.c:284
ICC profile data consisting of an opaque octet buffer following the format described by ISO 15076-1...
Definition: packet.h:274
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: packet.h:352
simple assert() macros that are a bit more flexible than ISO C assert().
enum AVPacketSideDataType type
Definition: packet.h:313
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, buffer_size_t *size)
Definition: avpacket.c:368
int side_data_elems
Definition: packet.h:381
AVBufferRef * av_buffer_create(uint8_t *data, buffer_size_t size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:29
int av_buffer_realloc(AVBufferRef **pbuf, buffer_size_t size)
Reallocate a given buffer.
Definition: buffer.c:169
#define fail()
Definition: checkasm.h:133
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
#define pass
Definition: fft_template.c:603
int av_copy_packet_side_data(AVPacket *pkt, const AVPacket *src)
Definition: avpacket.c:240
common internal API header
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
Definition: avpacket.c:600
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
This side data contains quality related information from the encoder.
Definition: packet.h:132
static int packet_alloc(AVBufferRef **buf, int size)
Definition: avpacket.c:84
int av_packet_make_refcounted(AVPacket *pkt)
Ensure the data described by a given packet is reference counted.
Definition: avpacket.c:696
This side data should be associated with a video stream and corresponds to the AVSphericalMapping str...
Definition: packet.h:228
Active Format Description data consisting of a single byte as specified in ETSI TS 101 154 using AVAc...
Definition: packet.h:261
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about quality
int av_buffer_is_writable(const AVBufferRef *buf)
Definition: buffer.c:133
AVPacket pkt
int av_packet_unpack_dictionary(const uint8_t *data, size_t size, AVDictionary **dict)
Unpack a dictionary from side_data.
Definition: avpacket.c:556
Content light level (based on CTA-861.3).
Definition: packet.h:235
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: packet.h:55
A list of zero terminated key/value strings.
Definition: packet.h:172
AVBufferRef * av_buffer_alloc(buffer_size_t size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:67
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:634
uint8_t * data
The data buffer.
Definition: buffer.h:92
Data found in BlockAdditional element of matroska container.
Definition: packet.h:191
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
Rational number (pair of numerator and denominator).
Definition: rational.h:58
Recommmends skipping the specified number of samples.
Definition: packet.h:156
FF_ENABLE_DEPRECATION_WARNINGS int av_packet_add_side_data(AVPacket *pkt, enum AVPacketSideDataType type, uint8_t *data, size_t size)
Wrap an existing array as a packet side data.
Definition: avpacket.c:309
FF_DISABLE_DEPRECATION_WARNINGS void av_free_packet(AVPacket *pkt)
Definition: avpacket.c:295
This side data contains encryption info for how to decrypt the packet.
Definition: packet.h:255
static int copy_packet_data(AVPacket *pkt, const AVPacket *src, int dup)
Definition: avpacket.c:212
int size
Size of data in bytes.
Definition: buffer.h:97
int av_copy_packet(AVPacket *dst, const AVPacket *src)
Definition: avpacket.c:276
#define flags(name, subs,...)
Definition: cbs_av1.c:561
This side data should be associated with an audio stream and contains ReplayGain information in form ...
Definition: packet.h:99
#define FF_MERGE_MARKER
Definition: avpacket.c:425
Timecode which conforms to SMPTE ST 12-1:2014.
Definition: packet.h:291
void av_init_packet(AVPacket *pkt)
Definition: avpacket.c:36
A reference to a data buffer.
Definition: buffer.h:84
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_RB64
Definition: bytestream.h:91
An AV_PKT_DATA_JP_DUALMONO side data packet indicates that the packet may contain "dual mono" audio s...
Definition: packet.h:166
AVPacketSideData * side_data
Additional packet data that can be provided by the container.
Definition: packet.h:380
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:83
common internal api header.
common internal and external API header
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:93
int av_grow_packet(AVPacket *pkt, int grow_by)
Increase packet size, correctly zeroing padding.
Definition: avpacket.c:122
char * key
Definition: dict.h:86
int avpriv_packet_list_put(PacketList **packet_buffer, PacketList **plast_pktl, AVPacket *pkt, int(*copy)(AVPacket *dst, const AVPacket *src), int flags)
Append an AVPacket to the list.
Definition: avpacket.c:753
The optional settings (rendering instructions) that immediately follow the timestamp specifier of a W...
Definition: packet.h:202
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:372
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:215
This structure supplies correlation between a packet timestamp and a wall clock production time...
Definition: avcodec.h:503
#define av_free(p)
char * value
Definition: dict.h:87
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
int ff_side_data_set_prft(AVPacket *pkt, int64_t timestamp)
Definition: avpacket.c:845
int len
uint8_t * av_packet_pack_dictionary(AVDictionary *dict, size_t *size)
Pack a dictionary for use in side_data.
Definition: avpacket.c:513
int av_dup_packet(AVPacket *pkt)
Definition: avpacket.c:265
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:64
This side data corresponds to the AVCPBProperties struct.
Definition: packet.h:145
static void get_packet_defaults(AVPacket *pkt)
Definition: avpacket.c:55
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: packet.h:368
const char * av_packet_side_data_name(enum AVPacketSideDataType type)
Definition: avpacket.c:385
#define av_freep(p)
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key, ignoring the suffix of the found key string.
Definition: dict.h:70
#define av_malloc_array(a, b)
int stream_index
Definition: packet.h:371
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
The number of side data types.
Definition: packet.h:301
static double val(void *priv, double ch)
Definition: aeval.c:76
This structure stores compressed data.
Definition: packet.h:346
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:362
int i
Definition: input.c:407
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
This side data should be associated with an audio stream and corresponds to enum AVAudioServiceType.
Definition: packet.h:120
static uint8_t tmp[11]
Definition: aes_ctr.c:27