3 * This file is part of Libav.
5 * Libav is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * Libav is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with Libav; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #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,
107 for (i = 0; i < 4 && frame->linesize[i]; i++)
108 frame->linesize[i] = FFALIGN(frame->linesize[i], align);
111 for (i = 0; i < 4 && frame->linesize[i]; i++) {
112 int h = frame->height;
113 if (i == 1 || i == 2)
114 h = -((-h) >> desc->log2_chroma_h);
116 frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h);
120 frame->data[i] = frame->buf[i]->data;
122 if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
123 av_buffer_unref(&frame->buf[1]);
124 frame->buf[1] = av_buffer_alloc(1024);
127 frame->data[1] = frame->buf[1]->data;
130 frame->extended_data = frame->data;
134 av_frame_unref(frame);
135 return AVERROR(ENOMEM);
138 static int get_audio_buffer(AVFrame *frame, int align)
140 int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
141 int planar = av_sample_fmt_is_planar(frame->format);
142 int planes = planar ? channels : 1;
145 if (!frame->linesize[0]) {
146 ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
147 frame->nb_samples, frame->format,
153 if (planes > AV_NUM_DATA_POINTERS) {
154 frame->extended_data = av_mallocz(planes *
155 sizeof(*frame->extended_data));
156 frame->extended_buf = av_mallocz((planes - AV_NUM_DATA_POINTERS) *
157 sizeof(*frame->extended_buf));
158 if (!frame->extended_data || !frame->extended_buf) {
159 av_freep(&frame->extended_data);
160 av_freep(&frame->extended_buf);
161 return AVERROR(ENOMEM);
163 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
165 frame->extended_data = frame->data;
167 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
168 frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
169 if (!frame->buf[i]) {
170 av_frame_unref(frame);
171 return AVERROR(ENOMEM);
173 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
175 for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
176 frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
177 if (!frame->extended_buf[i]) {
178 av_frame_unref(frame);
179 return AVERROR(ENOMEM);
181 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
187 int av_frame_get_buffer(AVFrame *frame, int align)
189 if (frame->format < 0)
190 return AVERROR(EINVAL);
192 if (frame->width > 0 && frame->height > 0)
193 return get_video_buffer(frame, align);
194 else if (frame->nb_samples > 0 && frame->channel_layout)
195 return get_audio_buffer(frame, align);
197 return AVERROR(EINVAL);
200 int av_frame_ref(AVFrame *dst, const AVFrame *src)
204 dst->format = src->format;
205 dst->width = src->width;
206 dst->height = src->height;
207 dst->channel_layout = src->channel_layout;
208 dst->nb_samples = src->nb_samples;
210 ret = av_frame_copy_props(dst, src);
214 /* duplicate the frame data if it's not refcounted */
216 ret = av_frame_get_buffer(dst, 32);
220 ret = av_frame_copy(dst, src);
227 /* ref the buffers */
228 for (i = 0; i < FF_ARRAY_ELEMS(src->buf) && src->buf[i]; i++) {
229 dst->buf[i] = av_buffer_ref(src->buf[i]);
231 ret = AVERROR(ENOMEM);
236 if (src->extended_buf) {
237 dst->extended_buf = av_mallocz(sizeof(*dst->extended_buf) *
238 src->nb_extended_buf);
239 if (!dst->extended_buf) {
240 ret = AVERROR(ENOMEM);
243 dst->nb_extended_buf = src->nb_extended_buf;
245 for (i = 0; i < src->nb_extended_buf; i++) {
246 dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
247 if (!dst->extended_buf[i]) {
248 ret = AVERROR(ENOMEM);
254 /* duplicate extended data */
255 if (src->extended_data != src->data) {
256 int ch = av_get_channel_layout_nb_channels(src->channel_layout);
259 ret = AVERROR(EINVAL);
263 dst->extended_data = av_malloc(sizeof(*dst->extended_data) * ch);
264 if (!dst->extended_data) {
265 ret = AVERROR(ENOMEM);
268 memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
270 dst->extended_data = dst->data;
272 memcpy(dst->data, src->data, sizeof(src->data));
273 memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
282 AVFrame *av_frame_clone(const AVFrame *src)
284 AVFrame *ret = av_frame_alloc();
289 if (av_frame_ref(ret, src) < 0)
295 void av_frame_unref(AVFrame *frame)
299 wipe_side_data(frame);
301 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
302 av_buffer_unref(&frame->buf[i]);
303 for (i = 0; i < frame->nb_extended_buf; i++)
304 av_buffer_unref(&frame->extended_buf[i]);
305 av_freep(&frame->extended_buf);
306 get_frame_defaults(frame);
309 void av_frame_move_ref(AVFrame *dst, AVFrame *src)
312 if (src->extended_data == src->data)
313 dst->extended_data = dst->data;
314 memset(src, 0, sizeof(*src));
315 get_frame_defaults(src);
318 int av_frame_is_writable(AVFrame *frame)
322 /* assume non-refcounted frames are not writable */
326 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++)
327 ret &= !!av_buffer_is_writable(frame->buf[i]);
328 for (i = 0; i < frame->nb_extended_buf; i++)
329 ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
334 int av_frame_make_writable(AVFrame *frame)
340 return AVERROR(EINVAL);
342 if (av_frame_is_writable(frame))
345 memset(&tmp, 0, sizeof(tmp));
346 tmp.format = frame->format;
347 tmp.width = frame->width;
348 tmp.height = frame->height;
349 tmp.channel_layout = frame->channel_layout;
350 tmp.nb_samples = frame->nb_samples;
351 ret = av_frame_get_buffer(&tmp, 32);
355 ret = av_frame_copy(&tmp, frame);
357 av_frame_unref(&tmp);
361 ret = av_frame_copy_props(&tmp, frame);
363 av_frame_unref(&tmp);
367 av_frame_unref(frame);
370 if (tmp.data == tmp.extended_data)
371 frame->extended_data = frame->data;
376 int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
380 dst->key_frame = src->key_frame;
381 dst->pict_type = src->pict_type;
382 dst->sample_aspect_ratio = src->sample_aspect_ratio;
384 dst->repeat_pict = src->repeat_pict;
385 dst->interlaced_frame = src->interlaced_frame;
386 dst->top_field_first = src->top_field_first;
387 dst->palette_has_changed = src->palette_has_changed;
388 dst->sample_rate = src->sample_rate;
389 dst->opaque = src->opaque;
390 dst->pkt_pts = src->pkt_pts;
391 dst->pkt_dts = src->pkt_dts;
392 dst->reordered_opaque = src->reordered_opaque;
393 dst->quality = src->quality;
394 dst->coded_picture_number = src->coded_picture_number;
395 dst->display_picture_number = src->display_picture_number;
396 dst->flags = src->flags;
397 dst->color_primaries = src->color_primaries;
398 dst->color_trc = src->color_trc;
399 dst->colorspace = src->colorspace;
400 dst->color_range = src->color_range;
401 dst->chroma_location = src->chroma_location;
403 #if FF_API_ERROR_FRAME
404 FF_DISABLE_DEPRECATION_WARNINGS
405 memcpy(dst->error, src->error, sizeof(dst->error));
406 FF_ENABLE_DEPRECATION_WARNINGS
409 for (i = 0; i < src->nb_side_data; i++) {
410 const AVFrameSideData *sd_src = src->side_data[i];
411 AVFrameSideData *sd_dst = av_frame_new_side_data(dst, sd_src->type,
415 return AVERROR(ENOMEM);
417 memcpy(sd_dst->data, sd_src->data, sd_src->size);
418 av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
424 AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
429 if (frame->nb_samples) {
430 int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
433 planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
437 if (plane < 0 || plane >= planes || !frame->extended_data[plane])
439 data = frame->extended_data[plane];
441 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
442 AVBufferRef *buf = frame->buf[i];
443 if (data >= buf->data && data < buf->data + buf->size)
446 for (i = 0; i < frame->nb_extended_buf; i++) {
447 AVBufferRef *buf = frame->extended_buf[i];
448 if (data >= buf->data && data < buf->data + buf->size)
454 AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
455 enum AVFrameSideDataType type,
458 AVFrameSideData *ret, **tmp;
460 if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
463 tmp = av_realloc(frame->side_data,
464 (frame->nb_side_data + 1) * sizeof(*frame->side_data));
467 frame->side_data = tmp;
469 ret = av_mallocz(sizeof(*ret));
473 ret->data = av_malloc(size);
482 frame->side_data[frame->nb_side_data++] = ret;
487 AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
488 enum AVFrameSideDataType type)
492 for (i = 0; i < frame->nb_side_data; i++) {
493 if (frame->side_data[i]->type == type)
494 return frame->side_data[i];
499 static int frame_copy_video(AVFrame *dst, const AVFrame *src)
501 const uint8_t *src_data[4];
504 if (dst->width != src->width ||
505 dst->height != src->height)
506 return AVERROR(EINVAL);
508 planes = av_pix_fmt_count_planes(dst->format);
509 for (i = 0; i < planes; i++)
510 if (!dst->data[i] || !src->data[i])
511 return AVERROR(EINVAL);
513 memcpy(src_data, src->data, sizeof(src_data));
514 av_image_copy(dst->data, dst->linesize,
515 src_data, src->linesize,
516 dst->format, dst->width, dst->height);
521 static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
523 int planar = av_sample_fmt_is_planar(dst->format);
524 int channels = av_get_channel_layout_nb_channels(dst->channel_layout);
525 int planes = planar ? channels : 1;
528 if (dst->nb_samples != src->nb_samples ||
529 dst->channel_layout != src->channel_layout)
530 return AVERROR(EINVAL);
532 for (i = 0; i < planes; i++)
533 if (!dst->extended_data[i] || !src->extended_data[i])
534 return AVERROR(EINVAL);
536 av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
537 dst->nb_samples, channels, dst->format);
542 int av_frame_copy(AVFrame *dst, const AVFrame *src)
544 if (dst->format != src->format || dst->format < 0)
545 return AVERROR(EINVAL);
547 if (dst->width > 0 && dst->height > 0)
548 return frame_copy_video(dst, src);
549 else if (dst->nb_samples > 0 && dst->channel_layout)
550 return frame_copy_audio(dst, src);
552 return AVERROR(EINVAL);
555 void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
559 for (i = 0; i < frame->nb_side_data; i++) {
560 AVFrameSideData *sd = frame->side_data[i];
561 if (sd->type == type) {
562 free_side_data(&frame->side_data[i]);
563 frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
564 frame->nb_side_data--;