2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "channel_layout.h"
27 #include "samplefmt.h"
29 MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
30 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
31 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
32 MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
33 MAKE_ACCESSORS(AVFrame, frame, int, channels)
34 MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
35 MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
36 MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
37 MAKE_ACCESSORS(AVFrame, frame, int, pkt_size)
38 MAKE_ACCESSORS(AVFrame, frame, enum AVColorSpace, colorspace)
39 MAKE_ACCESSORS(AVFrame, frame, enum AVColorRange, color_range)
41 #define CHECK_CHANNELS_CONSISTENCY(frame) \
42 av_assert2(!(frame)->channel_layout || \
43 (frame)->channels == \
44 av_get_channel_layout_nb_channels((frame)->channel_layout))
46 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 FF_DISABLE_DEPRECATION_WARNINGS
56 f->qscale_table = buf->data;
58 f->qscale_type = qp_type;
59 FF_ENABLE_DEPRECATION_WARNINGS
64 int8_t *av_frame_get_qp_table(AVFrame *f, int *stride, int *type)
66 FF_DISABLE_DEPRECATION_WARNINGS
68 *type = f->qscale_type;
69 FF_ENABLE_DEPRECATION_WARNINGS
74 return f->qp_table_buf->data;
78 const char *av_get_colorspace_name(enum AVColorSpace val)
80 static const char * const name[] = {
81 [AVCOL_SPC_RGB] = "GBR",
82 [AVCOL_SPC_BT709] = "bt709",
83 [AVCOL_SPC_FCC] = "fcc",
84 [AVCOL_SPC_BT470BG] = "bt470bg",
85 [AVCOL_SPC_SMPTE170M] = "smpte170m",
86 [AVCOL_SPC_SMPTE240M] = "smpte240m",
87 [AVCOL_SPC_YCOCG] = "YCgCo",
89 if ((unsigned)val >= FF_ARRAY_ELEMS(name))
94 static void get_frame_defaults(AVFrame *frame)
96 if (frame->extended_data != frame->data)
97 av_freep(&frame->extended_data);
99 memset(frame, 0, sizeof(*frame));
102 frame->pkt_dts = AV_NOPTS_VALUE;
104 FF_DISABLE_DEPRECATION_WARNINGS
105 frame->pkt_pts = AV_NOPTS_VALUE;
106 FF_ENABLE_DEPRECATION_WARNINGS
108 frame->best_effort_timestamp = AV_NOPTS_VALUE;
109 frame->pkt_duration = 0;
111 frame->pkt_size = -1;
112 frame->key_frame = 1;
113 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
114 frame->format = -1; /* unknown */
115 frame->extended_data = frame->data;
116 frame->color_primaries = AVCOL_PRI_UNSPECIFIED;
117 frame->color_trc = AVCOL_TRC_UNSPECIFIED;
118 frame->colorspace = AVCOL_SPC_UNSPECIFIED;
119 frame->color_range = AVCOL_RANGE_UNSPECIFIED;
120 frame->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
124 static void free_side_data(AVFrameSideData **ptr_sd)
126 AVFrameSideData *sd = *ptr_sd;
128 av_buffer_unref(&sd->buf);
129 av_dict_free(&sd->metadata);
133 static void wipe_side_data(AVFrame *frame)
137 for (i = 0; i < frame->nb_side_data; i++) {
138 free_side_data(&frame->side_data[i]);
140 frame->nb_side_data = 0;
142 av_freep(&frame->side_data);
145 AVFrame *av_frame_alloc(void)
147 AVFrame *frame = av_mallocz(sizeof(*frame));
152 frame->extended_data = NULL;
153 get_frame_defaults(frame);
158 void av_frame_free(AVFrame **frame)
160 if (!frame || !*frame)
163 av_frame_unref(*frame);
167 static int get_video_buffer(AVFrame *frame, int align)
169 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
173 return AVERROR(EINVAL);
175 if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
178 if (!frame->linesize[0]) {
179 for(i=1; i<=align; i+=i) {
180 ret = av_image_fill_linesizes(frame->linesize, frame->format,
181 FFALIGN(frame->width, i));
184 if (!(frame->linesize[0] & (align-1)))
188 for (i = 0; i < 4 && frame->linesize[i]; i++)
189 frame->linesize[i] = FFALIGN(frame->linesize[i], align);
192 for (i = 0; i < 4 && frame->linesize[i]; i++) {
193 int h = FFALIGN(frame->height, 32);
194 if (i == 1 || i == 2)
195 h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
197 frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h + 16 + 16/*STRIDE_ALIGN*/ - 1);
201 frame->data[i] = frame->buf[i]->data;
203 if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
204 av_buffer_unref(&frame->buf[1]);
205 frame->buf[1] = av_buffer_alloc(AVPALETTE_SIZE);
208 frame->data[1] = frame->buf[1]->data;
211 frame->extended_data = frame->data;
215 av_frame_unref(frame);
216 return AVERROR(ENOMEM);
219 static int get_audio_buffer(AVFrame *frame, int align)
222 int planar = av_sample_fmt_is_planar(frame->format);
226 if (!frame->channels)
227 frame->channels = av_get_channel_layout_nb_channels(frame->channel_layout);
229 channels = frame->channels;
230 planes = planar ? channels : 1;
232 CHECK_CHANNELS_CONSISTENCY(frame);
233 if (!frame->linesize[0]) {
234 ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
235 frame->nb_samples, frame->format,
241 if (planes > AV_NUM_DATA_POINTERS) {
242 frame->extended_data = av_mallocz_array(planes,
243 sizeof(*frame->extended_data));
244 frame->extended_buf = av_mallocz_array((planes - AV_NUM_DATA_POINTERS),
245 sizeof(*frame->extended_buf));
246 if (!frame->extended_data || !frame->extended_buf) {
247 av_freep(&frame->extended_data);
248 av_freep(&frame->extended_buf);
249 return AVERROR(ENOMEM);
251 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
253 frame->extended_data = frame->data;
255 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
256 frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
257 if (!frame->buf[i]) {
258 av_frame_unref(frame);
259 return AVERROR(ENOMEM);
261 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
263 for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
264 frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
265 if (!frame->extended_buf[i]) {
266 av_frame_unref(frame);
267 return AVERROR(ENOMEM);
269 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
275 int av_frame_get_buffer(AVFrame *frame, int align)
277 if (frame->format < 0)
278 return AVERROR(EINVAL);
280 if (frame->width > 0 && frame->height > 0)
281 return get_video_buffer(frame, align);
282 else if (frame->nb_samples > 0 && (frame->channel_layout || frame->channels > 0))
283 return get_audio_buffer(frame, align);
285 return AVERROR(EINVAL);
288 static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy)
292 dst->key_frame = src->key_frame;
293 dst->pict_type = src->pict_type;
294 dst->sample_aspect_ratio = src->sample_aspect_ratio;
296 dst->repeat_pict = src->repeat_pict;
297 dst->interlaced_frame = src->interlaced_frame;
298 dst->top_field_first = src->top_field_first;
299 dst->palette_has_changed = src->palette_has_changed;
300 dst->sample_rate = src->sample_rate;
301 dst->opaque = src->opaque;
303 FF_DISABLE_DEPRECATION_WARNINGS
304 dst->pkt_pts = src->pkt_pts;
305 FF_ENABLE_DEPRECATION_WARNINGS
307 dst->pkt_dts = src->pkt_dts;
308 dst->pkt_pos = src->pkt_pos;
309 dst->pkt_size = src->pkt_size;
310 dst->pkt_duration = src->pkt_duration;
311 dst->reordered_opaque = src->reordered_opaque;
312 dst->quality = src->quality;
313 dst->best_effort_timestamp = src->best_effort_timestamp;
314 dst->coded_picture_number = src->coded_picture_number;
315 dst->display_picture_number = src->display_picture_number;
316 dst->flags = src->flags;
317 dst->decode_error_flags = src->decode_error_flags;
318 dst->color_primaries = src->color_primaries;
319 dst->color_trc = src->color_trc;
320 dst->colorspace = src->colorspace;
321 dst->color_range = src->color_range;
322 dst->chroma_location = src->chroma_location;
324 av_dict_copy(&dst->metadata, src->metadata, 0);
326 #if FF_API_ERROR_FRAME
327 FF_DISABLE_DEPRECATION_WARNINGS
328 memcpy(dst->error, src->error, sizeof(dst->error));
329 FF_ENABLE_DEPRECATION_WARNINGS
332 for (i = 0; i < src->nb_side_data; i++) {
333 const AVFrameSideData *sd_src = src->side_data[i];
334 AVFrameSideData *sd_dst;
335 if ( sd_src->type == AV_FRAME_DATA_PANSCAN
336 && (src->width != dst->width || src->height != dst->height))
339 sd_dst = av_frame_new_side_data(dst, sd_src->type,
343 return AVERROR(ENOMEM);
345 memcpy(sd_dst->data, sd_src->data, sd_src->size);
347 sd_dst = av_frame_new_side_data(dst, sd_src->type, 0);
350 return AVERROR(ENOMEM);
353 sd_dst->buf = av_buffer_ref(sd_src->buf);
356 return AVERROR(ENOMEM);
358 sd_dst->data = sd_dst->buf->data;
359 sd_dst->size = sd_dst->buf->size;
362 av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
366 FF_DISABLE_DEPRECATION_WARNINGS
367 dst->qscale_table = NULL;
369 dst->qscale_type = 0;
370 av_buffer_unref(&dst->qp_table_buf);
371 if (src->qp_table_buf) {
372 dst->qp_table_buf = av_buffer_ref(src->qp_table_buf);
373 if (dst->qp_table_buf) {
374 dst->qscale_table = dst->qp_table_buf->data;
375 dst->qstride = src->qstride;
376 dst->qscale_type = src->qscale_type;
379 FF_ENABLE_DEPRECATION_WARNINGS
385 int av_frame_ref(AVFrame *dst, const AVFrame *src)
389 av_assert1(dst->width == 0 && dst->height == 0);
390 av_assert1(dst->channels == 0);
392 dst->format = src->format;
393 dst->width = src->width;
394 dst->height = src->height;
395 dst->channels = src->channels;
396 dst->channel_layout = src->channel_layout;
397 dst->nb_samples = src->nb_samples;
399 ret = frame_copy_props(dst, src, 0);
403 /* duplicate the frame data if it's not refcounted */
405 ret = av_frame_get_buffer(dst, 32);
409 ret = av_frame_copy(dst, src);
416 /* ref the buffers */
417 for (i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
420 dst->buf[i] = av_buffer_ref(src->buf[i]);
422 ret = AVERROR(ENOMEM);
427 if (src->extended_buf) {
428 dst->extended_buf = av_mallocz_array(sizeof(*dst->extended_buf),
429 src->nb_extended_buf);
430 if (!dst->extended_buf) {
431 ret = AVERROR(ENOMEM);
434 dst->nb_extended_buf = src->nb_extended_buf;
436 for (i = 0; i < src->nb_extended_buf; i++) {
437 dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
438 if (!dst->extended_buf[i]) {
439 ret = AVERROR(ENOMEM);
445 if (src->hw_frames_ctx) {
446 dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
447 if (!dst->hw_frames_ctx) {
448 ret = AVERROR(ENOMEM);
453 /* duplicate extended data */
454 if (src->extended_data != src->data) {
455 int ch = src->channels;
458 ret = AVERROR(EINVAL);
461 CHECK_CHANNELS_CONSISTENCY(src);
463 dst->extended_data = av_malloc_array(sizeof(*dst->extended_data), ch);
464 if (!dst->extended_data) {
465 ret = AVERROR(ENOMEM);
468 memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
470 dst->extended_data = dst->data;
472 memcpy(dst->data, src->data, sizeof(src->data));
473 memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
482 AVFrame *av_frame_clone(const AVFrame *src)
484 AVFrame *ret = av_frame_alloc();
489 if (av_frame_ref(ret, src) < 0)
495 void av_frame_unref(AVFrame *frame)
502 wipe_side_data(frame);
504 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
505 av_buffer_unref(&frame->buf[i]);
506 for (i = 0; i < frame->nb_extended_buf; i++)
507 av_buffer_unref(&frame->extended_buf[i]);
508 av_freep(&frame->extended_buf);
509 av_dict_free(&frame->metadata);
511 av_buffer_unref(&frame->qp_table_buf);
514 av_buffer_unref(&frame->hw_frames_ctx);
516 get_frame_defaults(frame);
519 void av_frame_move_ref(AVFrame *dst, AVFrame *src)
521 av_assert1(dst->width == 0 && dst->height == 0);
522 av_assert1(dst->channels == 0);
525 if (src->extended_data == src->data)
526 dst->extended_data = dst->data;
527 memset(src, 0, sizeof(*src));
528 get_frame_defaults(src);
531 int av_frame_is_writable(AVFrame *frame)
535 /* assume non-refcounted frames are not writable */
539 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
541 ret &= !!av_buffer_is_writable(frame->buf[i]);
542 for (i = 0; i < frame->nb_extended_buf; i++)
543 ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
548 int av_frame_make_writable(AVFrame *frame)
554 return AVERROR(EINVAL);
556 if (av_frame_is_writable(frame))
559 memset(&tmp, 0, sizeof(tmp));
560 tmp.format = frame->format;
561 tmp.width = frame->width;
562 tmp.height = frame->height;
563 tmp.channels = frame->channels;
564 tmp.channel_layout = frame->channel_layout;
565 tmp.nb_samples = frame->nb_samples;
566 ret = av_frame_get_buffer(&tmp, 32);
570 ret = av_frame_copy(&tmp, frame);
572 av_frame_unref(&tmp);
576 ret = av_frame_copy_props(&tmp, frame);
578 av_frame_unref(&tmp);
582 av_frame_unref(frame);
585 if (tmp.data == tmp.extended_data)
586 frame->extended_data = frame->data;
591 int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
593 return frame_copy_props(dst, src, 1);
596 AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
601 if (frame->nb_samples) {
602 int channels = frame->channels;
605 CHECK_CHANNELS_CONSISTENCY(frame);
606 planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
610 if (plane < 0 || plane >= planes || !frame->extended_data[plane])
612 data = frame->extended_data[plane];
614 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
615 AVBufferRef *buf = frame->buf[i];
616 if (data >= buf->data && data < buf->data + buf->size)
619 for (i = 0; i < frame->nb_extended_buf; i++) {
620 AVBufferRef *buf = frame->extended_buf[i];
621 if (data >= buf->data && data < buf->data + buf->size)
627 AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
628 enum AVFrameSideDataType type,
631 AVFrameSideData *ret, **tmp;
633 if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
636 tmp = av_realloc(frame->side_data,
637 (frame->nb_side_data + 1) * sizeof(*frame->side_data));
640 frame->side_data = tmp;
642 ret = av_mallocz(sizeof(*ret));
647 ret->buf = av_buffer_alloc(size);
653 ret->data = ret->buf->data;
658 frame->side_data[frame->nb_side_data++] = ret;
663 AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
664 enum AVFrameSideDataType type)
668 for (i = 0; i < frame->nb_side_data; i++) {
669 if (frame->side_data[i]->type == type)
670 return frame->side_data[i];
675 static int frame_copy_video(AVFrame *dst, const AVFrame *src)
677 const uint8_t *src_data[4];
680 if (dst->width < src->width ||
681 dst->height < src->height)
682 return AVERROR(EINVAL);
684 planes = av_pix_fmt_count_planes(dst->format);
685 for (i = 0; i < planes; i++)
686 if (!dst->data[i] || !src->data[i])
687 return AVERROR(EINVAL);
689 memcpy(src_data, src->data, sizeof(src_data));
690 av_image_copy(dst->data, dst->linesize,
691 src_data, src->linesize,
692 dst->format, src->width, src->height);
697 static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
699 int planar = av_sample_fmt_is_planar(dst->format);
700 int channels = dst->channels;
701 int planes = planar ? channels : 1;
704 if (dst->nb_samples != src->nb_samples ||
705 dst->channels != src->channels ||
706 dst->channel_layout != src->channel_layout)
707 return AVERROR(EINVAL);
709 CHECK_CHANNELS_CONSISTENCY(src);
711 for (i = 0; i < planes; i++)
712 if (!dst->extended_data[i] || !src->extended_data[i])
713 return AVERROR(EINVAL);
715 av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
716 dst->nb_samples, channels, dst->format);
721 int av_frame_copy(AVFrame *dst, const AVFrame *src)
723 if (dst->format != src->format || dst->format < 0)
724 return AVERROR(EINVAL);
726 if (dst->width > 0 && dst->height > 0)
727 return frame_copy_video(dst, src);
728 else if (dst->nb_samples > 0 && dst->channel_layout)
729 return frame_copy_audio(dst, src);
731 return AVERROR(EINVAL);
734 void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
738 for (i = 0; i < frame->nb_side_data; i++) {
739 AVFrameSideData *sd = frame->side_data[i];
740 if (sd->type == type) {
741 free_side_data(&frame->side_data[i]);
742 frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
743 frame->nb_side_data--;
748 const char *av_frame_side_data_name(enum AVFrameSideDataType type)
751 case AV_FRAME_DATA_PANSCAN: return "AVPanScan";
752 case AV_FRAME_DATA_A53_CC: return "ATSC A53 Part 4 Closed Captions";
753 case AV_FRAME_DATA_STEREO3D: return "Stereoscopic 3d metadata";
754 case AV_FRAME_DATA_MATRIXENCODING: return "AVMatrixEncoding";
755 case AV_FRAME_DATA_DOWNMIX_INFO: return "Metadata relevant to a downmix procedure";
756 case AV_FRAME_DATA_REPLAYGAIN: return "AVReplayGain";
757 case AV_FRAME_DATA_DISPLAYMATRIX: return "3x3 displaymatrix";
758 case AV_FRAME_DATA_AFD: return "Active format description";
759 case AV_FRAME_DATA_MOTION_VECTORS: return "Motion vectors";
760 case AV_FRAME_DATA_SKIP_SAMPLES: return "Skip samples";
761 case AV_FRAME_DATA_AUDIO_SERVICE_TYPE: return "Audio service type";
762 case AV_FRAME_DATA_MASTERING_DISPLAY_METADATA: return "Mastering display metadata";
763 case AV_FRAME_DATA_GOP_TIMECODE: return "GOP timecode";