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;};
49 int av_frame_set_qp_table(AVFrame *f, AVBufferRef *buf, int stride, int qp_type)
51 av_buffer_unref(&f->qp_table_buf);
53 f->qp_table_buf = buf;
55 f->qscale_table = buf->data;
57 f->qscale_type = qp_type;
62 int8_t *av_frame_get_qp_table(AVFrame *f, int *stride, int *type)
65 *type = f->qscale_type;
70 return f->qp_table_buf->data;
73 const char *av_get_colorspace_name(enum AVColorSpace val)
75 static const char * const 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",
84 if ((unsigned)val >= FF_ARRAY_ELEMS(name))
89 static void get_frame_defaults(AVFrame *frame)
91 if (frame->extended_data != frame->data)
92 av_freep(&frame->extended_data);
94 memset(frame, 0, sizeof(*frame));
98 frame->pkt_pts = AV_NOPTS_VALUE;
99 av_frame_set_best_effort_timestamp(frame, 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 frame->color_primaries = AVCOL_PRI_UNSPECIFIED;
108 frame->color_trc = AVCOL_TRC_UNSPECIFIED;
109 frame->colorspace = AVCOL_SPC_UNSPECIFIED;
110 frame->color_range = AVCOL_RANGE_UNSPECIFIED;
111 frame->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
114 static void free_side_data(AVFrameSideData **ptr_sd)
116 AVFrameSideData *sd = *ptr_sd;
118 av_buffer_unref(&sd->buf);
119 av_dict_free(&sd->metadata);
123 static void wipe_side_data(AVFrame *frame)
127 for (i = 0; i < frame->nb_side_data; i++) {
128 free_side_data(&frame->side_data[i]);
130 frame->nb_side_data = 0;
132 av_freep(&frame->side_data);
135 AVFrame *av_frame_alloc(void)
137 AVFrame *frame = av_mallocz(sizeof(*frame));
142 frame->extended_data = NULL;
143 get_frame_defaults(frame);
148 void av_frame_free(AVFrame **frame)
150 if (!frame || !*frame)
153 av_frame_unref(*frame);
157 static int get_video_buffer(AVFrame *frame, int align)
159 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
163 return AVERROR(EINVAL);
165 if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
168 if (!frame->linesize[0]) {
169 for(i=1; i<=align; i+=i) {
170 ret = av_image_fill_linesizes(frame->linesize, frame->format,
171 FFALIGN(frame->width, i));
174 if (!(frame->linesize[0] & (align-1)))
178 for (i = 0; i < 4 && frame->linesize[i]; i++)
179 frame->linesize[i] = FFALIGN(frame->linesize[i], align);
182 for (i = 0; i < 4 && frame->linesize[i]; i++) {
183 int h = FFALIGN(frame->height, 32);
184 if (i == 1 || i == 2)
185 h = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
187 frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h + 16 + 16/*STRIDE_ALIGN*/ - 1);
191 frame->data[i] = frame->buf[i]->data;
193 if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
194 av_buffer_unref(&frame->buf[1]);
195 frame->buf[1] = av_buffer_alloc(1024);
198 frame->data[1] = frame->buf[1]->data;
201 frame->extended_data = frame->data;
205 av_frame_unref(frame);
206 return AVERROR(ENOMEM);
209 static int get_audio_buffer(AVFrame *frame, int align)
212 int planar = av_sample_fmt_is_planar(frame->format);
216 if (!frame->channels)
217 frame->channels = av_get_channel_layout_nb_channels(frame->channel_layout);
219 channels = frame->channels;
220 planes = planar ? channels : 1;
222 CHECK_CHANNELS_CONSISTENCY(frame);
223 if (!frame->linesize[0]) {
224 ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
225 frame->nb_samples, frame->format,
231 if (planes > AV_NUM_DATA_POINTERS) {
232 frame->extended_data = av_mallocz_array(planes,
233 sizeof(*frame->extended_data));
234 frame->extended_buf = av_mallocz_array((planes - AV_NUM_DATA_POINTERS),
235 sizeof(*frame->extended_buf));
236 if (!frame->extended_data || !frame->extended_buf) {
237 av_freep(&frame->extended_data);
238 av_freep(&frame->extended_buf);
239 return AVERROR(ENOMEM);
241 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
243 frame->extended_data = frame->data;
245 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
246 frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
247 if (!frame->buf[i]) {
248 av_frame_unref(frame);
249 return AVERROR(ENOMEM);
251 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
253 for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
254 frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
255 if (!frame->extended_buf[i]) {
256 av_frame_unref(frame);
257 return AVERROR(ENOMEM);
259 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
265 int av_frame_get_buffer(AVFrame *frame, int align)
267 if (frame->format < 0)
268 return AVERROR(EINVAL);
270 if (frame->width > 0 && frame->height > 0)
271 return get_video_buffer(frame, align);
272 else if (frame->nb_samples > 0 && (frame->channel_layout || frame->channels > 0))
273 return get_audio_buffer(frame, align);
275 return AVERROR(EINVAL);
278 static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy)
282 dst->key_frame = src->key_frame;
283 dst->pict_type = src->pict_type;
284 dst->sample_aspect_ratio = src->sample_aspect_ratio;
286 dst->repeat_pict = src->repeat_pict;
287 dst->interlaced_frame = src->interlaced_frame;
288 dst->top_field_first = src->top_field_first;
289 dst->palette_has_changed = src->palette_has_changed;
290 dst->sample_rate = src->sample_rate;
291 dst->opaque = src->opaque;
292 #if FF_API_AVFRAME_LAVC
293 dst->type = src->type;
295 dst->pkt_pts = src->pkt_pts;
296 dst->pkt_dts = src->pkt_dts;
297 dst->pkt_pos = src->pkt_pos;
298 dst->pkt_size = src->pkt_size;
299 dst->pkt_duration = src->pkt_duration;
300 dst->reordered_opaque = src->reordered_opaque;
301 dst->quality = src->quality;
302 dst->best_effort_timestamp = src->best_effort_timestamp;
303 dst->coded_picture_number = src->coded_picture_number;
304 dst->display_picture_number = src->display_picture_number;
305 dst->flags = src->flags;
306 dst->decode_error_flags = src->decode_error_flags;
307 dst->color_primaries = src->color_primaries;
308 dst->color_trc = src->color_trc;
309 dst->colorspace = src->colorspace;
310 dst->color_range = src->color_range;
311 dst->chroma_location = src->chroma_location;
313 av_dict_copy(&dst->metadata, src->metadata, 0);
315 memcpy(dst->error, src->error, sizeof(dst->error));
317 for (i = 0; i < src->nb_side_data; i++) {
318 const AVFrameSideData *sd_src = src->side_data[i];
319 AVFrameSideData *sd_dst;
320 if ( sd_src->type == AV_FRAME_DATA_PANSCAN
321 && (src->width != dst->width || src->height != dst->height))
324 sd_dst = av_frame_new_side_data(dst, sd_src->type,
328 return AVERROR(ENOMEM);
330 memcpy(sd_dst->data, sd_src->data, sd_src->size);
332 sd_dst = av_frame_new_side_data(dst, sd_src->type, 0);
335 return AVERROR(ENOMEM);
337 sd_dst->buf = av_buffer_ref(sd_src->buf);
340 return AVERROR(ENOMEM);
342 sd_dst->data = sd_dst->buf->data;
343 sd_dst->size = sd_dst->buf->size;
345 av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
348 dst->qscale_table = NULL;
350 dst->qscale_type = 0;
351 if (src->qp_table_buf) {
352 dst->qp_table_buf = av_buffer_ref(src->qp_table_buf);
353 if (dst->qp_table_buf) {
354 dst->qscale_table = dst->qp_table_buf->data;
355 dst->qstride = src->qstride;
356 dst->qscale_type = src->qscale_type;
363 int av_frame_ref(AVFrame *dst, const AVFrame *src)
367 dst->format = src->format;
368 dst->width = src->width;
369 dst->height = src->height;
370 dst->channels = src->channels;
371 dst->channel_layout = src->channel_layout;
372 dst->nb_samples = src->nb_samples;
374 ret = frame_copy_props(dst, src, 0);
378 /* duplicate the frame data if it's not refcounted */
380 ret = av_frame_get_buffer(dst, 32);
384 ret = av_frame_copy(dst, src);
391 /* ref the buffers */
392 for (i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
395 dst->buf[i] = av_buffer_ref(src->buf[i]);
397 ret = AVERROR(ENOMEM);
402 if (src->extended_buf) {
403 dst->extended_buf = av_mallocz_array(sizeof(*dst->extended_buf),
404 src->nb_extended_buf);
405 if (!dst->extended_buf) {
406 ret = AVERROR(ENOMEM);
409 dst->nb_extended_buf = src->nb_extended_buf;
411 for (i = 0; i < src->nb_extended_buf; i++) {
412 dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
413 if (!dst->extended_buf[i]) {
414 ret = AVERROR(ENOMEM);
420 /* duplicate extended data */
421 if (src->extended_data != src->data) {
422 int ch = src->channels;
425 ret = AVERROR(EINVAL);
428 CHECK_CHANNELS_CONSISTENCY(src);
430 dst->extended_data = av_malloc_array(sizeof(*dst->extended_data), ch);
431 if (!dst->extended_data) {
432 ret = AVERROR(ENOMEM);
435 memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
437 dst->extended_data = dst->data;
439 memcpy(dst->data, src->data, sizeof(src->data));
440 memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
449 AVFrame *av_frame_clone(const AVFrame *src)
451 AVFrame *ret = av_frame_alloc();
456 if (av_frame_ref(ret, src) < 0)
462 void av_frame_unref(AVFrame *frame)
469 wipe_side_data(frame);
471 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
472 av_buffer_unref(&frame->buf[i]);
473 for (i = 0; i < frame->nb_extended_buf; i++)
474 av_buffer_unref(&frame->extended_buf[i]);
475 av_freep(&frame->extended_buf);
476 av_dict_free(&frame->metadata);
477 av_buffer_unref(&frame->qp_table_buf);
479 get_frame_defaults(frame);
482 void av_frame_move_ref(AVFrame *dst, AVFrame *src)
485 if (src->extended_data == src->data)
486 dst->extended_data = dst->data;
487 memset(src, 0, sizeof(*src));
488 get_frame_defaults(src);
491 int av_frame_is_writable(AVFrame *frame)
495 /* assume non-refcounted frames are not writable */
499 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
501 ret &= !!av_buffer_is_writable(frame->buf[i]);
502 for (i = 0; i < frame->nb_extended_buf; i++)
503 ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
508 int av_frame_make_writable(AVFrame *frame)
514 return AVERROR(EINVAL);
516 if (av_frame_is_writable(frame))
519 memset(&tmp, 0, sizeof(tmp));
520 tmp.format = frame->format;
521 tmp.width = frame->width;
522 tmp.height = frame->height;
523 tmp.channels = frame->channels;
524 tmp.channel_layout = frame->channel_layout;
525 tmp.nb_samples = frame->nb_samples;
526 ret = av_frame_get_buffer(&tmp, 32);
530 ret = av_frame_copy(&tmp, frame);
532 av_frame_unref(&tmp);
536 ret = av_frame_copy_props(&tmp, frame);
538 av_frame_unref(&tmp);
542 av_frame_unref(frame);
545 if (tmp.data == tmp.extended_data)
546 frame->extended_data = frame->data;
551 int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
553 return frame_copy_props(dst, src, 1);
556 AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
561 if (frame->nb_samples) {
562 int channels = frame->channels;
565 CHECK_CHANNELS_CONSISTENCY(frame);
566 planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
570 if (plane < 0 || plane >= planes || !frame->extended_data[plane])
572 data = frame->extended_data[plane];
574 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
575 AVBufferRef *buf = frame->buf[i];
576 if (data >= buf->data && data < buf->data + buf->size)
579 for (i = 0; i < frame->nb_extended_buf; i++) {
580 AVBufferRef *buf = frame->extended_buf[i];
581 if (data >= buf->data && data < buf->data + buf->size)
587 AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
588 enum AVFrameSideDataType type,
591 AVFrameSideData *ret, **tmp;
593 if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
596 tmp = av_realloc(frame->side_data,
597 (frame->nb_side_data + 1) * sizeof(*frame->side_data));
600 frame->side_data = tmp;
602 ret = av_mallocz(sizeof(*ret));
607 ret->buf = av_buffer_alloc(size);
613 ret->data = ret->buf->data;
618 frame->side_data[frame->nb_side_data++] = ret;
623 AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
624 enum AVFrameSideDataType type)
628 for (i = 0; i < frame->nb_side_data; i++) {
629 if (frame->side_data[i]->type == type)
630 return frame->side_data[i];
635 static int frame_copy_video(AVFrame *dst, const AVFrame *src)
637 const uint8_t *src_data[4];
640 if (dst->width < src->width ||
641 dst->height < src->height)
642 return AVERROR(EINVAL);
644 planes = av_pix_fmt_count_planes(dst->format);
645 for (i = 0; i < planes; i++)
646 if (!dst->data[i] || !src->data[i])
647 return AVERROR(EINVAL);
649 memcpy(src_data, src->data, sizeof(src_data));
650 av_image_copy(dst->data, dst->linesize,
651 src_data, src->linesize,
652 dst->format, src->width, src->height);
657 static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
659 int planar = av_sample_fmt_is_planar(dst->format);
660 int channels = dst->channels;
661 int planes = planar ? channels : 1;
664 if (dst->nb_samples != src->nb_samples ||
665 dst->channels != src->channels ||
666 dst->channel_layout != src->channel_layout)
667 return AVERROR(EINVAL);
669 CHECK_CHANNELS_CONSISTENCY(src);
671 for (i = 0; i < planes; i++)
672 if (!dst->extended_data[i] || !src->extended_data[i])
673 return AVERROR(EINVAL);
675 av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
676 dst->nb_samples, channels, dst->format);
681 int av_frame_copy(AVFrame *dst, const AVFrame *src)
683 if (dst->format != src->format || dst->format < 0)
684 return AVERROR(EINVAL);
686 if (dst->width > 0 && dst->height > 0)
687 return frame_copy_video(dst, src);
688 else if (dst->nb_samples > 0 && dst->channel_layout)
689 return frame_copy_audio(dst, src);
691 return AVERROR(EINVAL);
694 void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
698 for (i = 0; i < frame->nb_side_data; i++) {
699 AVFrameSideData *sd = frame->side_data[i];
700 if (sd->type == type) {
701 free_side_data(&frame->side_data[i]);
702 frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
703 frame->nb_side_data--;
708 const char *av_frame_side_data_name(enum AVFrameSideDataType type)
711 case AV_FRAME_DATA_PANSCAN: return "AVPanScan";
712 case AV_FRAME_DATA_A53_CC: return "ATSC A53 Part 4 Closed Captions";
713 case AV_FRAME_DATA_STEREO3D: return "Stereoscopic 3d metadata";
714 case AV_FRAME_DATA_MATRIXENCODING: return "AVMatrixEncoding";
715 case AV_FRAME_DATA_DOWNMIX_INFO: return "Metadata relevant to a downmix procedure";
716 case AV_FRAME_DATA_REPLAYGAIN: return "AVReplayGain";
717 case AV_FRAME_DATA_DISPLAYMATRIX: return "3x3 displaymatrix";
718 case AV_FRAME_DATA_MOTION_VECTORS: return "Motion vectors";