3 * This file is part of FFmpeg.
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.
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.
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
20 #include "channel_layout.h"
28 #include "samplefmt.h"
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)
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))
47 AVDictionary **avpriv_frame_get_metadatap(AVFrame *frame) {return &frame->metadata;};
50 int av_frame_set_qp_table(AVFrame *f, AVBufferRef *buf, int stride, int qp_type)
52 av_buffer_unref(&f->qp_table_buf);
54 f->qp_table_buf = buf;
56 FF_DISABLE_DEPRECATION_WARNINGS
57 f->qscale_table = buf->data;
59 f->qscale_type = qp_type;
60 FF_ENABLE_DEPRECATION_WARNINGS
65 int8_t *av_frame_get_qp_table(AVFrame *f, int *stride, int *type)
67 FF_DISABLE_DEPRECATION_WARNINGS
69 *type = f->qscale_type;
70 FF_ENABLE_DEPRECATION_WARNINGS
75 return f->qp_table_buf->data;
79 const char *av_get_colorspace_name(enum AVColorSpace val)
81 static const char * const name[] = {
82 [AVCOL_SPC_RGB] = "GBR",
83 [AVCOL_SPC_BT709] = "bt709",
84 [AVCOL_SPC_FCC] = "fcc",
85 [AVCOL_SPC_BT470BG] = "bt470bg",
86 [AVCOL_SPC_SMPTE170M] = "smpte170m",
87 [AVCOL_SPC_SMPTE240M] = "smpte240m",
88 [AVCOL_SPC_YCOCG] = "YCgCo",
90 if ((unsigned)val >= FF_ARRAY_ELEMS(name))
95 static void get_frame_defaults(AVFrame *frame)
97 if (frame->extended_data != frame->data)
98 av_freep(&frame->extended_data);
100 memset(frame, 0, sizeof(*frame));
104 frame->pkt_pts = AV_NOPTS_VALUE;
105 av_frame_set_best_effort_timestamp(frame, AV_NOPTS_VALUE);
106 av_frame_set_pkt_duration (frame, 0);
107 av_frame_set_pkt_pos (frame, -1);
108 av_frame_set_pkt_size (frame, -1);
109 frame->key_frame = 1;
110 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
111 frame->format = -1; /* unknown */
112 frame->extended_data = frame->data;
113 frame->color_primaries = AVCOL_PRI_UNSPECIFIED;
114 frame->color_trc = AVCOL_TRC_UNSPECIFIED;
115 frame->colorspace = AVCOL_SPC_UNSPECIFIED;
116 frame->color_range = AVCOL_RANGE_UNSPECIFIED;
117 frame->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
120 static void free_side_data(AVFrameSideData **ptr_sd)
122 AVFrameSideData *sd = *ptr_sd;
124 av_buffer_unref(&sd->buf);
125 av_dict_free(&sd->metadata);
129 static void wipe_side_data(AVFrame *frame)
133 for (i = 0; i < frame->nb_side_data; i++) {
134 free_side_data(&frame->side_data[i]);
136 frame->nb_side_data = 0;
138 av_freep(&frame->side_data);
141 AVFrame *av_frame_alloc(void)
143 AVFrame *frame = av_mallocz(sizeof(*frame));
148 frame->extended_data = NULL;
149 get_frame_defaults(frame);
154 void av_frame_free(AVFrame **frame)
156 if (!frame || !*frame)
159 av_frame_unref(*frame);
163 static int get_video_buffer(AVFrame *frame, int align)
165 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
169 return AVERROR(EINVAL);
171 if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
174 if (!frame->linesize[0]) {
175 for(i=1; i<=align; i+=i) {
176 ret = av_image_fill_linesizes(frame->linesize, frame->format,
177 FFALIGN(frame->width, i));
180 if (!(frame->linesize[0] & (align-1)))
184 for (i = 0; i < 4 && frame->linesize[i]; i++)
185 frame->linesize[i] = FFALIGN(frame->linesize[i], align);
188 for (i = 0; i < 4 && frame->linesize[i]; i++) {
189 int h = FFALIGN(frame->height, 32);
190 if (i == 1 || i == 2)
191 h = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
193 frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h + 16 + 16/*STRIDE_ALIGN*/ - 1);
197 frame->data[i] = frame->buf[i]->data;
199 if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
200 av_buffer_unref(&frame->buf[1]);
201 frame->buf[1] = av_buffer_alloc(1024);
204 frame->data[1] = frame->buf[1]->data;
207 frame->extended_data = frame->data;
211 av_frame_unref(frame);
212 return AVERROR(ENOMEM);
215 static int get_audio_buffer(AVFrame *frame, int align)
218 int planar = av_sample_fmt_is_planar(frame->format);
222 if (!frame->channels)
223 frame->channels = av_get_channel_layout_nb_channels(frame->channel_layout);
225 channels = frame->channels;
226 planes = planar ? channels : 1;
228 CHECK_CHANNELS_CONSISTENCY(frame);
229 if (!frame->linesize[0]) {
230 ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
231 frame->nb_samples, frame->format,
237 if (planes > AV_NUM_DATA_POINTERS) {
238 frame->extended_data = av_mallocz_array(planes,
239 sizeof(*frame->extended_data));
240 frame->extended_buf = av_mallocz_array((planes - AV_NUM_DATA_POINTERS),
241 sizeof(*frame->extended_buf));
242 if (!frame->extended_data || !frame->extended_buf) {
243 av_freep(&frame->extended_data);
244 av_freep(&frame->extended_buf);
245 return AVERROR(ENOMEM);
247 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
249 frame->extended_data = frame->data;
251 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
252 frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
253 if (!frame->buf[i]) {
254 av_frame_unref(frame);
255 return AVERROR(ENOMEM);
257 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
259 for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
260 frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
261 if (!frame->extended_buf[i]) {
262 av_frame_unref(frame);
263 return AVERROR(ENOMEM);
265 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
271 int av_frame_get_buffer(AVFrame *frame, int align)
273 if (frame->format < 0)
274 return AVERROR(EINVAL);
276 if (frame->width > 0 && frame->height > 0)
277 return get_video_buffer(frame, align);
278 else if (frame->nb_samples > 0 && (frame->channel_layout || frame->channels > 0))
279 return get_audio_buffer(frame, align);
281 return AVERROR(EINVAL);
284 static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy)
288 dst->key_frame = src->key_frame;
289 dst->pict_type = src->pict_type;
290 dst->sample_aspect_ratio = src->sample_aspect_ratio;
292 dst->repeat_pict = src->repeat_pict;
293 dst->interlaced_frame = src->interlaced_frame;
294 dst->top_field_first = src->top_field_first;
295 dst->palette_has_changed = src->palette_has_changed;
296 dst->sample_rate = src->sample_rate;
297 dst->opaque = src->opaque;
298 dst->pkt_pts = src->pkt_pts;
299 dst->pkt_dts = src->pkt_dts;
300 dst->pkt_pos = src->pkt_pos;
301 dst->pkt_size = src->pkt_size;
302 dst->pkt_duration = src->pkt_duration;
303 dst->reordered_opaque = src->reordered_opaque;
304 dst->quality = src->quality;
305 dst->best_effort_timestamp = src->best_effort_timestamp;
306 dst->coded_picture_number = src->coded_picture_number;
307 dst->display_picture_number = src->display_picture_number;
308 dst->flags = src->flags;
309 dst->decode_error_flags = src->decode_error_flags;
310 dst->color_primaries = src->color_primaries;
311 dst->color_trc = src->color_trc;
312 dst->colorspace = src->colorspace;
313 dst->color_range = src->color_range;
314 dst->chroma_location = src->chroma_location;
316 av_dict_copy(&dst->metadata, src->metadata, 0);
318 #if FF_API_ERROR_FRAME
319 FF_DISABLE_DEPRECATION_WARNINGS
320 memcpy(dst->error, src->error, sizeof(dst->error));
321 FF_ENABLE_DEPRECATION_WARNINGS
324 for (i = 0; i < src->nb_side_data; i++) {
325 const AVFrameSideData *sd_src = src->side_data[i];
326 AVFrameSideData *sd_dst;
327 if ( sd_src->type == AV_FRAME_DATA_PANSCAN
328 && (src->width != dst->width || src->height != dst->height))
331 sd_dst = av_frame_new_side_data(dst, sd_src->type,
335 return AVERROR(ENOMEM);
337 memcpy(sd_dst->data, sd_src->data, sd_src->size);
339 sd_dst = av_frame_new_side_data(dst, sd_src->type, 0);
342 return AVERROR(ENOMEM);
344 sd_dst->buf = av_buffer_ref(sd_src->buf);
347 return AVERROR(ENOMEM);
349 sd_dst->data = sd_dst->buf->data;
350 sd_dst->size = sd_dst->buf->size;
352 av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
356 FF_DISABLE_DEPRECATION_WARNINGS
357 dst->qscale_table = NULL;
359 dst->qscale_type = 0;
360 if (src->qp_table_buf) {
361 dst->qp_table_buf = av_buffer_ref(src->qp_table_buf);
362 if (dst->qp_table_buf) {
363 dst->qscale_table = dst->qp_table_buf->data;
364 dst->qstride = src->qstride;
365 dst->qscale_type = src->qscale_type;
368 FF_ENABLE_DEPRECATION_WARNINGS
374 int av_frame_ref(AVFrame *dst, const AVFrame *src)
378 dst->format = src->format;
379 dst->width = src->width;
380 dst->height = src->height;
381 dst->channels = src->channels;
382 dst->channel_layout = src->channel_layout;
383 dst->nb_samples = src->nb_samples;
385 ret = frame_copy_props(dst, src, 0);
389 /* duplicate the frame data if it's not refcounted */
391 ret = av_frame_get_buffer(dst, 32);
395 ret = av_frame_copy(dst, src);
402 /* ref the buffers */
403 for (i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
406 dst->buf[i] = av_buffer_ref(src->buf[i]);
408 ret = AVERROR(ENOMEM);
413 if (src->extended_buf) {
414 dst->extended_buf = av_mallocz_array(sizeof(*dst->extended_buf),
415 src->nb_extended_buf);
416 if (!dst->extended_buf) {
417 ret = AVERROR(ENOMEM);
420 dst->nb_extended_buf = src->nb_extended_buf;
422 for (i = 0; i < src->nb_extended_buf; i++) {
423 dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
424 if (!dst->extended_buf[i]) {
425 ret = AVERROR(ENOMEM);
431 /* duplicate extended data */
432 if (src->extended_data != src->data) {
433 int ch = src->channels;
436 ret = AVERROR(EINVAL);
439 CHECK_CHANNELS_CONSISTENCY(src);
441 dst->extended_data = av_malloc_array(sizeof(*dst->extended_data), ch);
442 if (!dst->extended_data) {
443 ret = AVERROR(ENOMEM);
446 memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
448 dst->extended_data = dst->data;
450 memcpy(dst->data, src->data, sizeof(src->data));
451 memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
460 AVFrame *av_frame_clone(const AVFrame *src)
462 AVFrame *ret = av_frame_alloc();
467 if (av_frame_ref(ret, src) < 0)
473 void av_frame_unref(AVFrame *frame)
480 wipe_side_data(frame);
482 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
483 av_buffer_unref(&frame->buf[i]);
484 for (i = 0; i < frame->nb_extended_buf; i++)
485 av_buffer_unref(&frame->extended_buf[i]);
486 av_freep(&frame->extended_buf);
487 av_dict_free(&frame->metadata);
489 av_buffer_unref(&frame->qp_table_buf);
492 get_frame_defaults(frame);
495 void av_frame_move_ref(AVFrame *dst, AVFrame *src)
498 if (src->extended_data == src->data)
499 dst->extended_data = dst->data;
500 memset(src, 0, sizeof(*src));
501 get_frame_defaults(src);
504 int av_frame_is_writable(AVFrame *frame)
508 /* assume non-refcounted frames are not writable */
512 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
514 ret &= !!av_buffer_is_writable(frame->buf[i]);
515 for (i = 0; i < frame->nb_extended_buf; i++)
516 ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
521 int av_frame_make_writable(AVFrame *frame)
527 return AVERROR(EINVAL);
529 if (av_frame_is_writable(frame))
532 memset(&tmp, 0, sizeof(tmp));
533 tmp.format = frame->format;
534 tmp.width = frame->width;
535 tmp.height = frame->height;
536 tmp.channels = frame->channels;
537 tmp.channel_layout = frame->channel_layout;
538 tmp.nb_samples = frame->nb_samples;
539 ret = av_frame_get_buffer(&tmp, 32);
543 ret = av_frame_copy(&tmp, frame);
545 av_frame_unref(&tmp);
549 ret = av_frame_copy_props(&tmp, frame);
551 av_frame_unref(&tmp);
555 av_frame_unref(frame);
558 if (tmp.data == tmp.extended_data)
559 frame->extended_data = frame->data;
564 int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
566 return frame_copy_props(dst, src, 1);
569 AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
574 if (frame->nb_samples) {
575 int channels = frame->channels;
578 CHECK_CHANNELS_CONSISTENCY(frame);
579 planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
583 if (plane < 0 || plane >= planes || !frame->extended_data[plane])
585 data = frame->extended_data[plane];
587 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
588 AVBufferRef *buf = frame->buf[i];
589 if (data >= buf->data && data < buf->data + buf->size)
592 for (i = 0; i < frame->nb_extended_buf; i++) {
593 AVBufferRef *buf = frame->extended_buf[i];
594 if (data >= buf->data && data < buf->data + buf->size)
600 AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
601 enum AVFrameSideDataType type,
604 AVFrameSideData *ret, **tmp;
606 if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
609 tmp = av_realloc(frame->side_data,
610 (frame->nb_side_data + 1) * sizeof(*frame->side_data));
613 frame->side_data = tmp;
615 ret = av_mallocz(sizeof(*ret));
620 ret->buf = av_buffer_alloc(size);
626 ret->data = ret->buf->data;
631 frame->side_data[frame->nb_side_data++] = ret;
636 AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
637 enum AVFrameSideDataType type)
641 for (i = 0; i < frame->nb_side_data; i++) {
642 if (frame->side_data[i]->type == type)
643 return frame->side_data[i];
648 static int frame_copy_video(AVFrame *dst, const AVFrame *src)
650 const uint8_t *src_data[4];
653 if (dst->width < src->width ||
654 dst->height < src->height)
655 return AVERROR(EINVAL);
657 planes = av_pix_fmt_count_planes(dst->format);
658 for (i = 0; i < planes; i++)
659 if (!dst->data[i] || !src->data[i])
660 return AVERROR(EINVAL);
662 memcpy(src_data, src->data, sizeof(src_data));
663 av_image_copy(dst->data, dst->linesize,
664 src_data, src->linesize,
665 dst->format, src->width, src->height);
670 static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
672 int planar = av_sample_fmt_is_planar(dst->format);
673 int channels = dst->channels;
674 int planes = planar ? channels : 1;
677 if (dst->nb_samples != src->nb_samples ||
678 dst->channels != src->channels ||
679 dst->channel_layout != src->channel_layout)
680 return AVERROR(EINVAL);
682 CHECK_CHANNELS_CONSISTENCY(src);
684 for (i = 0; i < planes; i++)
685 if (!dst->extended_data[i] || !src->extended_data[i])
686 return AVERROR(EINVAL);
688 av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
689 dst->nb_samples, channels, dst->format);
694 int av_frame_copy(AVFrame *dst, const AVFrame *src)
696 if (dst->format != src->format || dst->format < 0)
697 return AVERROR(EINVAL);
699 if (dst->width > 0 && dst->height > 0)
700 return frame_copy_video(dst, src);
701 else if (dst->nb_samples > 0 && dst->channel_layout)
702 return frame_copy_audio(dst, src);
704 return AVERROR(EINVAL);
707 void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
711 for (i = 0; i < frame->nb_side_data; i++) {
712 AVFrameSideData *sd = frame->side_data[i];
713 if (sd->type == type) {
714 free_side_data(&frame->side_data[i]);
715 frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
716 frame->nb_side_data--;
721 const char *av_frame_side_data_name(enum AVFrameSideDataType type)
724 case AV_FRAME_DATA_PANSCAN: return "AVPanScan";
725 case AV_FRAME_DATA_A53_CC: return "ATSC A53 Part 4 Closed Captions";
726 case AV_FRAME_DATA_STEREO3D: return "Stereoscopic 3d metadata";
727 case AV_FRAME_DATA_MATRIXENCODING: return "AVMatrixEncoding";
728 case AV_FRAME_DATA_DOWNMIX_INFO: return "Metadata relevant to a downmix procedure";
729 case AV_FRAME_DATA_REPLAYGAIN: return "AVReplayGain";
730 case AV_FRAME_DATA_DISPLAYMATRIX: return "3x3 displaymatrix";
731 case AV_FRAME_DATA_MOTION_VECTORS: return "Motion vectors";