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 #if FF_API_FRAME_GET_SET
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)
43 #define CHECK_CHANNELS_CONSISTENCY(frame) \
44 av_assert2(!(frame)->channel_layout || \
45 (frame)->channels == \
46 av_get_channel_layout_nb_channels((frame)->channel_layout))
49 struct qp_properties {
54 int av_frame_set_qp_table(AVFrame *f, AVBufferRef *buf, int stride, int qp_type)
56 struct qp_properties *p;
60 FF_DISABLE_DEPRECATION_WARNINGS
61 av_buffer_unref(&f->qp_table_buf);
63 f->qp_table_buf = buf;
64 f->qscale_table = buf->data;
66 f->qscale_type = qp_type;
67 FF_ENABLE_DEPRECATION_WARNINGS
69 av_frame_remove_side_data(f, AV_FRAME_DATA_QP_TABLE_PROPERTIES);
70 av_frame_remove_side_data(f, AV_FRAME_DATA_QP_TABLE_DATA);
72 ref = av_buffer_ref(buf);
73 if (!av_frame_new_side_data_from_buf(f, AV_FRAME_DATA_QP_TABLE_DATA, ref)) {
74 av_buffer_unref(&ref);
75 return AVERROR(ENOMEM);
78 sd = av_frame_new_side_data(f, AV_FRAME_DATA_QP_TABLE_PROPERTIES,
79 sizeof(struct qp_properties));
81 return AVERROR(ENOMEM);
83 p = (struct qp_properties *)sd->data;
90 int8_t *av_frame_get_qp_table(AVFrame *f, int *stride, int *type)
92 AVBufferRef *buf = NULL;
97 FF_DISABLE_DEPRECATION_WARNINGS
98 if (f->qp_table_buf) {
100 *type = f->qscale_type;
101 buf = f->qp_table_buf;
102 FF_ENABLE_DEPRECATION_WARNINGS
105 struct qp_properties *p;
106 sd = av_frame_get_side_data(f, AV_FRAME_DATA_QP_TABLE_PROPERTIES);
109 p = (struct qp_properties *)sd->data;
110 sd = av_frame_get_side_data(f, AV_FRAME_DATA_QP_TABLE_DATA);
118 return buf ? buf->data : NULL;
122 const char *av_get_colorspace_name(enum AVColorSpace val)
124 static const char * const name[] = {
125 [AVCOL_SPC_RGB] = "GBR",
126 [AVCOL_SPC_BT709] = "bt709",
127 [AVCOL_SPC_FCC] = "fcc",
128 [AVCOL_SPC_BT470BG] = "bt470bg",
129 [AVCOL_SPC_SMPTE170M] = "smpte170m",
130 [AVCOL_SPC_SMPTE240M] = "smpte240m",
131 [AVCOL_SPC_YCOCG] = "YCgCo",
133 if ((unsigned)val >= FF_ARRAY_ELEMS(name))
138 static void get_frame_defaults(AVFrame *frame)
140 if (frame->extended_data != frame->data)
141 av_freep(&frame->extended_data);
143 memset(frame, 0, sizeof(*frame));
146 frame->pkt_dts = AV_NOPTS_VALUE;
148 FF_DISABLE_DEPRECATION_WARNINGS
149 frame->pkt_pts = AV_NOPTS_VALUE;
150 FF_ENABLE_DEPRECATION_WARNINGS
152 frame->best_effort_timestamp = AV_NOPTS_VALUE;
153 frame->pkt_duration = 0;
155 frame->pkt_size = -1;
156 frame->key_frame = 1;
157 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
158 frame->format = -1; /* unknown */
159 frame->extended_data = frame->data;
160 frame->color_primaries = AVCOL_PRI_UNSPECIFIED;
161 frame->color_trc = AVCOL_TRC_UNSPECIFIED;
162 frame->colorspace = AVCOL_SPC_UNSPECIFIED;
163 frame->color_range = AVCOL_RANGE_UNSPECIFIED;
164 frame->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
168 static void free_side_data(AVFrameSideData **ptr_sd)
170 AVFrameSideData *sd = *ptr_sd;
172 av_buffer_unref(&sd->buf);
173 av_dict_free(&sd->metadata);
177 static void wipe_side_data(AVFrame *frame)
181 for (i = 0; i < frame->nb_side_data; i++) {
182 free_side_data(&frame->side_data[i]);
184 frame->nb_side_data = 0;
186 av_freep(&frame->side_data);
189 AVFrame *av_frame_alloc(void)
191 AVFrame *frame = av_mallocz(sizeof(*frame));
196 frame->extended_data = NULL;
197 get_frame_defaults(frame);
202 void av_frame_free(AVFrame **frame)
204 if (!frame || !*frame)
207 av_frame_unref(*frame);
211 static int get_video_buffer(AVFrame *frame, int align)
213 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
217 return AVERROR(EINVAL);
219 if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
222 if (!frame->linesize[0]) {
224 align = 32; /* STRIDE_ALIGN. Should be av_cpu_max_align() */
226 for(i=1; i<=align; i+=i) {
227 ret = av_image_fill_linesizes(frame->linesize, frame->format,
228 FFALIGN(frame->width, i));
231 if (!(frame->linesize[0] & (align-1)))
235 for (i = 0; i < 4 && frame->linesize[i]; i++)
236 frame->linesize[i] = FFALIGN(frame->linesize[i], align);
239 for (i = 0; i < 4 && frame->linesize[i]; i++) {
240 int h = FFALIGN(frame->height, 32);
241 if (i == 1 || i == 2)
242 h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
244 frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h + 16 + 16/*STRIDE_ALIGN*/ - 1);
248 frame->data[i] = frame->buf[i]->data;
250 if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
251 av_buffer_unref(&frame->buf[1]);
252 frame->buf[1] = av_buffer_alloc(AVPALETTE_SIZE);
255 frame->data[1] = frame->buf[1]->data;
258 frame->extended_data = frame->data;
262 av_frame_unref(frame);
263 return AVERROR(ENOMEM);
266 static int get_audio_buffer(AVFrame *frame, int align)
269 int planar = av_sample_fmt_is_planar(frame->format);
273 if (!frame->channels)
274 frame->channels = av_get_channel_layout_nb_channels(frame->channel_layout);
276 channels = frame->channels;
277 planes = planar ? channels : 1;
279 CHECK_CHANNELS_CONSISTENCY(frame);
280 if (!frame->linesize[0]) {
281 ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
282 frame->nb_samples, frame->format,
288 if (planes > AV_NUM_DATA_POINTERS) {
289 frame->extended_data = av_mallocz_array(planes,
290 sizeof(*frame->extended_data));
291 frame->extended_buf = av_mallocz_array((planes - AV_NUM_DATA_POINTERS),
292 sizeof(*frame->extended_buf));
293 if (!frame->extended_data || !frame->extended_buf) {
294 av_freep(&frame->extended_data);
295 av_freep(&frame->extended_buf);
296 return AVERROR(ENOMEM);
298 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
300 frame->extended_data = frame->data;
302 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
303 frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
304 if (!frame->buf[i]) {
305 av_frame_unref(frame);
306 return AVERROR(ENOMEM);
308 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
310 for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
311 frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
312 if (!frame->extended_buf[i]) {
313 av_frame_unref(frame);
314 return AVERROR(ENOMEM);
316 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
322 int av_frame_get_buffer(AVFrame *frame, int align)
324 if (frame->format < 0)
325 return AVERROR(EINVAL);
327 if (frame->width > 0 && frame->height > 0)
328 return get_video_buffer(frame, align);
329 else if (frame->nb_samples > 0 && (frame->channel_layout || frame->channels > 0))
330 return get_audio_buffer(frame, align);
332 return AVERROR(EINVAL);
335 static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy)
339 dst->key_frame = src->key_frame;
340 dst->pict_type = src->pict_type;
341 dst->sample_aspect_ratio = src->sample_aspect_ratio;
342 dst->crop_top = src->crop_top;
343 dst->crop_bottom = src->crop_bottom;
344 dst->crop_left = src->crop_left;
345 dst->crop_right = src->crop_right;
347 dst->repeat_pict = src->repeat_pict;
348 dst->interlaced_frame = src->interlaced_frame;
349 dst->top_field_first = src->top_field_first;
350 dst->palette_has_changed = src->palette_has_changed;
351 dst->sample_rate = src->sample_rate;
352 dst->opaque = src->opaque;
354 FF_DISABLE_DEPRECATION_WARNINGS
355 dst->pkt_pts = src->pkt_pts;
356 FF_ENABLE_DEPRECATION_WARNINGS
358 dst->pkt_dts = src->pkt_dts;
359 dst->pkt_pos = src->pkt_pos;
360 dst->pkt_size = src->pkt_size;
361 dst->pkt_duration = src->pkt_duration;
362 dst->reordered_opaque = src->reordered_opaque;
363 dst->quality = src->quality;
364 dst->best_effort_timestamp = src->best_effort_timestamp;
365 dst->coded_picture_number = src->coded_picture_number;
366 dst->display_picture_number = src->display_picture_number;
367 dst->flags = src->flags;
368 dst->decode_error_flags = src->decode_error_flags;
369 dst->color_primaries = src->color_primaries;
370 dst->color_trc = src->color_trc;
371 dst->colorspace = src->colorspace;
372 dst->color_range = src->color_range;
373 dst->chroma_location = src->chroma_location;
375 av_dict_copy(&dst->metadata, src->metadata, 0);
377 #if FF_API_ERROR_FRAME
378 FF_DISABLE_DEPRECATION_WARNINGS
379 memcpy(dst->error, src->error, sizeof(dst->error));
380 FF_ENABLE_DEPRECATION_WARNINGS
383 for (i = 0; i < src->nb_side_data; i++) {
384 const AVFrameSideData *sd_src = src->side_data[i];
385 AVFrameSideData *sd_dst;
386 if ( sd_src->type == AV_FRAME_DATA_PANSCAN
387 && (src->width != dst->width || src->height != dst->height))
390 sd_dst = av_frame_new_side_data(dst, sd_src->type,
394 return AVERROR(ENOMEM);
396 memcpy(sd_dst->data, sd_src->data, sd_src->size);
398 AVBufferRef *ref = av_buffer_ref(sd_src->buf);
399 sd_dst = av_frame_new_side_data_from_buf(dst, sd_src->type, ref);
401 av_buffer_unref(&ref);
403 return AVERROR(ENOMEM);
406 av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
410 FF_DISABLE_DEPRECATION_WARNINGS
411 dst->qscale_table = NULL;
413 dst->qscale_type = 0;
414 av_buffer_unref(&dst->qp_table_buf);
415 if (src->qp_table_buf) {
416 dst->qp_table_buf = av_buffer_ref(src->qp_table_buf);
417 if (dst->qp_table_buf) {
418 dst->qscale_table = dst->qp_table_buf->data;
419 dst->qstride = src->qstride;
420 dst->qscale_type = src->qscale_type;
423 FF_ENABLE_DEPRECATION_WARNINGS
426 av_buffer_unref(&dst->opaque_ref);
427 av_buffer_unref(&dst->private_ref);
428 if (src->opaque_ref) {
429 dst->opaque_ref = av_buffer_ref(src->opaque_ref);
430 if (!dst->opaque_ref)
431 return AVERROR(ENOMEM);
433 if (src->private_ref) {
434 dst->private_ref = av_buffer_ref(src->private_ref);
435 if (!dst->private_ref)
436 return AVERROR(ENOMEM);
441 int av_frame_ref(AVFrame *dst, const AVFrame *src)
445 av_assert1(dst->width == 0 && dst->height == 0);
446 av_assert1(dst->channels == 0);
448 dst->format = src->format;
449 dst->width = src->width;
450 dst->height = src->height;
451 dst->channels = src->channels;
452 dst->channel_layout = src->channel_layout;
453 dst->nb_samples = src->nb_samples;
455 ret = frame_copy_props(dst, src, 0);
459 /* duplicate the frame data if it's not refcounted */
461 ret = av_frame_get_buffer(dst, 32);
465 ret = av_frame_copy(dst, src);
472 /* ref the buffers */
473 for (i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
476 dst->buf[i] = av_buffer_ref(src->buf[i]);
478 ret = AVERROR(ENOMEM);
483 if (src->extended_buf) {
484 dst->extended_buf = av_mallocz_array(sizeof(*dst->extended_buf),
485 src->nb_extended_buf);
486 if (!dst->extended_buf) {
487 ret = AVERROR(ENOMEM);
490 dst->nb_extended_buf = src->nb_extended_buf;
492 for (i = 0; i < src->nb_extended_buf; i++) {
493 dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
494 if (!dst->extended_buf[i]) {
495 ret = AVERROR(ENOMEM);
501 if (src->hw_frames_ctx) {
502 dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
503 if (!dst->hw_frames_ctx) {
504 ret = AVERROR(ENOMEM);
509 /* duplicate extended data */
510 if (src->extended_data != src->data) {
511 int ch = src->channels;
514 ret = AVERROR(EINVAL);
517 CHECK_CHANNELS_CONSISTENCY(src);
519 dst->extended_data = av_malloc_array(sizeof(*dst->extended_data), ch);
520 if (!dst->extended_data) {
521 ret = AVERROR(ENOMEM);
524 memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
526 dst->extended_data = dst->data;
528 memcpy(dst->data, src->data, sizeof(src->data));
529 memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
538 AVFrame *av_frame_clone(const AVFrame *src)
540 AVFrame *ret = av_frame_alloc();
545 if (av_frame_ref(ret, src) < 0)
551 void av_frame_unref(AVFrame *frame)
558 wipe_side_data(frame);
560 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
561 av_buffer_unref(&frame->buf[i]);
562 for (i = 0; i < frame->nb_extended_buf; i++)
563 av_buffer_unref(&frame->extended_buf[i]);
564 av_freep(&frame->extended_buf);
565 av_dict_free(&frame->metadata);
567 FF_DISABLE_DEPRECATION_WARNINGS
568 av_buffer_unref(&frame->qp_table_buf);
569 FF_ENABLE_DEPRECATION_WARNINGS
572 av_buffer_unref(&frame->hw_frames_ctx);
574 av_buffer_unref(&frame->opaque_ref);
575 av_buffer_unref(&frame->private_ref);
577 get_frame_defaults(frame);
580 void av_frame_move_ref(AVFrame *dst, AVFrame *src)
582 av_assert1(dst->width == 0 && dst->height == 0);
583 av_assert1(dst->channels == 0);
586 if (src->extended_data == src->data)
587 dst->extended_data = dst->data;
588 memset(src, 0, sizeof(*src));
589 get_frame_defaults(src);
592 int av_frame_is_writable(AVFrame *frame)
596 /* assume non-refcounted frames are not writable */
600 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
602 ret &= !!av_buffer_is_writable(frame->buf[i]);
603 for (i = 0; i < frame->nb_extended_buf; i++)
604 ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
609 int av_frame_make_writable(AVFrame *frame)
615 return AVERROR(EINVAL);
617 if (av_frame_is_writable(frame))
620 memset(&tmp, 0, sizeof(tmp));
621 tmp.format = frame->format;
622 tmp.width = frame->width;
623 tmp.height = frame->height;
624 tmp.channels = frame->channels;
625 tmp.channel_layout = frame->channel_layout;
626 tmp.nb_samples = frame->nb_samples;
627 ret = av_frame_get_buffer(&tmp, 32);
631 ret = av_frame_copy(&tmp, frame);
633 av_frame_unref(&tmp);
637 ret = av_frame_copy_props(&tmp, frame);
639 av_frame_unref(&tmp);
643 av_frame_unref(frame);
646 if (tmp.data == tmp.extended_data)
647 frame->extended_data = frame->data;
652 int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
654 return frame_copy_props(dst, src, 1);
657 AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
662 if (frame->nb_samples) {
663 int channels = frame->channels;
666 CHECK_CHANNELS_CONSISTENCY(frame);
667 planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
671 if (plane < 0 || plane >= planes || !frame->extended_data[plane])
673 data = frame->extended_data[plane];
675 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
676 AVBufferRef *buf = frame->buf[i];
677 if (data >= buf->data && data < buf->data + buf->size)
680 for (i = 0; i < frame->nb_extended_buf; i++) {
681 AVBufferRef *buf = frame->extended_buf[i];
682 if (data >= buf->data && data < buf->data + buf->size)
688 AVFrameSideData *av_frame_new_side_data_from_buf(AVFrame *frame,
689 enum AVFrameSideDataType type,
692 AVFrameSideData *ret, **tmp;
697 if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
700 tmp = av_realloc(frame->side_data,
701 (frame->nb_side_data + 1) * sizeof(*frame->side_data));
704 frame->side_data = tmp;
706 ret = av_mallocz(sizeof(*ret));
711 ret->data = ret->buf->data;
712 ret->size = buf->size;
715 frame->side_data[frame->nb_side_data++] = ret;
720 AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
721 enum AVFrameSideDataType type,
724 AVFrameSideData *ret;
725 AVBufferRef *buf = av_buffer_alloc(size);
726 ret = av_frame_new_side_data_from_buf(frame, type, buf);
728 av_buffer_unref(&buf);
732 AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
733 enum AVFrameSideDataType type)
737 for (i = 0; i < frame->nb_side_data; i++) {
738 if (frame->side_data[i]->type == type)
739 return frame->side_data[i];
744 static int frame_copy_video(AVFrame *dst, const AVFrame *src)
746 const uint8_t *src_data[4];
749 if (dst->width < src->width ||
750 dst->height < src->height)
751 return AVERROR(EINVAL);
753 planes = av_pix_fmt_count_planes(dst->format);
754 for (i = 0; i < planes; i++)
755 if (!dst->data[i] || !src->data[i])
756 return AVERROR(EINVAL);
758 memcpy(src_data, src->data, sizeof(src_data));
759 av_image_copy(dst->data, dst->linesize,
760 src_data, src->linesize,
761 dst->format, src->width, src->height);
766 static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
768 int planar = av_sample_fmt_is_planar(dst->format);
769 int channels = dst->channels;
770 int planes = planar ? channels : 1;
773 if (dst->nb_samples != src->nb_samples ||
774 dst->channels != src->channels ||
775 dst->channel_layout != src->channel_layout)
776 return AVERROR(EINVAL);
778 CHECK_CHANNELS_CONSISTENCY(src);
780 for (i = 0; i < planes; i++)
781 if (!dst->extended_data[i] || !src->extended_data[i])
782 return AVERROR(EINVAL);
784 av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
785 dst->nb_samples, channels, dst->format);
790 int av_frame_copy(AVFrame *dst, const AVFrame *src)
792 if (dst->format != src->format || dst->format < 0)
793 return AVERROR(EINVAL);
795 if (dst->width > 0 && dst->height > 0)
796 return frame_copy_video(dst, src);
797 else if (dst->nb_samples > 0 && dst->channels > 0)
798 return frame_copy_audio(dst, src);
800 return AVERROR(EINVAL);
803 void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
807 for (i = 0; i < frame->nb_side_data; i++) {
808 AVFrameSideData *sd = frame->side_data[i];
809 if (sd->type == type) {
810 free_side_data(&frame->side_data[i]);
811 frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
812 frame->nb_side_data--;
817 const char *av_frame_side_data_name(enum AVFrameSideDataType type)
820 case AV_FRAME_DATA_PANSCAN: return "AVPanScan";
821 case AV_FRAME_DATA_A53_CC: return "ATSC A53 Part 4 Closed Captions";
822 case AV_FRAME_DATA_STEREO3D: return "Stereoscopic 3d metadata";
823 case AV_FRAME_DATA_MATRIXENCODING: return "AVMatrixEncoding";
824 case AV_FRAME_DATA_DOWNMIX_INFO: return "Metadata relevant to a downmix procedure";
825 case AV_FRAME_DATA_REPLAYGAIN: return "AVReplayGain";
826 case AV_FRAME_DATA_DISPLAYMATRIX: return "3x3 displaymatrix";
827 case AV_FRAME_DATA_AFD: return "Active format description";
828 case AV_FRAME_DATA_MOTION_VECTORS: return "Motion vectors";
829 case AV_FRAME_DATA_SKIP_SAMPLES: return "Skip samples";
830 case AV_FRAME_DATA_AUDIO_SERVICE_TYPE: return "Audio service type";
831 case AV_FRAME_DATA_MASTERING_DISPLAY_METADATA: return "Mastering display metadata";
832 case AV_FRAME_DATA_CONTENT_LIGHT_LEVEL: return "Content light level metadata";
833 case AV_FRAME_DATA_GOP_TIMECODE: return "GOP timecode";
834 case AV_FRAME_DATA_ICC_PROFILE: return "ICC profile";
835 case AV_FRAME_DATA_QP_TABLE_PROPERTIES: return "QP table properties";
836 case AV_FRAME_DATA_QP_TABLE_DATA: return "QP table data";
841 static int calc_cropping_offsets(size_t offsets[4], const AVFrame *frame,
842 const AVPixFmtDescriptor *desc)
846 for (i = 0; frame->data[i]; i++) {
847 const AVComponentDescriptor *comp = NULL;
848 int shift_x = (i == 1 || i == 2) ? desc->log2_chroma_w : 0;
849 int shift_y = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
851 if (desc->flags & (AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_PSEUDOPAL) && i == 1) {
856 /* find any component descriptor for this plane */
857 for (j = 0; j < desc->nb_components; j++) {
858 if (desc->comp[j].plane == i) {
859 comp = &desc->comp[j];
866 offsets[i] = (frame->crop_top >> shift_y) * frame->linesize[i] +
867 (frame->crop_left >> shift_x) * comp->step;
873 int av_frame_apply_cropping(AVFrame *frame, int flags)
875 const AVPixFmtDescriptor *desc;
879 if (!(frame->width > 0 && frame->height > 0))
880 return AVERROR(EINVAL);
882 if (frame->crop_left >= INT_MAX - frame->crop_right ||
883 frame->crop_top >= INT_MAX - frame->crop_bottom ||
884 (frame->crop_left + frame->crop_right) >= frame->width ||
885 (frame->crop_top + frame->crop_bottom) >= frame->height)
886 return AVERROR(ERANGE);
888 desc = av_pix_fmt_desc_get(frame->format);
892 /* Apply just the right/bottom cropping for hwaccel formats. Bitstream
893 * formats cannot be easily handled here either (and corresponding decoders
894 * should not export any cropping anyway), so do the same for those as well.
896 if (desc->flags & (AV_PIX_FMT_FLAG_BITSTREAM | AV_PIX_FMT_FLAG_HWACCEL)) {
897 frame->width -= frame->crop_right;
898 frame->height -= frame->crop_bottom;
899 frame->crop_right = 0;
900 frame->crop_bottom = 0;
904 /* calculate the offsets for each plane */
905 calc_cropping_offsets(offsets, frame, desc);
907 /* adjust the offsets to avoid breaking alignment */
908 if (!(flags & AV_FRAME_CROP_UNALIGNED)) {
909 int log2_crop_align = frame->crop_left ? ff_ctz(frame->crop_left) : INT_MAX;
910 int min_log2_align = INT_MAX;
912 for (i = 0; frame->data[i]; i++) {
913 int log2_align = offsets[i] ? ff_ctz(offsets[i]) : INT_MAX;
914 min_log2_align = FFMIN(log2_align, min_log2_align);
917 /* we assume, and it should always be true, that the data alignment is
918 * related to the cropping alignment by a constant power-of-2 factor */
919 if (log2_crop_align < min_log2_align)
922 if (min_log2_align < 5) {
923 frame->crop_left &= ~((1 << (5 + log2_crop_align - min_log2_align)) - 1);
924 calc_cropping_offsets(offsets, frame, desc);
928 for (i = 0; frame->data[i]; i++)
929 frame->data[i] += offsets[i];
931 frame->width -= (frame->crop_left + frame->crop_right);
932 frame->height -= (frame->crop_top + frame->crop_bottom);
933 frame->crop_left = 0;
934 frame->crop_right = 0;
936 frame->crop_bottom = 0;