2 * This file is part of Libav.
4 * Libav 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 * Libav 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 Libav; 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 static void get_frame_defaults(AVFrame *frame)
31 if (frame->extended_data != frame->data)
32 av_freep(&frame->extended_data);
34 memset(frame, 0, sizeof(*frame));
36 frame->pts = AV_NOPTS_VALUE;
38 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
39 frame->format = -1; /* unknown */
40 frame->extended_data = frame->data;
41 frame->color_primaries = AVCOL_PRI_UNSPECIFIED;
42 frame->color_trc = AVCOL_TRC_UNSPECIFIED;
43 frame->colorspace = AVCOL_SPC_UNSPECIFIED;
44 frame->color_range = AVCOL_RANGE_UNSPECIFIED;
45 frame->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
48 static void free_side_data(AVFrameSideData **ptr_sd)
50 AVFrameSideData *sd = *ptr_sd;
53 av_dict_free(&sd->metadata);
57 static void wipe_side_data(AVFrame *frame)
61 for (i = 0; i < frame->nb_side_data; i++) {
62 free_side_data(&frame->side_data[i]);
64 frame->nb_side_data = 0;
66 av_freep(&frame->side_data);
69 AVFrame *av_frame_alloc(void)
71 AVFrame *frame = av_mallocz(sizeof(*frame));
76 get_frame_defaults(frame);
81 void av_frame_free(AVFrame **frame)
83 if (!frame || !*frame)
86 av_frame_unref(*frame);
90 static int get_video_buffer(AVFrame *frame, int align)
92 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
96 return AVERROR(EINVAL);
98 if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
101 if (!frame->linesize[0]) {
102 ret = av_image_fill_linesizes(frame->linesize, frame->format,
108 align = av_cpu_max_align();
110 for (i = 0; i < 4 && frame->linesize[i]; i++)
111 frame->linesize[i] = FFALIGN(frame->linesize[i], align);
114 for (i = 0; i < 4 && frame->linesize[i]; i++) {
115 int h = frame->height;
116 if (i == 1 || i == 2)
117 h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
119 frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h);
123 frame->data[i] = frame->buf[i]->data;
125 if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
126 av_buffer_unref(&frame->buf[1]);
127 frame->buf[1] = av_buffer_alloc(1024);
130 frame->data[1] = frame->buf[1]->data;
133 frame->extended_data = frame->data;
137 av_frame_unref(frame);
138 return AVERROR(ENOMEM);
141 static int get_audio_buffer(AVFrame *frame, int align)
143 int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
144 int planar = av_sample_fmt_is_planar(frame->format);
145 int planes = planar ? channels : 1;
148 if (!frame->linesize[0]) {
149 ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
150 frame->nb_samples, frame->format,
156 if (planes > AV_NUM_DATA_POINTERS) {
157 frame->extended_data = av_mallocz(planes *
158 sizeof(*frame->extended_data));
159 frame->extended_buf = av_mallocz((planes - AV_NUM_DATA_POINTERS) *
160 sizeof(*frame->extended_buf));
161 if (!frame->extended_data || !frame->extended_buf) {
162 av_freep(&frame->extended_data);
163 av_freep(&frame->extended_buf);
164 return AVERROR(ENOMEM);
166 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
168 frame->extended_data = frame->data;
170 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
171 frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
172 if (!frame->buf[i]) {
173 av_frame_unref(frame);
174 return AVERROR(ENOMEM);
176 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
178 for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
179 frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
180 if (!frame->extended_buf[i]) {
181 av_frame_unref(frame);
182 return AVERROR(ENOMEM);
184 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
190 int av_frame_get_buffer(AVFrame *frame, int align)
192 if (frame->format < 0)
193 return AVERROR(EINVAL);
195 if (frame->width > 0 && frame->height > 0)
196 return get_video_buffer(frame, align);
197 else if (frame->nb_samples > 0 && frame->channel_layout)
198 return get_audio_buffer(frame, align);
200 return AVERROR(EINVAL);
203 int av_frame_ref(AVFrame *dst, const AVFrame *src)
207 dst->format = src->format;
208 dst->width = src->width;
209 dst->height = src->height;
210 dst->channel_layout = src->channel_layout;
211 dst->nb_samples = src->nb_samples;
213 ret = av_frame_copy_props(dst, src);
217 /* duplicate the frame data if it's not refcounted */
219 ret = av_frame_get_buffer(dst, 32);
223 ret = av_frame_copy(dst, src);
230 /* ref the buffers */
231 for (i = 0; i < FF_ARRAY_ELEMS(src->buf) && src->buf[i]; i++) {
232 dst->buf[i] = av_buffer_ref(src->buf[i]);
234 ret = AVERROR(ENOMEM);
239 if (src->extended_buf) {
240 dst->extended_buf = av_mallocz(sizeof(*dst->extended_buf) *
241 src->nb_extended_buf);
242 if (!dst->extended_buf) {
243 ret = AVERROR(ENOMEM);
246 dst->nb_extended_buf = src->nb_extended_buf;
248 for (i = 0; i < src->nb_extended_buf; i++) {
249 dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
250 if (!dst->extended_buf[i]) {
251 ret = AVERROR(ENOMEM);
257 if (src->hw_frames_ctx) {
258 dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
259 if (!dst->hw_frames_ctx) {
260 ret = AVERROR(ENOMEM);
265 /* duplicate extended data */
266 if (src->extended_data != src->data) {
267 int ch = av_get_channel_layout_nb_channels(src->channel_layout);
270 ret = AVERROR(EINVAL);
274 dst->extended_data = av_malloc(sizeof(*dst->extended_data) * ch);
275 if (!dst->extended_data) {
276 ret = AVERROR(ENOMEM);
279 memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
281 dst->extended_data = dst->data;
283 memcpy(dst->data, src->data, sizeof(src->data));
284 memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
293 AVFrame *av_frame_clone(const AVFrame *src)
295 AVFrame *ret = av_frame_alloc();
300 if (av_frame_ref(ret, src) < 0)
306 void av_frame_unref(AVFrame *frame)
310 wipe_side_data(frame);
312 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
313 av_buffer_unref(&frame->buf[i]);
314 for (i = 0; i < frame->nb_extended_buf; i++)
315 av_buffer_unref(&frame->extended_buf[i]);
316 av_freep(&frame->extended_buf);
318 av_buffer_unref(&frame->hw_frames_ctx);
320 get_frame_defaults(frame);
323 void av_frame_move_ref(AVFrame *dst, AVFrame *src)
326 if (src->extended_data == src->data)
327 dst->extended_data = dst->data;
328 memset(src, 0, sizeof(*src));
329 get_frame_defaults(src);
332 int av_frame_is_writable(AVFrame *frame)
336 /* assume non-refcounted frames are not writable */
340 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++)
341 ret &= !!av_buffer_is_writable(frame->buf[i]);
342 for (i = 0; i < frame->nb_extended_buf; i++)
343 ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
348 int av_frame_make_writable(AVFrame *frame)
354 return AVERROR(EINVAL);
356 if (av_frame_is_writable(frame))
359 memset(&tmp, 0, sizeof(tmp));
360 tmp.format = frame->format;
361 tmp.width = frame->width;
362 tmp.height = frame->height;
363 tmp.channel_layout = frame->channel_layout;
364 tmp.nb_samples = frame->nb_samples;
365 ret = av_frame_get_buffer(&tmp, 32);
369 ret = av_frame_copy(&tmp, frame);
371 av_frame_unref(&tmp);
375 ret = av_frame_copy_props(&tmp, frame);
377 av_frame_unref(&tmp);
381 av_frame_unref(frame);
384 if (tmp.data == tmp.extended_data)
385 frame->extended_data = frame->data;
390 int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
394 dst->key_frame = src->key_frame;
395 dst->pict_type = src->pict_type;
396 dst->sample_aspect_ratio = src->sample_aspect_ratio;
397 dst->crop_top = src->crop_top;
398 dst->crop_bottom = src->crop_bottom;
399 dst->crop_left = src->crop_left;
400 dst->crop_right = src->crop_right;
402 dst->repeat_pict = src->repeat_pict;
403 dst->interlaced_frame = src->interlaced_frame;
404 dst->top_field_first = src->top_field_first;
405 dst->palette_has_changed = src->palette_has_changed;
406 dst->sample_rate = src->sample_rate;
407 dst->opaque = src->opaque;
409 FF_DISABLE_DEPRECATION_WARNINGS
410 dst->pkt_pts = src->pkt_pts;
411 FF_ENABLE_DEPRECATION_WARNINGS
413 dst->pkt_dts = src->pkt_dts;
414 dst->reordered_opaque = src->reordered_opaque;
415 dst->quality = src->quality;
416 dst->coded_picture_number = src->coded_picture_number;
417 dst->display_picture_number = src->display_picture_number;
418 dst->flags = src->flags;
419 dst->color_primaries = src->color_primaries;
420 dst->color_trc = src->color_trc;
421 dst->colorspace = src->colorspace;
422 dst->color_range = src->color_range;
423 dst->chroma_location = src->chroma_location;
425 #if FF_API_ERROR_FRAME
426 FF_DISABLE_DEPRECATION_WARNINGS
427 memcpy(dst->error, src->error, sizeof(dst->error));
428 FF_ENABLE_DEPRECATION_WARNINGS
431 for (i = 0; i < src->nb_side_data; i++) {
432 const AVFrameSideData *sd_src = src->side_data[i];
433 AVFrameSideData *sd_dst = av_frame_new_side_data(dst, sd_src->type,
437 return AVERROR(ENOMEM);
439 memcpy(sd_dst->data, sd_src->data, sd_src->size);
440 av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
446 AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
451 if (frame->nb_samples) {
452 int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
455 planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
459 if (plane < 0 || plane >= planes || !frame->extended_data[plane])
461 data = frame->extended_data[plane];
463 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
464 AVBufferRef *buf = frame->buf[i];
465 if (data >= buf->data && data < buf->data + buf->size)
468 for (i = 0; i < frame->nb_extended_buf; i++) {
469 AVBufferRef *buf = frame->extended_buf[i];
470 if (data >= buf->data && data < buf->data + buf->size)
476 AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
477 enum AVFrameSideDataType type,
480 AVFrameSideData *ret, **tmp;
482 if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
485 tmp = av_realloc(frame->side_data,
486 (frame->nb_side_data + 1) * sizeof(*frame->side_data));
489 frame->side_data = tmp;
491 ret = av_mallocz(sizeof(*ret));
495 ret->data = av_malloc(size);
504 frame->side_data[frame->nb_side_data++] = ret;
509 AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
510 enum AVFrameSideDataType type)
514 for (i = 0; i < frame->nb_side_data; i++) {
515 if (frame->side_data[i]->type == type)
516 return frame->side_data[i];
521 static int frame_copy_video(AVFrame *dst, const AVFrame *src)
523 const uint8_t *src_data[4];
526 if (dst->width != src->width ||
527 dst->height != src->height)
528 return AVERROR(EINVAL);
530 planes = av_pix_fmt_count_planes(dst->format);
531 for (i = 0; i < planes; i++)
532 if (!dst->data[i] || !src->data[i])
533 return AVERROR(EINVAL);
535 memcpy(src_data, src->data, sizeof(src_data));
536 av_image_copy(dst->data, dst->linesize,
537 src_data, src->linesize,
538 dst->format, dst->width, dst->height);
543 static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
545 int planar = av_sample_fmt_is_planar(dst->format);
546 int channels = av_get_channel_layout_nb_channels(dst->channel_layout);
547 int planes = planar ? channels : 1;
550 if (dst->nb_samples != src->nb_samples ||
551 dst->channel_layout != src->channel_layout)
552 return AVERROR(EINVAL);
554 for (i = 0; i < planes; i++)
555 if (!dst->extended_data[i] || !src->extended_data[i])
556 return AVERROR(EINVAL);
558 av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
559 dst->nb_samples, channels, dst->format);
564 int av_frame_copy(AVFrame *dst, const AVFrame *src)
566 if (dst->format != src->format || dst->format < 0)
567 return AVERROR(EINVAL);
569 if (dst->width > 0 && dst->height > 0)
570 return frame_copy_video(dst, src);
571 else if (dst->nb_samples > 0 && dst->channel_layout)
572 return frame_copy_audio(dst, src);
574 return AVERROR(EINVAL);
577 void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
581 for (i = 0; i < frame->nb_side_data; i++) {
582 AVFrameSideData *sd = frame->side_data[i];
583 if (sd->type == type) {
584 free_side_data(&frame->side_data[i]);
585 frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
586 frame->nb_side_data--;