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"
28 #include "hwcontext.h"
30 #if FF_API_FRAME_GET_SET
31 MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
32 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
33 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
34 MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
35 MAKE_ACCESSORS(AVFrame, frame, int, channels)
36 MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
37 MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
38 MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
39 MAKE_ACCESSORS(AVFrame, frame, int, pkt_size)
40 MAKE_ACCESSORS(AVFrame, frame, enum AVColorSpace, colorspace)
41 MAKE_ACCESSORS(AVFrame, frame, enum AVColorRange, color_range)
44 #define CHECK_CHANNELS_CONSISTENCY(frame) \
45 av_assert2(!(frame)->channel_layout || \
46 (frame)->channels == \
47 av_get_channel_layout_nb_channels((frame)->channel_layout))
50 struct qp_properties {
55 int av_frame_set_qp_table(AVFrame *f, AVBufferRef *buf, int stride, int qp_type)
57 struct qp_properties *p;
61 FF_DISABLE_DEPRECATION_WARNINGS
62 av_buffer_unref(&f->qp_table_buf);
64 f->qp_table_buf = buf;
65 f->qscale_table = buf->data;
67 f->qscale_type = qp_type;
68 FF_ENABLE_DEPRECATION_WARNINGS
70 av_frame_remove_side_data(f, AV_FRAME_DATA_QP_TABLE_PROPERTIES);
71 av_frame_remove_side_data(f, AV_FRAME_DATA_QP_TABLE_DATA);
73 ref = av_buffer_ref(buf);
74 if (!av_frame_new_side_data_from_buf(f, AV_FRAME_DATA_QP_TABLE_DATA, ref)) {
75 av_buffer_unref(&ref);
76 return AVERROR(ENOMEM);
79 sd = av_frame_new_side_data(f, AV_FRAME_DATA_QP_TABLE_PROPERTIES,
80 sizeof(struct qp_properties));
82 return AVERROR(ENOMEM);
84 p = (struct qp_properties *)sd->data;
91 int8_t *av_frame_get_qp_table(AVFrame *f, int *stride, int *type)
93 AVBufferRef *buf = NULL;
98 FF_DISABLE_DEPRECATION_WARNINGS
99 if (f->qp_table_buf) {
100 *stride = f->qstride;
101 *type = f->qscale_type;
102 buf = f->qp_table_buf;
103 FF_ENABLE_DEPRECATION_WARNINGS
106 struct qp_properties *p;
107 sd = av_frame_get_side_data(f, AV_FRAME_DATA_QP_TABLE_PROPERTIES);
110 p = (struct qp_properties *)sd->data;
111 sd = av_frame_get_side_data(f, AV_FRAME_DATA_QP_TABLE_DATA);
119 return buf ? buf->data : NULL;
123 #if FF_API_COLORSPACE_NAME
124 const char *av_get_colorspace_name(enum AVColorSpace val)
126 static const char * const name[] = {
127 [AVCOL_SPC_RGB] = "GBR",
128 [AVCOL_SPC_BT709] = "bt709",
129 [AVCOL_SPC_FCC] = "fcc",
130 [AVCOL_SPC_BT470BG] = "bt470bg",
131 [AVCOL_SPC_SMPTE170M] = "smpte170m",
132 [AVCOL_SPC_SMPTE240M] = "smpte240m",
133 [AVCOL_SPC_YCOCG] = "YCgCo",
135 if ((unsigned)val >= FF_ARRAY_ELEMS(name))
140 static void get_frame_defaults(AVFrame *frame)
142 if (frame->extended_data != frame->data)
143 av_freep(&frame->extended_data);
145 memset(frame, 0, sizeof(*frame));
148 frame->pkt_dts = AV_NOPTS_VALUE;
150 FF_DISABLE_DEPRECATION_WARNINGS
151 frame->pkt_pts = AV_NOPTS_VALUE;
152 FF_ENABLE_DEPRECATION_WARNINGS
154 frame->best_effort_timestamp = AV_NOPTS_VALUE;
155 frame->pkt_duration = 0;
157 frame->pkt_size = -1;
158 frame->key_frame = 1;
159 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
160 frame->format = -1; /* unknown */
161 frame->extended_data = frame->data;
162 frame->color_primaries = AVCOL_PRI_UNSPECIFIED;
163 frame->color_trc = AVCOL_TRC_UNSPECIFIED;
164 frame->colorspace = AVCOL_SPC_UNSPECIFIED;
165 frame->color_range = AVCOL_RANGE_UNSPECIFIED;
166 frame->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
170 static void free_side_data(AVFrameSideData **ptr_sd)
172 AVFrameSideData *sd = *ptr_sd;
174 av_buffer_unref(&sd->buf);
175 av_dict_free(&sd->metadata);
179 static void wipe_side_data(AVFrame *frame)
183 for (i = 0; i < frame->nb_side_data; i++) {
184 free_side_data(&frame->side_data[i]);
186 frame->nb_side_data = 0;
188 av_freep(&frame->side_data);
191 AVFrame *av_frame_alloc(void)
193 AVFrame *frame = av_mallocz(sizeof(*frame));
198 frame->extended_data = NULL;
199 get_frame_defaults(frame);
204 void av_frame_free(AVFrame **frame)
206 if (!frame || !*frame)
209 av_frame_unref(*frame);
213 static int get_video_buffer(AVFrame *frame, int align)
215 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
216 int ret, i, padded_height, total_size;
217 int plane_padding = FFMAX(16 + 16/*STRIDE_ALIGN*/, align);
218 ptrdiff_t linesizes[4];
222 return AVERROR(EINVAL);
224 if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
227 if (!frame->linesize[0]) {
229 align = 32; /* STRIDE_ALIGN. Should be av_cpu_max_align() */
231 for(i=1; i<=align; i+=i) {
232 ret = av_image_fill_linesizes(frame->linesize, frame->format,
233 FFALIGN(frame->width, i));
236 if (!(frame->linesize[0] & (align-1)))
240 for (i = 0; i < 4 && frame->linesize[i]; i++)
241 frame->linesize[i] = FFALIGN(frame->linesize[i], align);
244 for (i = 0; i < 4; i++)
245 linesizes[i] = frame->linesize[i];
247 padded_height = FFALIGN(frame->height, 32);
248 if ((ret = av_image_fill_plane_sizes(sizes, frame->format,
249 padded_height, linesizes)) < 0)
252 total_size = 4*plane_padding;
253 for (i = 0; i < 4; i++) {
254 if (sizes[i] > INT_MAX - total_size)
255 return AVERROR(EINVAL);
256 total_size += sizes[i];
259 frame->buf[0] = av_buffer_alloc(total_size);
260 if (!frame->buf[0]) {
261 ret = AVERROR(ENOMEM);
265 if ((ret = av_image_fill_pointers(frame->data, frame->format, padded_height,
266 frame->buf[0]->data, frame->linesize)) < 0)
269 for (i = 1; i < 4; i++) {
271 frame->data[i] += i * plane_padding;
274 frame->extended_data = frame->data;
278 av_frame_unref(frame);
282 static int get_audio_buffer(AVFrame *frame, int align)
285 int planar = av_sample_fmt_is_planar(frame->format);
289 if (!frame->channels)
290 frame->channels = av_get_channel_layout_nb_channels(frame->channel_layout);
292 channels = frame->channels;
293 planes = planar ? channels : 1;
295 CHECK_CHANNELS_CONSISTENCY(frame);
296 if (!frame->linesize[0]) {
297 ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
298 frame->nb_samples, frame->format,
304 if (planes > AV_NUM_DATA_POINTERS) {
305 frame->extended_data = av_mallocz_array(planes,
306 sizeof(*frame->extended_data));
307 frame->extended_buf = av_mallocz_array((planes - AV_NUM_DATA_POINTERS),
308 sizeof(*frame->extended_buf));
309 if (!frame->extended_data || !frame->extended_buf) {
310 av_freep(&frame->extended_data);
311 av_freep(&frame->extended_buf);
312 return AVERROR(ENOMEM);
314 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
316 frame->extended_data = frame->data;
318 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
319 frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
320 if (!frame->buf[i]) {
321 av_frame_unref(frame);
322 return AVERROR(ENOMEM);
324 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
326 for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
327 frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
328 if (!frame->extended_buf[i]) {
329 av_frame_unref(frame);
330 return AVERROR(ENOMEM);
332 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
338 int av_frame_get_buffer(AVFrame *frame, int align)
340 if (frame->format < 0)
341 return AVERROR(EINVAL);
343 if (frame->width > 0 && frame->height > 0)
344 return get_video_buffer(frame, align);
345 else if (frame->nb_samples > 0 && (frame->channel_layout || frame->channels > 0))
346 return get_audio_buffer(frame, align);
348 return AVERROR(EINVAL);
351 static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy)
355 dst->key_frame = src->key_frame;
356 dst->pict_type = src->pict_type;
357 dst->sample_aspect_ratio = src->sample_aspect_ratio;
358 dst->crop_top = src->crop_top;
359 dst->crop_bottom = src->crop_bottom;
360 dst->crop_left = src->crop_left;
361 dst->crop_right = src->crop_right;
363 dst->repeat_pict = src->repeat_pict;
364 dst->interlaced_frame = src->interlaced_frame;
365 dst->top_field_first = src->top_field_first;
366 dst->palette_has_changed = src->palette_has_changed;
367 dst->sample_rate = src->sample_rate;
368 dst->opaque = src->opaque;
370 FF_DISABLE_DEPRECATION_WARNINGS
371 dst->pkt_pts = src->pkt_pts;
372 FF_ENABLE_DEPRECATION_WARNINGS
374 dst->pkt_dts = src->pkt_dts;
375 dst->pkt_pos = src->pkt_pos;
376 dst->pkt_size = src->pkt_size;
377 dst->pkt_duration = src->pkt_duration;
378 dst->reordered_opaque = src->reordered_opaque;
379 dst->quality = src->quality;
380 dst->best_effort_timestamp = src->best_effort_timestamp;
381 dst->coded_picture_number = src->coded_picture_number;
382 dst->display_picture_number = src->display_picture_number;
383 dst->flags = src->flags;
384 dst->decode_error_flags = src->decode_error_flags;
385 dst->color_primaries = src->color_primaries;
386 dst->color_trc = src->color_trc;
387 dst->colorspace = src->colorspace;
388 dst->color_range = src->color_range;
389 dst->chroma_location = src->chroma_location;
391 av_dict_copy(&dst->metadata, src->metadata, 0);
393 #if FF_API_ERROR_FRAME
394 FF_DISABLE_DEPRECATION_WARNINGS
395 memcpy(dst->error, src->error, sizeof(dst->error));
396 FF_ENABLE_DEPRECATION_WARNINGS
399 for (i = 0; i < src->nb_side_data; i++) {
400 const AVFrameSideData *sd_src = src->side_data[i];
401 AVFrameSideData *sd_dst;
402 if ( sd_src->type == AV_FRAME_DATA_PANSCAN
403 && (src->width != dst->width || src->height != dst->height))
406 sd_dst = av_frame_new_side_data(dst, sd_src->type,
410 return AVERROR(ENOMEM);
412 memcpy(sd_dst->data, sd_src->data, sd_src->size);
414 AVBufferRef *ref = av_buffer_ref(sd_src->buf);
415 sd_dst = av_frame_new_side_data_from_buf(dst, sd_src->type, ref);
417 av_buffer_unref(&ref);
419 return AVERROR(ENOMEM);
422 av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
426 FF_DISABLE_DEPRECATION_WARNINGS
427 dst->qscale_table = NULL;
429 dst->qscale_type = 0;
430 av_buffer_replace(&dst->qp_table_buf, src->qp_table_buf);
431 if (dst->qp_table_buf) {
432 dst->qscale_table = dst->qp_table_buf->data;
433 dst->qstride = src->qstride;
434 dst->qscale_type = src->qscale_type;
436 FF_ENABLE_DEPRECATION_WARNINGS
439 ret = av_buffer_replace(&dst->opaque_ref, src->opaque_ref);
440 ret |= av_buffer_replace(&dst->private_ref, src->private_ref);
444 int av_frame_ref(AVFrame *dst, const AVFrame *src)
448 av_assert1(dst->width == 0 && dst->height == 0);
449 av_assert1(dst->channels == 0);
451 dst->format = src->format;
452 dst->width = src->width;
453 dst->height = src->height;
454 dst->channels = src->channels;
455 dst->channel_layout = src->channel_layout;
456 dst->nb_samples = src->nb_samples;
458 ret = frame_copy_props(dst, src, 0);
462 /* duplicate the frame data if it's not refcounted */
464 ret = av_frame_get_buffer(dst, 0);
468 ret = av_frame_copy(dst, src);
475 /* ref the buffers */
476 for (i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
479 dst->buf[i] = av_buffer_ref(src->buf[i]);
481 ret = AVERROR(ENOMEM);
486 if (src->extended_buf) {
487 dst->extended_buf = av_mallocz_array(sizeof(*dst->extended_buf),
488 src->nb_extended_buf);
489 if (!dst->extended_buf) {
490 ret = AVERROR(ENOMEM);
493 dst->nb_extended_buf = src->nb_extended_buf;
495 for (i = 0; i < src->nb_extended_buf; i++) {
496 dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
497 if (!dst->extended_buf[i]) {
498 ret = AVERROR(ENOMEM);
504 if (src->hw_frames_ctx) {
505 dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
506 if (!dst->hw_frames_ctx) {
507 ret = AVERROR(ENOMEM);
512 /* duplicate extended data */
513 if (src->extended_data != src->data) {
514 int ch = src->channels;
517 ret = AVERROR(EINVAL);
520 CHECK_CHANNELS_CONSISTENCY(src);
522 dst->extended_data = av_malloc_array(sizeof(*dst->extended_data), ch);
523 if (!dst->extended_data) {
524 ret = AVERROR(ENOMEM);
527 memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
529 dst->extended_data = dst->data;
531 memcpy(dst->data, src->data, sizeof(src->data));
532 memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
541 AVFrame *av_frame_clone(const AVFrame *src)
543 AVFrame *ret = av_frame_alloc();
548 if (av_frame_ref(ret, src) < 0)
554 void av_frame_unref(AVFrame *frame)
561 wipe_side_data(frame);
563 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
564 av_buffer_unref(&frame->buf[i]);
565 for (i = 0; i < frame->nb_extended_buf; i++)
566 av_buffer_unref(&frame->extended_buf[i]);
567 av_freep(&frame->extended_buf);
568 av_dict_free(&frame->metadata);
570 FF_DISABLE_DEPRECATION_WARNINGS
571 av_buffer_unref(&frame->qp_table_buf);
572 FF_ENABLE_DEPRECATION_WARNINGS
575 av_buffer_unref(&frame->hw_frames_ctx);
577 av_buffer_unref(&frame->opaque_ref);
578 av_buffer_unref(&frame->private_ref);
580 get_frame_defaults(frame);
583 void av_frame_move_ref(AVFrame *dst, AVFrame *src)
585 av_assert1(dst->width == 0 && dst->height == 0);
586 av_assert1(dst->channels == 0);
589 if (src->extended_data == src->data)
590 dst->extended_data = dst->data;
591 memset(src, 0, sizeof(*src));
592 get_frame_defaults(src);
595 int av_frame_is_writable(AVFrame *frame)
599 /* assume non-refcounted frames are not writable */
603 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
605 ret &= !!av_buffer_is_writable(frame->buf[i]);
606 for (i = 0; i < frame->nb_extended_buf; i++)
607 ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
612 int av_frame_make_writable(AVFrame *frame)
618 return AVERROR(EINVAL);
620 if (av_frame_is_writable(frame))
623 memset(&tmp, 0, sizeof(tmp));
624 tmp.format = frame->format;
625 tmp.width = frame->width;
626 tmp.height = frame->height;
627 tmp.channels = frame->channels;
628 tmp.channel_layout = frame->channel_layout;
629 tmp.nb_samples = frame->nb_samples;
631 if (frame->hw_frames_ctx)
632 ret = av_hwframe_get_buffer(frame->hw_frames_ctx, &tmp, 0);
634 ret = av_frame_get_buffer(&tmp, 0);
638 ret = av_frame_copy(&tmp, frame);
640 av_frame_unref(&tmp);
644 ret = av_frame_copy_props(&tmp, frame);
646 av_frame_unref(&tmp);
650 av_frame_unref(frame);
653 if (tmp.data == tmp.extended_data)
654 frame->extended_data = frame->data;
659 int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
661 return frame_copy_props(dst, src, 1);
664 AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
669 if (frame->nb_samples) {
670 int channels = frame->channels;
673 CHECK_CHANNELS_CONSISTENCY(frame);
674 planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
678 if (plane < 0 || plane >= planes || !frame->extended_data[plane])
680 data = frame->extended_data[plane];
682 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
683 AVBufferRef *buf = frame->buf[i];
684 if (data >= buf->data && data < buf->data + buf->size)
687 for (i = 0; i < frame->nb_extended_buf; i++) {
688 AVBufferRef *buf = frame->extended_buf[i];
689 if (data >= buf->data && data < buf->data + buf->size)
695 AVFrameSideData *av_frame_new_side_data_from_buf(AVFrame *frame,
696 enum AVFrameSideDataType type,
699 AVFrameSideData *ret, **tmp;
704 if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
707 tmp = av_realloc(frame->side_data,
708 (frame->nb_side_data + 1) * sizeof(*frame->side_data));
711 frame->side_data = tmp;
713 ret = av_mallocz(sizeof(*ret));
718 ret->data = ret->buf->data;
719 ret->size = buf->size;
722 frame->side_data[frame->nb_side_data++] = ret;
727 AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
728 enum AVFrameSideDataType type,
731 AVFrameSideData *ret;
732 AVBufferRef *buf = av_buffer_alloc(size);
733 ret = av_frame_new_side_data_from_buf(frame, type, buf);
735 av_buffer_unref(&buf);
739 AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
740 enum AVFrameSideDataType type)
744 for (i = 0; i < frame->nb_side_data; i++) {
745 if (frame->side_data[i]->type == type)
746 return frame->side_data[i];
751 static int frame_copy_video(AVFrame *dst, const AVFrame *src)
753 const uint8_t *src_data[4];
756 if (dst->width < src->width ||
757 dst->height < src->height)
758 return AVERROR(EINVAL);
760 if (src->hw_frames_ctx || dst->hw_frames_ctx)
761 return av_hwframe_transfer_data(dst, src, 0);
763 planes = av_pix_fmt_count_planes(dst->format);
764 for (i = 0; i < planes; i++)
765 if (!dst->data[i] || !src->data[i])
766 return AVERROR(EINVAL);
768 memcpy(src_data, src->data, sizeof(src_data));
769 av_image_copy(dst->data, dst->linesize,
770 src_data, src->linesize,
771 dst->format, src->width, src->height);
776 static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
778 int planar = av_sample_fmt_is_planar(dst->format);
779 int channels = dst->channels;
780 int planes = planar ? channels : 1;
783 if (dst->nb_samples != src->nb_samples ||
784 dst->channels != src->channels ||
785 dst->channel_layout != src->channel_layout)
786 return AVERROR(EINVAL);
788 CHECK_CHANNELS_CONSISTENCY(src);
790 for (i = 0; i < planes; i++)
791 if (!dst->extended_data[i] || !src->extended_data[i])
792 return AVERROR(EINVAL);
794 av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
795 dst->nb_samples, channels, dst->format);
800 int av_frame_copy(AVFrame *dst, const AVFrame *src)
802 if (dst->format != src->format || dst->format < 0)
803 return AVERROR(EINVAL);
805 if (dst->width > 0 && dst->height > 0)
806 return frame_copy_video(dst, src);
807 else if (dst->nb_samples > 0 && dst->channels > 0)
808 return frame_copy_audio(dst, src);
810 return AVERROR(EINVAL);
813 void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
817 for (i = frame->nb_side_data - 1; i >= 0; i--) {
818 AVFrameSideData *sd = frame->side_data[i];
819 if (sd->type == type) {
820 free_side_data(&frame->side_data[i]);
821 frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
822 frame->nb_side_data--;
827 const char *av_frame_side_data_name(enum AVFrameSideDataType type)
830 case AV_FRAME_DATA_PANSCAN: return "AVPanScan";
831 case AV_FRAME_DATA_A53_CC: return "ATSC A53 Part 4 Closed Captions";
832 case AV_FRAME_DATA_STEREO3D: return "Stereo 3D";
833 case AV_FRAME_DATA_MATRIXENCODING: return "AVMatrixEncoding";
834 case AV_FRAME_DATA_DOWNMIX_INFO: return "Metadata relevant to a downmix procedure";
835 case AV_FRAME_DATA_REPLAYGAIN: return "AVReplayGain";
836 case AV_FRAME_DATA_DISPLAYMATRIX: return "3x3 displaymatrix";
837 case AV_FRAME_DATA_AFD: return "Active format description";
838 case AV_FRAME_DATA_MOTION_VECTORS: return "Motion vectors";
839 case AV_FRAME_DATA_SKIP_SAMPLES: return "Skip samples";
840 case AV_FRAME_DATA_AUDIO_SERVICE_TYPE: return "Audio service type";
841 case AV_FRAME_DATA_MASTERING_DISPLAY_METADATA: return "Mastering display metadata";
842 case AV_FRAME_DATA_CONTENT_LIGHT_LEVEL: return "Content light level metadata";
843 case AV_FRAME_DATA_GOP_TIMECODE: return "GOP timecode";
844 case AV_FRAME_DATA_S12M_TIMECODE: return "SMPTE 12-1 timecode";
845 case AV_FRAME_DATA_SPHERICAL: return "Spherical Mapping";
846 case AV_FRAME_DATA_ICC_PROFILE: return "ICC profile";
848 case AV_FRAME_DATA_QP_TABLE_PROPERTIES: return "QP table properties";
849 case AV_FRAME_DATA_QP_TABLE_DATA: return "QP table data";
851 case AV_FRAME_DATA_DYNAMIC_HDR_PLUS: return "HDR Dynamic Metadata SMPTE2094-40 (HDR10+)";
852 case AV_FRAME_DATA_REGIONS_OF_INTEREST: return "Regions Of Interest";
853 case AV_FRAME_DATA_VIDEO_ENC_PARAMS: return "Video encoding parameters";
854 case AV_FRAME_DATA_SEI_UNREGISTERED: return "H.26[45] User Data Unregistered SEI message";
855 case AV_FRAME_DATA_FILM_GRAIN_PARAMS: return "Film grain parameters";
856 case AV_FRAME_DATA_DETECTION_BBOXES: return "Bounding boxes for object detection and classification";
861 static int calc_cropping_offsets(size_t offsets[4], const AVFrame *frame,
862 const AVPixFmtDescriptor *desc)
866 for (i = 0; frame->data[i]; i++) {
867 const AVComponentDescriptor *comp = NULL;
868 int shift_x = (i == 1 || i == 2) ? desc->log2_chroma_w : 0;
869 int shift_y = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
871 if (desc->flags & (AV_PIX_FMT_FLAG_PAL | FF_PSEUDOPAL) && i == 1) {
876 /* find any component descriptor for this plane */
877 for (j = 0; j < desc->nb_components; j++) {
878 if (desc->comp[j].plane == i) {
879 comp = &desc->comp[j];
886 offsets[i] = (frame->crop_top >> shift_y) * frame->linesize[i] +
887 (frame->crop_left >> shift_x) * comp->step;
893 int av_frame_apply_cropping(AVFrame *frame, int flags)
895 const AVPixFmtDescriptor *desc;
899 if (!(frame->width > 0 && frame->height > 0))
900 return AVERROR(EINVAL);
902 if (frame->crop_left >= INT_MAX - frame->crop_right ||
903 frame->crop_top >= INT_MAX - frame->crop_bottom ||
904 (frame->crop_left + frame->crop_right) >= frame->width ||
905 (frame->crop_top + frame->crop_bottom) >= frame->height)
906 return AVERROR(ERANGE);
908 desc = av_pix_fmt_desc_get(frame->format);
912 /* Apply just the right/bottom cropping for hwaccel formats. Bitstream
913 * formats cannot be easily handled here either (and corresponding decoders
914 * should not export any cropping anyway), so do the same for those as well.
916 if (desc->flags & (AV_PIX_FMT_FLAG_BITSTREAM | AV_PIX_FMT_FLAG_HWACCEL)) {
917 frame->width -= frame->crop_right;
918 frame->height -= frame->crop_bottom;
919 frame->crop_right = 0;
920 frame->crop_bottom = 0;
924 /* calculate the offsets for each plane */
925 calc_cropping_offsets(offsets, frame, desc);
927 /* adjust the offsets to avoid breaking alignment */
928 if (!(flags & AV_FRAME_CROP_UNALIGNED)) {
929 int log2_crop_align = frame->crop_left ? ff_ctz(frame->crop_left) : INT_MAX;
930 int min_log2_align = INT_MAX;
932 for (i = 0; frame->data[i]; i++) {
933 int log2_align = offsets[i] ? ff_ctz(offsets[i]) : INT_MAX;
934 min_log2_align = FFMIN(log2_align, min_log2_align);
937 /* we assume, and it should always be true, that the data alignment is
938 * related to the cropping alignment by a constant power-of-2 factor */
939 if (log2_crop_align < min_log2_align)
942 if (min_log2_align < 5) {
943 frame->crop_left &= ~((1 << (5 + log2_crop_align - min_log2_align)) - 1);
944 calc_cropping_offsets(offsets, frame, desc);
948 for (i = 0; frame->data[i]; i++)
949 frame->data[i] += offsets[i];
951 frame->width -= (frame->crop_left + frame->crop_right);
952 frame->height -= (frame->crop_top + frame->crop_bottom);
953 frame->crop_left = 0;
954 frame->crop_right = 0;
956 frame->crop_bottom = 0;