FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
frame.c
Go to the documentation of this file.
1 /*
2  *
3  * This file is part of FFmpeg.
4  *
5  * FFmpeg is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * FFmpeg is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with FFmpeg; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #include "channel_layout.h"
21 #include "avassert.h"
22 #include "buffer.h"
23 #include "common.h"
24 #include "dict.h"
25 #include "frame.h"
26 #include "imgutils.h"
27 #include "mem.h"
28 #include "samplefmt.h"
29 
30 MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
31 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
32 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
33 MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
34 MAKE_ACCESSORS(AVFrame, frame, int, channels)
35 MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
36 MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
37 MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
38 MAKE_ACCESSORS(AVFrame, frame, int, pkt_size)
39 MAKE_ACCESSORS(AVFrame, frame, enum AVColorSpace, colorspace)
40 MAKE_ACCESSORS(AVFrame, frame, enum AVColorRange, color_range)
41 
42 #define CHECK_CHANNELS_CONSISTENCY(frame) \
43  av_assert2(!(frame)->channel_layout || \
44  (frame)->channels == \
45  av_get_channel_layout_nb_channels((frame)->channel_layout))
46 
47 AVDictionary **avpriv_frame_get_metadatap(AVFrame *frame) {return &frame->metadata;};
48 
49 int av_frame_set_qp_table(AVFrame *f, AVBufferRef *buf, int stride, int qp_type)
50 {
52 
53  f->qp_table_buf = buf;
54 
55  f->qscale_table = buf->data;
56  f->qstride = stride;
57  f->qscale_type = qp_type;
58 
59  return 0;
60 }
61 
62 int8_t *av_frame_get_qp_table(AVFrame *f, int *stride, int *type)
63 {
64  *stride = f->qstride;
65  *type = f->qscale_type;
66 
67  if (!f->qp_table_buf)
68  return NULL;
69 
70  return f->qp_table_buf->data;
71 }
72 
73 const char *av_get_colorspace_name(enum AVColorSpace val)
74 {
75  static const char *name[] = {
76  [AVCOL_SPC_RGB] = "GBR",
77  [AVCOL_SPC_BT709] = "bt709",
78  [AVCOL_SPC_FCC] = "fcc",
79  [AVCOL_SPC_BT470BG] = "bt470bg",
80  [AVCOL_SPC_SMPTE170M] = "smpte170m",
81  [AVCOL_SPC_SMPTE240M] = "smpte240m",
82  [AVCOL_SPC_YCOCG] = "YCgCo",
83  };
84  if ((unsigned)val >= FF_ARRAY_ELEMS(name))
85  return NULL;
86  return name[val];
87 }
88 
89 static void get_frame_defaults(AVFrame *frame)
90 {
91  if (frame->extended_data != frame->data)
92  av_freep(&frame->extended_data);
93 
94  memset(frame, 0, sizeof(*frame));
95 
96  frame->pts =
97  frame->pkt_dts =
98  frame->pkt_pts = AV_NOPTS_VALUE;
100  av_frame_set_pkt_duration (frame, 0);
101  av_frame_set_pkt_pos (frame, -1);
102  av_frame_set_pkt_size (frame, -1);
103  frame->key_frame = 1;
104  frame->sample_aspect_ratio = (AVRational){ 0, 1 };
105  frame->format = -1; /* unknown */
106  frame->extended_data = frame->data;
107 #if FF_API_AVFRAME_COLORSPACE
113 #endif
114 }
115 
116 static void free_side_data(AVFrameSideData **ptr_sd)
117 {
118  AVFrameSideData *sd = *ptr_sd;
119 
120  av_freep(&sd->data);
121  av_dict_free(&sd->metadata);
122  av_freep(ptr_sd);
123 }
124 
125 AVFrame *av_frame_alloc(void)
126 {
127  AVFrame *frame = av_mallocz(sizeof(*frame));
128 
129  if (!frame)
130  return NULL;
131 
132  frame->extended_data = NULL;
133  get_frame_defaults(frame);
134 
135  return frame;
136 }
137 
138 void av_frame_free(AVFrame **frame)
139 {
140  if (!frame || !*frame)
141  return;
142 
143  av_frame_unref(*frame);
144  av_freep(frame);
145 }
146 
147 static int get_video_buffer(AVFrame *frame, int align)
148 {
149  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
150  int ret, i;
151 
152  if (!desc)
153  return AVERROR(EINVAL);
154 
155  if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
156  return ret;
157 
158  if (!frame->linesize[0]) {
159  for(i=1; i<=align; i+=i) {
160  ret = av_image_fill_linesizes(frame->linesize, frame->format,
161  FFALIGN(frame->width, i));
162  if (ret < 0)
163  return ret;
164  if (!(frame->linesize[0] & (align-1)))
165  break;
166  }
167 
168  for (i = 0; i < 4 && frame->linesize[i]; i++)
169  frame->linesize[i] = FFALIGN(frame->linesize[i], align);
170  }
171 
172  for (i = 0; i < 4 && frame->linesize[i]; i++) {
173  int h = FFALIGN(frame->height, 32);
174  if (i == 1 || i == 2)
175  h = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
176 
177  frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h + 16 + 16/*STRIDE_ALIGN*/ - 1);
178  if (!frame->buf[i])
179  goto fail;
180 
181  frame->data[i] = frame->buf[i]->data;
182  }
183  if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
184  av_buffer_unref(&frame->buf[1]);
185  frame->buf[1] = av_buffer_alloc(1024);
186  if (!frame->buf[1])
187  goto fail;
188  frame->data[1] = frame->buf[1]->data;
189  }
190 
191  frame->extended_data = frame->data;
192 
193  return 0;
194 fail:
195  av_frame_unref(frame);
196  return AVERROR(ENOMEM);
197 }
198 
199 static int get_audio_buffer(AVFrame *frame, int align)
200 {
201  int channels;
202  int planar = av_sample_fmt_is_planar(frame->format);
203  int planes;
204  int ret, i;
205 
206  if (!frame->channels)
208 
209  channels = frame->channels;
210  planes = planar ? channels : 1;
211 
213  if (!frame->linesize[0]) {
214  ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
215  frame->nb_samples, frame->format,
216  align);
217  if (ret < 0)
218  return ret;
219  }
220 
221  if (planes > AV_NUM_DATA_POINTERS) {
222  frame->extended_data = av_mallocz_array(planes,
223  sizeof(*frame->extended_data));
225  sizeof(*frame->extended_buf));
226  if (!frame->extended_data || !frame->extended_buf) {
227  av_freep(&frame->extended_data);
228  av_freep(&frame->extended_buf);
229  return AVERROR(ENOMEM);
230  }
231  frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
232  } else
233  frame->extended_data = frame->data;
234 
235  for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
236  frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
237  if (!frame->buf[i]) {
238  av_frame_unref(frame);
239  return AVERROR(ENOMEM);
240  }
241  frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
242  }
243  for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
244  frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
245  if (!frame->extended_buf[i]) {
246  av_frame_unref(frame);
247  return AVERROR(ENOMEM);
248  }
249  frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
250  }
251  return 0;
252 
253 }
254 
255 int av_frame_get_buffer(AVFrame *frame, int align)
256 {
257  if (frame->format < 0)
258  return AVERROR(EINVAL);
259 
260  if (frame->width > 0 && frame->height > 0)
261  return get_video_buffer(frame, align);
262  else if (frame->nb_samples > 0 && (frame->channel_layout || frame->channels > 0))
263  return get_audio_buffer(frame, align);
264 
265  return AVERROR(EINVAL);
266 }
267 
268 int av_frame_ref(AVFrame *dst, const AVFrame *src)
269 {
270  int i, ret = 0;
271 
272  dst->format = src->format;
273  dst->width = src->width;
274  dst->height = src->height;
275  dst->channels = src->channels;
276  dst->channel_layout = src->channel_layout;
277  dst->nb_samples = src->nb_samples;
278 
279  ret = av_frame_copy_props(dst, src);
280  if (ret < 0)
281  return ret;
282 
283  /* duplicate the frame data if it's not refcounted */
284  if (!src->buf[0]) {
285  ret = av_frame_get_buffer(dst, 32);
286  if (ret < 0)
287  return ret;
288 
289  ret = av_frame_copy(dst, src);
290  if (ret < 0)
291  av_frame_unref(dst);
292 
293  return ret;
294  }
295 
296  /* ref the buffers */
297  for (i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
298  if (!src->buf[i])
299  continue;
300  dst->buf[i] = av_buffer_ref(src->buf[i]);
301  if (!dst->buf[i]) {
302  ret = AVERROR(ENOMEM);
303  goto fail;
304  }
305  }
306 
307  if (src->extended_buf) {
308  dst->extended_buf = av_mallocz_array(sizeof(*dst->extended_buf),
309  src->nb_extended_buf);
310  if (!dst->extended_buf) {
311  ret = AVERROR(ENOMEM);
312  goto fail;
313  }
314  dst->nb_extended_buf = src->nb_extended_buf;
315 
316  for (i = 0; i < src->nb_extended_buf; i++) {
317  dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
318  if (!dst->extended_buf[i]) {
319  ret = AVERROR(ENOMEM);
320  goto fail;
321  }
322  }
323  }
324 
325  /* duplicate extended data */
326  if (src->extended_data != src->data) {
327  int ch = src->channels;
328 
329  if (!ch) {
330  ret = AVERROR(EINVAL);
331  goto fail;
332  }
334 
335  dst->extended_data = av_malloc_array(sizeof(*dst->extended_data), ch);
336  if (!dst->extended_data) {
337  ret = AVERROR(ENOMEM);
338  goto fail;
339  }
340  memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
341  } else
342  dst->extended_data = dst->data;
343 
344  memcpy(dst->data, src->data, sizeof(src->data));
345  memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
346 
347  return 0;
348 
349 fail:
350  av_frame_unref(dst);
351  return ret;
352 }
353 
354 AVFrame *av_frame_clone(const AVFrame *src)
355 {
356  AVFrame *ret = av_frame_alloc();
357 
358  if (!ret)
359  return NULL;
360 
361  if (av_frame_ref(ret, src) < 0)
362  av_frame_free(&ret);
363 
364  return ret;
365 }
366 
367 void av_frame_unref(AVFrame *frame)
368 {
369  int i;
370 
371  for (i = 0; i < frame->nb_side_data; i++) {
372  free_side_data(&frame->side_data[i]);
373  }
374  av_freep(&frame->side_data);
375 
376  for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
377  av_buffer_unref(&frame->buf[i]);
378  for (i = 0; i < frame->nb_extended_buf; i++)
379  av_buffer_unref(&frame->extended_buf[i]);
380  av_freep(&frame->extended_buf);
381  av_dict_free(&frame->metadata);
382  av_buffer_unref(&frame->qp_table_buf);
383 
384  get_frame_defaults(frame);
385 }
386 
387 void av_frame_move_ref(AVFrame *dst, AVFrame *src)
388 {
389  *dst = *src;
390  if (src->extended_data == src->data)
391  dst->extended_data = dst->data;
392  memset(src, 0, sizeof(*src));
393  get_frame_defaults(src);
394 }
395 
396 int av_frame_is_writable(AVFrame *frame)
397 {
398  int i, ret = 1;
399 
400  /* assume non-refcounted frames are not writable */
401  if (!frame->buf[0])
402  return 0;
403 
404  for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
405  if (frame->buf[i])
406  ret &= !!av_buffer_is_writable(frame->buf[i]);
407  for (i = 0; i < frame->nb_extended_buf; i++)
408  ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
409 
410  return ret;
411 }
412 
413 int av_frame_make_writable(AVFrame *frame)
414 {
415  AVFrame tmp;
416  int ret;
417 
418  if (!frame->buf[0])
419  return AVERROR(EINVAL);
420 
421  if (av_frame_is_writable(frame))
422  return 0;
423 
424  memset(&tmp, 0, sizeof(tmp));
425  tmp.format = frame->format;
426  tmp.width = frame->width;
427  tmp.height = frame->height;
428  tmp.channels = frame->channels;
429  tmp.channel_layout = frame->channel_layout;
430  tmp.nb_samples = frame->nb_samples;
431  ret = av_frame_get_buffer(&tmp, 32);
432  if (ret < 0)
433  return ret;
434 
435  ret = av_frame_copy(&tmp, frame);
436  if (ret < 0) {
437  av_frame_unref(&tmp);
438  return ret;
439  }
440 
441  ret = av_frame_copy_props(&tmp, frame);
442  if (ret < 0) {
443  av_frame_unref(&tmp);
444  return ret;
445  }
446 
447  av_frame_unref(frame);
448 
449  *frame = tmp;
450  if (tmp.data == tmp.extended_data)
451  frame->extended_data = frame->data;
452 
453  return 0;
454 }
455 
456 int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
457 {
458  int i;
459 
460  dst->key_frame = src->key_frame;
461  dst->pict_type = src->pict_type;
463  dst->pts = src->pts;
464  dst->repeat_pict = src->repeat_pict;
466  dst->top_field_first = src->top_field_first;
468  dst->sample_rate = src->sample_rate;
469  dst->opaque = src->opaque;
470 #if FF_API_AVFRAME_LAVC
471  dst->type = src->type;
472 #endif
473  dst->pkt_pts = src->pkt_pts;
474  dst->pkt_dts = src->pkt_dts;
475  dst->pkt_pos = src->pkt_pos;
476  dst->pkt_size = src->pkt_size;
477  dst->pkt_duration = src->pkt_duration;
479  dst->quality = src->quality;
483  dst->flags = src->flags;
485 #if FF_API_AVFRAME_COLORSPACE
486  dst->color_primaries = src->color_primaries;
487  dst->color_trc = src->color_trc;
488  dst->colorspace = src->colorspace;
489  dst->color_range = src->color_range;
490  dst->chroma_location = src->chroma_location;
491 #endif
492 
493  av_dict_copy(&dst->metadata, src->metadata, 0);
494 
495  memcpy(dst->error, src->error, sizeof(dst->error));
496 
497  for (i = 0; i < src->nb_side_data; i++) {
498  const AVFrameSideData *sd_src = src->side_data[i];
499  AVFrameSideData *sd_dst = av_frame_new_side_data(dst, sd_src->type,
500  sd_src->size);
501  if (!sd_dst) {
502  for (i = 0; i < dst->nb_side_data; i++) {
503  free_side_data(&dst->side_data[i]);
504  }
505  av_freep(&dst->side_data);
506  return AVERROR(ENOMEM);
507  }
508  memcpy(sd_dst->data, sd_src->data, sd_src->size);
509  av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
510  }
511 
512  dst->qscale_table = NULL;
513  dst->qstride = 0;
514  dst->qscale_type = 0;
515  if (src->qp_table_buf) {
517  if (dst->qp_table_buf) {
518  dst->qscale_table = dst->qp_table_buf->data;
519  dst->qstride = src->qstride;
520  dst->qscale_type = src->qscale_type;
521  }
522  }
523 
524  return 0;
525 }
526 
527 AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
528 {
529  uint8_t *data;
530  int planes, i;
531 
532  if (frame->nb_samples) {
533  int channels = frame->channels;
534  if (!channels)
535  return NULL;
537  planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
538  } else
539  planes = 4;
540 
541  if (plane < 0 || plane >= planes || !frame->extended_data[plane])
542  return NULL;
543  data = frame->extended_data[plane];
544 
545  for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
546  AVBufferRef *buf = frame->buf[i];
547  if (data >= buf->data && data < buf->data + buf->size)
548  return buf;
549  }
550  for (i = 0; i < frame->nb_extended_buf; i++) {
551  AVBufferRef *buf = frame->extended_buf[i];
552  if (data >= buf->data && data < buf->data + buf->size)
553  return buf;
554  }
555  return NULL;
556 }
557 
560  int size)
561 {
562  AVFrameSideData *ret, **tmp;
563 
564  if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
565  return NULL;
566 
567  tmp = av_realloc(frame->side_data,
568  (frame->nb_side_data + 1) * sizeof(*frame->side_data));
569  if (!tmp)
570  return NULL;
571  frame->side_data = tmp;
572 
573  ret = av_mallocz(sizeof(*ret));
574  if (!ret)
575  return NULL;
576 
577  ret->data = av_malloc(size);
578  if (!ret->data) {
579  av_freep(&ret);
580  return NULL;
581  }
582 
583  ret->size = size;
584  ret->type = type;
585 
586  frame->side_data[frame->nb_side_data++] = ret;
587 
588  return ret;
589 }
590 
593 {
594  int i;
595 
596  for (i = 0; i < frame->nb_side_data; i++) {
597  if (frame->side_data[i]->type == type)
598  return frame->side_data[i];
599  }
600  return NULL;
601 }
602 
603 static int frame_copy_video(AVFrame *dst, const AVFrame *src)
604 {
605  const uint8_t *src_data[4];
606  int i, planes;
607 
608  if (dst->width < src->width ||
609  dst->height < src->height)
610  return AVERROR(EINVAL);
611 
612  planes = av_pix_fmt_count_planes(dst->format);
613  for (i = 0; i < planes; i++)
614  if (!dst->data[i] || !src->data[i])
615  return AVERROR(EINVAL);
616 
617  memcpy(src_data, src->data, sizeof(src_data));
618  av_image_copy(dst->data, dst->linesize,
619  src_data, src->linesize,
620  dst->format, src->width, src->height);
621 
622  return 0;
623 }
624 
625 static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
626 {
627  int planar = av_sample_fmt_is_planar(dst->format);
628  int channels = dst->channels;
629  int planes = planar ? channels : 1;
630  int i;
631 
632  if (dst->nb_samples != src->nb_samples ||
633  dst->channels != src->channels ||
634  dst->channel_layout != src->channel_layout)
635  return AVERROR(EINVAL);
636 
638 
639  for (i = 0; i < planes; i++)
640  if (!dst->extended_data[i] || !src->extended_data[i])
641  return AVERROR(EINVAL);
642 
644  dst->nb_samples, channels, dst->format);
645 
646  return 0;
647 }
648 
649 int av_frame_copy(AVFrame *dst, const AVFrame *src)
650 {
651  if (dst->format != src->format || dst->format < 0)
652  return AVERROR(EINVAL);
653 
654  if (dst->width > 0 && dst->height > 0)
655  return frame_copy_video(dst, src);
656  else if (dst->nb_samples > 0 && dst->channel_layout)
657  return frame_copy_audio(dst, src);
658 
659  return AVERROR(EINVAL);
660 }
661 
663 {
664  int i;
665 
666  for (i = 0; i < frame->nb_side_data; i++) {
667  AVFrameSideData *sd = frame->side_data[i];
668  if (sd->type == type) {
669  free_side_data(&frame->side_data[i]);
670  frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
671  frame->nb_side_data--;
672  }
673  }
674 }