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 #if FF_API_FRAME_GET_SET
35 MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
36 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
37 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
38 MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
39 MAKE_ACCESSORS(AVFrame, frame, int, channels)
40 MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
41 MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
42 MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
43 MAKE_ACCESSORS(AVFrame, frame, int, pkt_size)
44 MAKE_ACCESSORS(AVFrame, frame, enum AVColorSpace, colorspace)
45 MAKE_ACCESSORS(AVFrame, frame, enum AVColorRange, color_range)
48 #define CHECK_CHANNELS_CONSISTENCY(frame) \
49 av_assert2(!(frame)->channel_layout || \
50 (frame)->channels == \
51 av_get_channel_layout_nb_channels((frame)->channel_layout))
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]) {
185 align = 32; /* STRIDE_ALIGN. Should be av_cpu_max_align() */
187 for(i=1; i<=align; i+=i) {
188 ret = av_image_fill_linesizes(frame->linesize, frame->format,
189 FFALIGN(frame->width, i));
192 if (!(frame->linesize[0] & (align-1)))
196 for (i = 0; i < 4 && frame->linesize[i]; i++)
197 frame->linesize[i] = FFALIGN(frame->linesize[i], align);
200 for (i = 0; i < 4 && frame->linesize[i]; i++) {
201 int h = FFALIGN(frame->height, 32);
202 if (i == 1 || i == 2)
203 h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
205 frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h + 16 + 16/*STRIDE_ALIGN*/ - 1);
209 frame->data[i] = frame->buf[i]->data;
211 if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
212 av_buffer_unref(&frame->buf[1]);
213 frame->buf[1] = av_buffer_alloc(AVPALETTE_SIZE);
216 frame->data[1] = frame->buf[1]->data;
219 frame->extended_data = frame->data;
223 av_frame_unref(frame);
224 return AVERROR(ENOMEM);
227 static int get_audio_buffer(AVFrame *frame, int align)
230 int planar = av_sample_fmt_is_planar(frame->format);
234 if (!frame->channels)
235 frame->channels = av_get_channel_layout_nb_channels(frame->channel_layout);
237 channels = frame->channels;
238 planes = planar ? channels : 1;
240 CHECK_CHANNELS_CONSISTENCY(frame);
241 if (!frame->linesize[0]) {
242 ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
243 frame->nb_samples, frame->format,
249 if (planes > AV_NUM_DATA_POINTERS) {
250 frame->extended_data = av_mallocz_array(planes,
251 sizeof(*frame->extended_data));
252 frame->extended_buf = av_mallocz_array((planes - AV_NUM_DATA_POINTERS),
253 sizeof(*frame->extended_buf));
254 if (!frame->extended_data || !frame->extended_buf) {
255 av_freep(&frame->extended_data);
256 av_freep(&frame->extended_buf);
257 return AVERROR(ENOMEM);
259 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
261 frame->extended_data = frame->data;
263 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
264 frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
265 if (!frame->buf[i]) {
266 av_frame_unref(frame);
267 return AVERROR(ENOMEM);
269 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
271 for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
272 frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
273 if (!frame->extended_buf[i]) {
274 av_frame_unref(frame);
275 return AVERROR(ENOMEM);
277 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
283 int av_frame_get_buffer(AVFrame *frame, int align)
285 if (frame->format < 0)
286 return AVERROR(EINVAL);
288 if (frame->width > 0 && frame->height > 0)
289 return get_video_buffer(frame, align);
290 else if (frame->nb_samples > 0 && (frame->channel_layout || frame->channels > 0))
291 return get_audio_buffer(frame, align);
293 return AVERROR(EINVAL);
296 static int frame_copy_props(AVFrame *dst, const AVFrame *src, int force_copy)
300 dst->key_frame = src->key_frame;
301 dst->pict_type = src->pict_type;
302 dst->sample_aspect_ratio = src->sample_aspect_ratio;
303 dst->crop_top = src->crop_top;
304 dst->crop_bottom = src->crop_bottom;
305 dst->crop_left = src->crop_left;
306 dst->crop_right = src->crop_right;
308 dst->repeat_pict = src->repeat_pict;
309 dst->interlaced_frame = src->interlaced_frame;
310 dst->top_field_first = src->top_field_first;
311 dst->palette_has_changed = src->palette_has_changed;
312 dst->sample_rate = src->sample_rate;
313 dst->opaque = src->opaque;
315 FF_DISABLE_DEPRECATION_WARNINGS
316 dst->pkt_pts = src->pkt_pts;
317 FF_ENABLE_DEPRECATION_WARNINGS
319 dst->pkt_dts = src->pkt_dts;
320 dst->pkt_pos = src->pkt_pos;
321 dst->pkt_size = src->pkt_size;
322 dst->pkt_duration = src->pkt_duration;
323 dst->reordered_opaque = src->reordered_opaque;
324 dst->quality = src->quality;
325 dst->best_effort_timestamp = src->best_effort_timestamp;
326 dst->coded_picture_number = src->coded_picture_number;
327 dst->display_picture_number = src->display_picture_number;
328 dst->flags = src->flags;
329 dst->decode_error_flags = src->decode_error_flags;
330 dst->color_primaries = src->color_primaries;
331 dst->color_trc = src->color_trc;
332 dst->colorspace = src->colorspace;
333 dst->color_range = src->color_range;
334 dst->chroma_location = src->chroma_location;
336 av_dict_copy(&dst->metadata, src->metadata, 0);
338 #if FF_API_ERROR_FRAME
339 FF_DISABLE_DEPRECATION_WARNINGS
340 memcpy(dst->error, src->error, sizeof(dst->error));
341 FF_ENABLE_DEPRECATION_WARNINGS
344 for (i = 0; i < src->nb_side_data; i++) {
345 const AVFrameSideData *sd_src = src->side_data[i];
346 AVFrameSideData *sd_dst;
347 if ( sd_src->type == AV_FRAME_DATA_PANSCAN
348 && (src->width != dst->width || src->height != dst->height))
351 sd_dst = av_frame_new_side_data(dst, sd_src->type,
355 return AVERROR(ENOMEM);
357 memcpy(sd_dst->data, sd_src->data, sd_src->size);
359 sd_dst = frame_new_side_data(dst, sd_src->type, av_buffer_ref(sd_src->buf));
362 return AVERROR(ENOMEM);
365 av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
369 FF_DISABLE_DEPRECATION_WARNINGS
370 dst->qscale_table = NULL;
372 dst->qscale_type = 0;
373 av_buffer_unref(&dst->qp_table_buf);
374 if (src->qp_table_buf) {
375 dst->qp_table_buf = av_buffer_ref(src->qp_table_buf);
376 if (dst->qp_table_buf) {
377 dst->qscale_table = dst->qp_table_buf->data;
378 dst->qstride = src->qstride;
379 dst->qscale_type = src->qscale_type;
382 FF_ENABLE_DEPRECATION_WARNINGS
385 av_buffer_unref(&dst->opaque_ref);
386 av_buffer_unref(&dst->private_ref);
387 if (src->opaque_ref) {
388 dst->opaque_ref = av_buffer_ref(src->opaque_ref);
389 if (!dst->opaque_ref)
390 return AVERROR(ENOMEM);
392 if (src->private_ref) {
393 dst->private_ref = av_buffer_ref(src->private_ref);
394 if (!dst->private_ref)
395 return AVERROR(ENOMEM);
400 int av_frame_ref(AVFrame *dst, const AVFrame *src)
404 av_assert1(dst->width == 0 && dst->height == 0);
405 av_assert1(dst->channels == 0);
407 dst->format = src->format;
408 dst->width = src->width;
409 dst->height = src->height;
410 dst->channels = src->channels;
411 dst->channel_layout = src->channel_layout;
412 dst->nb_samples = src->nb_samples;
414 ret = frame_copy_props(dst, src, 0);
418 /* duplicate the frame data if it's not refcounted */
420 ret = av_frame_get_buffer(dst, 32);
424 ret = av_frame_copy(dst, src);
431 /* ref the buffers */
432 for (i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
435 dst->buf[i] = av_buffer_ref(src->buf[i]);
437 ret = AVERROR(ENOMEM);
442 if (src->extended_buf) {
443 dst->extended_buf = av_mallocz_array(sizeof(*dst->extended_buf),
444 src->nb_extended_buf);
445 if (!dst->extended_buf) {
446 ret = AVERROR(ENOMEM);
449 dst->nb_extended_buf = src->nb_extended_buf;
451 for (i = 0; i < src->nb_extended_buf; i++) {
452 dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
453 if (!dst->extended_buf[i]) {
454 ret = AVERROR(ENOMEM);
460 if (src->hw_frames_ctx) {
461 dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
462 if (!dst->hw_frames_ctx) {
463 ret = AVERROR(ENOMEM);
468 /* duplicate extended data */
469 if (src->extended_data != src->data) {
470 int ch = src->channels;
473 ret = AVERROR(EINVAL);
476 CHECK_CHANNELS_CONSISTENCY(src);
478 dst->extended_data = av_malloc_array(sizeof(*dst->extended_data), ch);
479 if (!dst->extended_data) {
480 ret = AVERROR(ENOMEM);
483 memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
485 dst->extended_data = dst->data;
487 memcpy(dst->data, src->data, sizeof(src->data));
488 memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
497 AVFrame *av_frame_clone(const AVFrame *src)
499 AVFrame *ret = av_frame_alloc();
504 if (av_frame_ref(ret, src) < 0)
510 void av_frame_unref(AVFrame *frame)
517 wipe_side_data(frame);
519 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
520 av_buffer_unref(&frame->buf[i]);
521 for (i = 0; i < frame->nb_extended_buf; i++)
522 av_buffer_unref(&frame->extended_buf[i]);
523 av_freep(&frame->extended_buf);
524 av_dict_free(&frame->metadata);
526 av_buffer_unref(&frame->qp_table_buf);
529 av_buffer_unref(&frame->hw_frames_ctx);
531 av_buffer_unref(&frame->opaque_ref);
532 av_buffer_unref(&frame->private_ref);
534 get_frame_defaults(frame);
537 void av_frame_move_ref(AVFrame *dst, AVFrame *src)
539 av_assert1(dst->width == 0 && dst->height == 0);
540 av_assert1(dst->channels == 0);
543 if (src->extended_data == src->data)
544 dst->extended_data = dst->data;
545 memset(src, 0, sizeof(*src));
546 get_frame_defaults(src);
549 int av_frame_is_writable(AVFrame *frame)
553 /* assume non-refcounted frames are not writable */
557 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
559 ret &= !!av_buffer_is_writable(frame->buf[i]);
560 for (i = 0; i < frame->nb_extended_buf; i++)
561 ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
566 int av_frame_make_writable(AVFrame *frame)
572 return AVERROR(EINVAL);
574 if (av_frame_is_writable(frame))
577 memset(&tmp, 0, sizeof(tmp));
578 tmp.format = frame->format;
579 tmp.width = frame->width;
580 tmp.height = frame->height;
581 tmp.channels = frame->channels;
582 tmp.channel_layout = frame->channel_layout;
583 tmp.nb_samples = frame->nb_samples;
584 ret = av_frame_get_buffer(&tmp, 32);
588 ret = av_frame_copy(&tmp, frame);
590 av_frame_unref(&tmp);
594 ret = av_frame_copy_props(&tmp, frame);
596 av_frame_unref(&tmp);
600 av_frame_unref(frame);
603 if (tmp.data == tmp.extended_data)
604 frame->extended_data = frame->data;
609 int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
611 return frame_copy_props(dst, src, 1);
614 AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
619 if (frame->nb_samples) {
620 int channels = frame->channels;
623 CHECK_CHANNELS_CONSISTENCY(frame);
624 planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
628 if (plane < 0 || plane >= planes || !frame->extended_data[plane])
630 data = frame->extended_data[plane];
632 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
633 AVBufferRef *buf = frame->buf[i];
634 if (data >= buf->data && data < buf->data + buf->size)
637 for (i = 0; i < frame->nb_extended_buf; i++) {
638 AVBufferRef *buf = frame->extended_buf[i];
639 if (data >= buf->data && data < buf->data + buf->size)
645 static AVFrameSideData *frame_new_side_data(AVFrame *frame,
646 enum AVFrameSideDataType type,
649 AVFrameSideData *ret, **tmp;
654 if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
657 tmp = av_realloc(frame->side_data,
658 (frame->nb_side_data + 1) * sizeof(*frame->side_data));
661 frame->side_data = tmp;
663 ret = av_mallocz(sizeof(*ret));
668 ret->data = ret->buf->data;
669 ret->size = buf->size;
672 frame->side_data[frame->nb_side_data++] = ret;
676 av_buffer_unref(&buf);
680 AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
681 enum AVFrameSideDataType type,
685 return frame_new_side_data(frame, type, av_buffer_alloc(size));
688 AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
689 enum AVFrameSideDataType type)
693 for (i = 0; i < frame->nb_side_data; i++) {
694 if (frame->side_data[i]->type == type)
695 return frame->side_data[i];
700 static int frame_copy_video(AVFrame *dst, const AVFrame *src)
702 const uint8_t *src_data[4];
705 if (dst->width < src->width ||
706 dst->height < src->height)
707 return AVERROR(EINVAL);
709 planes = av_pix_fmt_count_planes(dst->format);
710 for (i = 0; i < planes; i++)
711 if (!dst->data[i] || !src->data[i])
712 return AVERROR(EINVAL);
714 memcpy(src_data, src->data, sizeof(src_data));
715 av_image_copy(dst->data, dst->linesize,
716 src_data, src->linesize,
717 dst->format, src->width, src->height);
722 static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
724 int planar = av_sample_fmt_is_planar(dst->format);
725 int channels = dst->channels;
726 int planes = planar ? channels : 1;
729 if (dst->nb_samples != src->nb_samples ||
730 dst->channels != src->channels ||
731 dst->channel_layout != src->channel_layout)
732 return AVERROR(EINVAL);
734 CHECK_CHANNELS_CONSISTENCY(src);
736 for (i = 0; i < planes; i++)
737 if (!dst->extended_data[i] || !src->extended_data[i])
738 return AVERROR(EINVAL);
740 av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
741 dst->nb_samples, channels, dst->format);
746 int av_frame_copy(AVFrame *dst, const AVFrame *src)
748 if (dst->format != src->format || dst->format < 0)
749 return AVERROR(EINVAL);
751 if (dst->width > 0 && dst->height > 0)
752 return frame_copy_video(dst, src);
753 else if (dst->nb_samples > 0 && dst->channels > 0)
754 return frame_copy_audio(dst, src);
756 return AVERROR(EINVAL);
759 void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
763 for (i = 0; i < frame->nb_side_data; i++) {
764 AVFrameSideData *sd = frame->side_data[i];
765 if (sd->type == type) {
766 free_side_data(&frame->side_data[i]);
767 frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
768 frame->nb_side_data--;
773 const char *av_frame_side_data_name(enum AVFrameSideDataType type)
776 case AV_FRAME_DATA_PANSCAN: return "AVPanScan";
777 case AV_FRAME_DATA_A53_CC: return "ATSC A53 Part 4 Closed Captions";
778 case AV_FRAME_DATA_STEREO3D: return "Stereoscopic 3d metadata";
779 case AV_FRAME_DATA_MATRIXENCODING: return "AVMatrixEncoding";
780 case AV_FRAME_DATA_DOWNMIX_INFO: return "Metadata relevant to a downmix procedure";
781 case AV_FRAME_DATA_REPLAYGAIN: return "AVReplayGain";
782 case AV_FRAME_DATA_DISPLAYMATRIX: return "3x3 displaymatrix";
783 case AV_FRAME_DATA_AFD: return "Active format description";
784 case AV_FRAME_DATA_MOTION_VECTORS: return "Motion vectors";
785 case AV_FRAME_DATA_SKIP_SAMPLES: return "Skip samples";
786 case AV_FRAME_DATA_AUDIO_SERVICE_TYPE: return "Audio service type";
787 case AV_FRAME_DATA_MASTERING_DISPLAY_METADATA: return "Mastering display metadata";
788 case AV_FRAME_DATA_CONTENT_LIGHT_LEVEL: return "Content light level metadata";
789 case AV_FRAME_DATA_GOP_TIMECODE: return "GOP timecode";
790 case AV_FRAME_DATA_ICC_PROFILE: return "ICC profile";
795 static int calc_cropping_offsets(size_t offsets[4], const AVFrame *frame,
796 const AVPixFmtDescriptor *desc)
800 for (i = 0; frame->data[i]; i++) {
801 const AVComponentDescriptor *comp = NULL;
802 int shift_x = (i == 1 || i == 2) ? desc->log2_chroma_w : 0;
803 int shift_y = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
805 if (desc->flags & (AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_PSEUDOPAL) && i == 1) {
810 /* find any component descriptor for this plane */
811 for (j = 0; j < desc->nb_components; j++) {
812 if (desc->comp[j].plane == i) {
813 comp = &desc->comp[j];
820 offsets[i] = (frame->crop_top >> shift_y) * frame->linesize[i] +
821 (frame->crop_left >> shift_x) * comp->step;
827 int av_frame_apply_cropping(AVFrame *frame, int flags)
829 const AVPixFmtDescriptor *desc;
833 if (!(frame->width > 0 && frame->height > 0))
834 return AVERROR(EINVAL);
836 if (frame->crop_left >= INT_MAX - frame->crop_right ||
837 frame->crop_top >= INT_MAX - frame->crop_bottom ||
838 (frame->crop_left + frame->crop_right) >= frame->width ||
839 (frame->crop_top + frame->crop_bottom) >= frame->height)
840 return AVERROR(ERANGE);
842 desc = av_pix_fmt_desc_get(frame->format);
846 /* Apply just the right/bottom cropping for hwaccel formats. Bitstream
847 * formats cannot be easily handled here either (and corresponding decoders
848 * should not export any cropping anyway), so do the same for those as well.
850 if (desc->flags & (AV_PIX_FMT_FLAG_BITSTREAM | AV_PIX_FMT_FLAG_HWACCEL)) {
851 frame->width -= frame->crop_right;
852 frame->height -= frame->crop_bottom;
853 frame->crop_right = 0;
854 frame->crop_bottom = 0;
858 /* calculate the offsets for each plane */
859 calc_cropping_offsets(offsets, frame, desc);
861 /* adjust the offsets to avoid breaking alignment */
862 if (!(flags & AV_FRAME_CROP_UNALIGNED)) {
863 int log2_crop_align = frame->crop_left ? ff_ctz(frame->crop_left) : INT_MAX;
864 int min_log2_align = INT_MAX;
866 for (i = 0; frame->data[i]; i++) {
867 int log2_align = offsets[i] ? ff_ctz(offsets[i]) : INT_MAX;
868 min_log2_align = FFMIN(log2_align, min_log2_align);
871 /* we assume, and it should always be true, that the data alignment is
872 * related to the cropping alignment by a constant power-of-2 factor */
873 if (log2_crop_align < min_log2_align)
876 if (min_log2_align < 5) {
877 frame->crop_left &= ~((1 << (5 + log2_crop_align - min_log2_align)) - 1);
878 calc_cropping_offsets(offsets, frame, desc);
882 for (i = 0; frame->data[i]; i++)
883 frame->data[i] += offsets[i];
885 frame->width -= (frame->crop_left + frame->crop_right);
886 frame->height -= (frame->crop_top + frame->crop_bottom);
887 frame->crop_left = 0;
888 frame->crop_right = 0;
890 frame->crop_bottom = 0;