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"
30 static AVFrameSideData *frame_new_side_data(AVFrame *frame,
31 enum AVFrameSideDataType type,
34 MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
35 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
36 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
37 MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
38 MAKE_ACCESSORS(AVFrame, frame, int, channels)
39 MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
40 MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
41 MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
42 MAKE_ACCESSORS(AVFrame, frame, int, pkt_size)
43 MAKE_ACCESSORS(AVFrame, frame, enum AVColorSpace, colorspace)
44 MAKE_ACCESSORS(AVFrame, frame, enum AVColorRange, color_range)
46 #define CHECK_CHANNELS_CONSISTENCY(frame) \
47 av_assert2(!(frame)->channel_layout || \
48 (frame)->channels == \
49 av_get_channel_layout_nb_channels((frame)->channel_layout))
51 AVDictionary **avpriv_frame_get_metadatap(AVFrame *frame) {return &frame->metadata;};
54 int av_frame_set_qp_table(AVFrame *f, AVBufferRef *buf, int stride, int qp_type)
56 av_buffer_unref(&f->qp_table_buf);
58 f->qp_table_buf = buf;
60 FF_DISABLE_DEPRECATION_WARNINGS
61 f->qscale_table = buf->data;
63 f->qscale_type = qp_type;
64 FF_ENABLE_DEPRECATION_WARNINGS
69 int8_t *av_frame_get_qp_table(AVFrame *f, int *stride, int *type)
71 FF_DISABLE_DEPRECATION_WARNINGS
73 *type = f->qscale_type;
74 FF_ENABLE_DEPRECATION_WARNINGS
79 return f->qp_table_buf->data;
83 const char *av_get_colorspace_name(enum AVColorSpace val)
85 static const char * const name[] = {
86 [AVCOL_SPC_RGB] = "GBR",
87 [AVCOL_SPC_BT709] = "bt709",
88 [AVCOL_SPC_FCC] = "fcc",
89 [AVCOL_SPC_BT470BG] = "bt470bg",
90 [AVCOL_SPC_SMPTE170M] = "smpte170m",
91 [AVCOL_SPC_SMPTE240M] = "smpte240m",
92 [AVCOL_SPC_YCOCG] = "YCgCo",
94 if ((unsigned)val >= FF_ARRAY_ELEMS(name))
99 static void get_frame_defaults(AVFrame *frame)
101 if (frame->extended_data != frame->data)
102 av_freep(&frame->extended_data);
104 memset(frame, 0, sizeof(*frame));
107 frame->pkt_dts = AV_NOPTS_VALUE;
109 FF_DISABLE_DEPRECATION_WARNINGS
110 frame->pkt_pts = AV_NOPTS_VALUE;
111 FF_ENABLE_DEPRECATION_WARNINGS
113 frame->best_effort_timestamp = AV_NOPTS_VALUE;
114 frame->pkt_duration = 0;
116 frame->pkt_size = -1;
117 frame->key_frame = 1;
118 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
119 frame->format = -1; /* unknown */
120 frame->extended_data = frame->data;
121 frame->color_primaries = AVCOL_PRI_UNSPECIFIED;
122 frame->color_trc = AVCOL_TRC_UNSPECIFIED;
123 frame->colorspace = AVCOL_SPC_UNSPECIFIED;
124 frame->color_range = AVCOL_RANGE_UNSPECIFIED;
125 frame->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
129 static void free_side_data(AVFrameSideData **ptr_sd)
131 AVFrameSideData *sd = *ptr_sd;
133 av_buffer_unref(&sd->buf);
134 av_dict_free(&sd->metadata);
138 static void wipe_side_data(AVFrame *frame)
142 for (i = 0; i < frame->nb_side_data; i++) {
143 free_side_data(&frame->side_data[i]);
145 frame->nb_side_data = 0;
147 av_freep(&frame->side_data);
150 AVFrame *av_frame_alloc(void)
152 AVFrame *frame = av_mallocz(sizeof(*frame));
157 frame->extended_data = NULL;
158 get_frame_defaults(frame);
163 void av_frame_free(AVFrame **frame)
165 if (!frame || !*frame)
168 av_frame_unref(*frame);
172 static int get_video_buffer(AVFrame *frame, int align)
174 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
178 return AVERROR(EINVAL);
180 if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
183 if (!frame->linesize[0]) {
184 for(i=1; i<=align; i+=i) {
185 ret = av_image_fill_linesizes(frame->linesize, frame->format,
186 FFALIGN(frame->width, i));
189 if (!(frame->linesize[0] & (align-1)))
193 for (i = 0; i < 4 && frame->linesize[i]; i++)
194 frame->linesize[i] = FFALIGN(frame->linesize[i], align);
197 for (i = 0; i < 4 && frame->linesize[i]; i++) {
198 int h = FFALIGN(frame->height, 32);
199 if (i == 1 || i == 2)
200 h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
202 frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h + 16 + 16/*STRIDE_ALIGN*/ - 1);
206 frame->data[i] = frame->buf[i]->data;
208 if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
209 av_buffer_unref(&frame->buf[1]);
210 frame->buf[1] = av_buffer_alloc(AVPALETTE_SIZE);
213 frame->data[1] = frame->buf[1]->data;
216 frame->extended_data = frame->data;
220 av_frame_unref(frame);
221 return AVERROR(ENOMEM);
224 static int get_audio_buffer(AVFrame *frame, int align)
227 int planar = av_sample_fmt_is_planar(frame->format);
231 if (!frame->channels)
232 frame->channels = av_get_channel_layout_nb_channels(frame->channel_layout);
234 channels = frame->channels;
235 planes = planar ? channels : 1;
237 CHECK_CHANNELS_CONSISTENCY(frame);
238 if (!frame->linesize[0]) {
239 ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
240 frame->nb_samples, frame->format,
246 if (planes > AV_NUM_DATA_POINTERS) {
247 frame->extended_data = av_mallocz_array(planes,
248 sizeof(*frame->extended_data));
249 frame->extended_buf = av_mallocz_array((planes - AV_NUM_DATA_POINTERS),
250 sizeof(*frame->extended_buf));
251 if (!frame->extended_data || !frame->extended_buf) {
252 av_freep(&frame->extended_data);
253 av_freep(&frame->extended_buf);
254 return AVERROR(ENOMEM);
256 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
258 frame->extended_data = frame->data;
260 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
261 frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
262 if (!frame->buf[i]) {
263 av_frame_unref(frame);
264 return AVERROR(ENOMEM);
266 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
268 for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
269 frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
270 if (!frame->extended_buf[i]) {
271 av_frame_unref(frame);
272 return AVERROR(ENOMEM);
274 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
280 int av_frame_get_buffer(AVFrame *frame, int align)
282 if (frame->format < 0)
283 return AVERROR(EINVAL);
285 if (frame->width > 0 && frame->height > 0)
286 return get_video_buffer(frame, align);
287 else if (frame->nb_samples > 0 && (frame->channel_layout || frame->channels > 0))
288 return get_audio_buffer(frame, align);
290 return AVERROR(EINVAL);
293 static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy)
297 dst->key_frame = src->key_frame;
298 dst->pict_type = src->pict_type;
299 dst->sample_aspect_ratio = src->sample_aspect_ratio;
300 dst->crop_top = src->crop_top;
301 dst->crop_bottom = src->crop_bottom;
302 dst->crop_left = src->crop_left;
303 dst->crop_right = src->crop_right;
305 dst->repeat_pict = src->repeat_pict;
306 dst->interlaced_frame = src->interlaced_frame;
307 dst->top_field_first = src->top_field_first;
308 dst->palette_has_changed = src->palette_has_changed;
309 dst->sample_rate = src->sample_rate;
310 dst->opaque = src->opaque;
312 FF_DISABLE_DEPRECATION_WARNINGS
313 dst->pkt_pts = src->pkt_pts;
314 FF_ENABLE_DEPRECATION_WARNINGS
316 dst->pkt_dts = src->pkt_dts;
317 dst->pkt_pos = src->pkt_pos;
318 dst->pkt_size = src->pkt_size;
319 dst->pkt_duration = src->pkt_duration;
320 dst->reordered_opaque = src->reordered_opaque;
321 dst->quality = src->quality;
322 dst->best_effort_timestamp = src->best_effort_timestamp;
323 dst->coded_picture_number = src->coded_picture_number;
324 dst->display_picture_number = src->display_picture_number;
325 dst->flags = src->flags;
326 dst->decode_error_flags = src->decode_error_flags;
327 dst->color_primaries = src->color_primaries;
328 dst->color_trc = src->color_trc;
329 dst->colorspace = src->colorspace;
330 dst->color_range = src->color_range;
331 dst->chroma_location = src->chroma_location;
333 av_dict_copy(&dst->metadata, src->metadata, 0);
335 #if FF_API_ERROR_FRAME
336 FF_DISABLE_DEPRECATION_WARNINGS
337 memcpy(dst->error, src->error, sizeof(dst->error));
338 FF_ENABLE_DEPRECATION_WARNINGS
341 for (i = 0; i < src->nb_side_data; i++) {
342 const AVFrameSideData *sd_src = src->side_data[i];
343 AVFrameSideData *sd_dst;
344 if ( sd_src->type == AV_FRAME_DATA_PANSCAN
345 && (src->width != dst->width || src->height != dst->height))
348 sd_dst = av_frame_new_side_data(dst, sd_src->type,
352 return AVERROR(ENOMEM);
354 memcpy(sd_dst->data, sd_src->data, sd_src->size);
356 sd_dst = frame_new_side_data(dst, sd_src->type, av_buffer_ref(sd_src->buf));
359 return AVERROR(ENOMEM);
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
382 av_buffer_unref(&dst->opaque_ref);
383 if (src->opaque_ref) {
384 dst->opaque_ref = av_buffer_ref(src->opaque_ref);
385 if (!dst->opaque_ref)
386 return AVERROR(ENOMEM);
392 int av_frame_ref(AVFrame *dst, const AVFrame *src)
396 av_assert1(dst->width == 0 && dst->height == 0);
397 av_assert1(dst->channels == 0);
399 dst->format = src->format;
400 dst->width = src->width;
401 dst->height = src->height;
402 dst->channels = src->channels;
403 dst->channel_layout = src->channel_layout;
404 dst->nb_samples = src->nb_samples;
406 ret = frame_copy_props(dst, src, 0);
410 /* duplicate the frame data if it's not refcounted */
412 ret = av_frame_get_buffer(dst, 32);
416 ret = av_frame_copy(dst, src);
423 /* ref the buffers */
424 for (i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
427 dst->buf[i] = av_buffer_ref(src->buf[i]);
429 ret = AVERROR(ENOMEM);
434 if (src->extended_buf) {
435 dst->extended_buf = av_mallocz_array(sizeof(*dst->extended_buf),
436 src->nb_extended_buf);
437 if (!dst->extended_buf) {
438 ret = AVERROR(ENOMEM);
441 dst->nb_extended_buf = src->nb_extended_buf;
443 for (i = 0; i < src->nb_extended_buf; i++) {
444 dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
445 if (!dst->extended_buf[i]) {
446 ret = AVERROR(ENOMEM);
452 if (src->hw_frames_ctx) {
453 dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
454 if (!dst->hw_frames_ctx) {
455 ret = AVERROR(ENOMEM);
460 /* duplicate extended data */
461 if (src->extended_data != src->data) {
462 int ch = src->channels;
465 ret = AVERROR(EINVAL);
468 CHECK_CHANNELS_CONSISTENCY(src);
470 dst->extended_data = av_malloc_array(sizeof(*dst->extended_data), ch);
471 if (!dst->extended_data) {
472 ret = AVERROR(ENOMEM);
475 memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
477 dst->extended_data = dst->data;
479 memcpy(dst->data, src->data, sizeof(src->data));
480 memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
489 AVFrame *av_frame_clone(const AVFrame *src)
491 AVFrame *ret = av_frame_alloc();
496 if (av_frame_ref(ret, src) < 0)
502 void av_frame_unref(AVFrame *frame)
509 wipe_side_data(frame);
511 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
512 av_buffer_unref(&frame->buf[i]);
513 for (i = 0; i < frame->nb_extended_buf; i++)
514 av_buffer_unref(&frame->extended_buf[i]);
515 av_freep(&frame->extended_buf);
516 av_dict_free(&frame->metadata);
518 av_buffer_unref(&frame->qp_table_buf);
521 av_buffer_unref(&frame->hw_frames_ctx);
523 av_buffer_unref(&frame->opaque_ref);
525 get_frame_defaults(frame);
528 void av_frame_move_ref(AVFrame *dst, AVFrame *src)
530 av_assert1(dst->width == 0 && dst->height == 0);
531 av_assert1(dst->channels == 0);
534 if (src->extended_data == src->data)
535 dst->extended_data = dst->data;
536 memset(src, 0, sizeof(*src));
537 get_frame_defaults(src);
540 int av_frame_is_writable(AVFrame *frame)
544 /* assume non-refcounted frames are not writable */
548 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
550 ret &= !!av_buffer_is_writable(frame->buf[i]);
551 for (i = 0; i < frame->nb_extended_buf; i++)
552 ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
557 int av_frame_make_writable(AVFrame *frame)
563 return AVERROR(EINVAL);
565 if (av_frame_is_writable(frame))
568 memset(&tmp, 0, sizeof(tmp));
569 tmp.format = frame->format;
570 tmp.width = frame->width;
571 tmp.height = frame->height;
572 tmp.channels = frame->channels;
573 tmp.channel_layout = frame->channel_layout;
574 tmp.nb_samples = frame->nb_samples;
575 ret = av_frame_get_buffer(&tmp, 32);
579 ret = av_frame_copy(&tmp, frame);
581 av_frame_unref(&tmp);
585 ret = av_frame_copy_props(&tmp, frame);
587 av_frame_unref(&tmp);
591 av_frame_unref(frame);
594 if (tmp.data == tmp.extended_data)
595 frame->extended_data = frame->data;
600 int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
602 return frame_copy_props(dst, src, 1);
605 AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
610 if (frame->nb_samples) {
611 int channels = frame->channels;
614 CHECK_CHANNELS_CONSISTENCY(frame);
615 planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
619 if (plane < 0 || plane >= planes || !frame->extended_data[plane])
621 data = frame->extended_data[plane];
623 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
624 AVBufferRef *buf = frame->buf[i];
625 if (data >= buf->data && data < buf->data + buf->size)
628 for (i = 0; i < frame->nb_extended_buf; i++) {
629 AVBufferRef *buf = frame->extended_buf[i];
630 if (data >= buf->data && data < buf->data + buf->size)
636 static AVFrameSideData *frame_new_side_data(AVFrame *frame,
637 enum AVFrameSideDataType type,
640 AVFrameSideData *ret, **tmp;
645 if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
648 tmp = av_realloc(frame->side_data,
649 (frame->nb_side_data + 1) * sizeof(*frame->side_data));
652 frame->side_data = tmp;
654 ret = av_mallocz(sizeof(*ret));
659 ret->data = ret->buf->data;
660 ret->size = buf->size;
663 frame->side_data[frame->nb_side_data++] = ret;
667 av_buffer_unref(&buf);
671 AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
672 enum AVFrameSideDataType type,
676 return frame_new_side_data(frame, type, av_buffer_alloc(size));
679 AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
680 enum AVFrameSideDataType type)
684 for (i = 0; i < frame->nb_side_data; i++) {
685 if (frame->side_data[i]->type == type)
686 return frame->side_data[i];
691 static int frame_copy_video(AVFrame *dst, const AVFrame *src)
693 const uint8_t *src_data[4];
696 if (dst->width < src->width ||
697 dst->height < src->height)
698 return AVERROR(EINVAL);
700 planes = av_pix_fmt_count_planes(dst->format);
701 for (i = 0; i < planes; i++)
702 if (!dst->data[i] || !src->data[i])
703 return AVERROR(EINVAL);
705 memcpy(src_data, src->data, sizeof(src_data));
706 av_image_copy(dst->data, dst->linesize,
707 src_data, src->linesize,
708 dst->format, src->width, src->height);
713 static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
715 int planar = av_sample_fmt_is_planar(dst->format);
716 int channels = dst->channels;
717 int planes = planar ? channels : 1;
720 if (dst->nb_samples != src->nb_samples ||
721 dst->channels != src->channels ||
722 dst->channel_layout != src->channel_layout)
723 return AVERROR(EINVAL);
725 CHECK_CHANNELS_CONSISTENCY(src);
727 for (i = 0; i < planes; i++)
728 if (!dst->extended_data[i] || !src->extended_data[i])
729 return AVERROR(EINVAL);
731 av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
732 dst->nb_samples, channels, dst->format);
737 int av_frame_copy(AVFrame *dst, const AVFrame *src)
739 if (dst->format != src->format || dst->format < 0)
740 return AVERROR(EINVAL);
742 if (dst->width > 0 && dst->height > 0)
743 return frame_copy_video(dst, src);
744 else if (dst->nb_samples > 0 && dst->channels > 0)
745 return frame_copy_audio(dst, src);
747 return AVERROR(EINVAL);
750 void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
754 for (i = 0; i < frame->nb_side_data; i++) {
755 AVFrameSideData *sd = frame->side_data[i];
756 if (sd->type == type) {
757 free_side_data(&frame->side_data[i]);
758 frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
759 frame->nb_side_data--;
764 const char *av_frame_side_data_name(enum AVFrameSideDataType type)
767 case AV_FRAME_DATA_PANSCAN: return "AVPanScan";
768 case AV_FRAME_DATA_A53_CC: return "ATSC A53 Part 4 Closed Captions";
769 case AV_FRAME_DATA_STEREO3D: return "Stereoscopic 3d metadata";
770 case AV_FRAME_DATA_MATRIXENCODING: return "AVMatrixEncoding";
771 case AV_FRAME_DATA_DOWNMIX_INFO: return "Metadata relevant to a downmix procedure";
772 case AV_FRAME_DATA_REPLAYGAIN: return "AVReplayGain";
773 case AV_FRAME_DATA_DISPLAYMATRIX: return "3x3 displaymatrix";
774 case AV_FRAME_DATA_AFD: return "Active format description";
775 case AV_FRAME_DATA_MOTION_VECTORS: return "Motion vectors";
776 case AV_FRAME_DATA_SKIP_SAMPLES: return "Skip samples";
777 case AV_FRAME_DATA_AUDIO_SERVICE_TYPE: return "Audio service type";
778 case AV_FRAME_DATA_MASTERING_DISPLAY_METADATA: return "Mastering display metadata";
779 case AV_FRAME_DATA_CONTENT_LIGHT_LEVEL: return "Content light level metadata";
780 case AV_FRAME_DATA_GOP_TIMECODE: return "GOP timecode";