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 #if FF_API_AVFRAME_COLORSPACE
42 frame->color_primaries = AVCOL_PRI_UNSPECIFIED;
43 frame->color_trc = AVCOL_TRC_UNSPECIFIED;
44 frame->colorspace = AVCOL_SPC_UNSPECIFIED;
45 frame->color_range = AVCOL_RANGE_UNSPECIFIED;
46 frame->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
50 AVFrame *av_frame_alloc(void)
52 AVFrame *frame = av_mallocz(sizeof(*frame));
57 get_frame_defaults(frame);
62 void av_frame_free(AVFrame **frame)
64 if (!frame || !*frame)
67 av_frame_unref(*frame);
71 static int get_video_buffer(AVFrame *frame, int align)
73 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
77 return AVERROR(EINVAL);
79 if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
82 if (!frame->linesize[0]) {
83 ret = av_image_fill_linesizes(frame->linesize, frame->format,
88 for (i = 0; i < 4 && frame->linesize[i]; i++)
89 frame->linesize[i] = FFALIGN(frame->linesize[i], align);
92 for (i = 0; i < 4 && frame->linesize[i]; i++) {
93 int h = frame->height;
95 h = -((-h) >> desc->log2_chroma_h);
97 frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h);
101 frame->data[i] = frame->buf[i]->data;
103 if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
104 av_buffer_unref(&frame->buf[1]);
105 frame->buf[1] = av_buffer_alloc(1024);
108 frame->data[1] = frame->buf[1]->data;
111 frame->extended_data = frame->data;
115 av_frame_unref(frame);
116 return AVERROR(ENOMEM);
119 static int get_audio_buffer(AVFrame *frame, int align)
121 int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
122 int planar = av_sample_fmt_is_planar(frame->format);
123 int planes = planar ? channels : 1;
126 if (!frame->linesize[0]) {
127 ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
128 frame->nb_samples, frame->format,
134 if (planes > AV_NUM_DATA_POINTERS) {
135 frame->extended_data = av_mallocz(planes *
136 sizeof(*frame->extended_data));
137 frame->extended_buf = av_mallocz((planes - AV_NUM_DATA_POINTERS) *
138 sizeof(*frame->extended_buf));
139 if (!frame->extended_data || !frame->extended_buf) {
140 av_freep(&frame->extended_data);
141 av_freep(&frame->extended_buf);
142 return AVERROR(ENOMEM);
144 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
146 frame->extended_data = frame->data;
148 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
149 frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
150 if (!frame->buf[i]) {
151 av_frame_unref(frame);
152 return AVERROR(ENOMEM);
154 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
156 for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
157 frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
158 if (!frame->extended_buf[i]) {
159 av_frame_unref(frame);
160 return AVERROR(ENOMEM);
162 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
168 int av_frame_get_buffer(AVFrame *frame, int align)
170 if (frame->format < 0)
171 return AVERROR(EINVAL);
173 if (frame->width > 0 && frame->height > 0)
174 return get_video_buffer(frame, align);
175 else if (frame->nb_samples > 0 && frame->channel_layout)
176 return get_audio_buffer(frame, align);
178 return AVERROR(EINVAL);
181 int av_frame_ref(AVFrame *dst, const AVFrame *src)
185 dst->format = src->format;
186 dst->width = src->width;
187 dst->height = src->height;
188 dst->channel_layout = src->channel_layout;
189 dst->nb_samples = src->nb_samples;
191 ret = av_frame_copy_props(dst, src);
195 /* duplicate the frame data if it's not refcounted */
197 ret = av_frame_get_buffer(dst, 32);
201 ret = av_frame_copy(dst, src);
208 /* ref the buffers */
209 for (i = 0; i < FF_ARRAY_ELEMS(src->buf) && src->buf[i]; i++) {
210 dst->buf[i] = av_buffer_ref(src->buf[i]);
212 ret = AVERROR(ENOMEM);
217 if (src->extended_buf) {
218 dst->extended_buf = av_mallocz(sizeof(*dst->extended_buf) *
219 src->nb_extended_buf);
220 if (!dst->extended_buf) {
221 ret = AVERROR(ENOMEM);
224 dst->nb_extended_buf = src->nb_extended_buf;
226 for (i = 0; i < src->nb_extended_buf; i++) {
227 dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
228 if (!dst->extended_buf[i]) {
229 ret = AVERROR(ENOMEM);
235 /* duplicate extended data */
236 if (src->extended_data != src->data) {
237 int ch = av_get_channel_layout_nb_channels(src->channel_layout);
240 ret = AVERROR(EINVAL);
244 dst->extended_data = av_malloc(sizeof(*dst->extended_data) * ch);
245 if (!dst->extended_data) {
246 ret = AVERROR(ENOMEM);
249 memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
251 dst->extended_data = dst->data;
253 memcpy(dst->data, src->data, sizeof(src->data));
254 memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
263 AVFrame *av_frame_clone(const AVFrame *src)
265 AVFrame *ret = av_frame_alloc();
270 if (av_frame_ref(ret, src) < 0)
276 void av_frame_unref(AVFrame *frame)
280 for (i = 0; i < frame->nb_side_data; i++) {
281 av_freep(&frame->side_data[i]->data);
282 av_dict_free(&frame->side_data[i]->metadata);
283 av_freep(&frame->side_data[i]);
285 av_freep(&frame->side_data);
287 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
288 av_buffer_unref(&frame->buf[i]);
289 for (i = 0; i < frame->nb_extended_buf; i++)
290 av_buffer_unref(&frame->extended_buf[i]);
291 av_freep(&frame->extended_buf);
292 get_frame_defaults(frame);
295 void av_frame_move_ref(AVFrame *dst, AVFrame *src)
298 if (src->extended_data == src->data)
299 dst->extended_data = dst->data;
300 memset(src, 0, sizeof(*src));
301 get_frame_defaults(src);
304 int av_frame_is_writable(AVFrame *frame)
308 /* assume non-refcounted frames are not writable */
312 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++)
313 ret &= !!av_buffer_is_writable(frame->buf[i]);
314 for (i = 0; i < frame->nb_extended_buf; i++)
315 ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
320 int av_frame_make_writable(AVFrame *frame)
326 return AVERROR(EINVAL);
328 if (av_frame_is_writable(frame))
331 memset(&tmp, 0, sizeof(tmp));
332 tmp.format = frame->format;
333 tmp.width = frame->width;
334 tmp.height = frame->height;
335 tmp.channel_layout = frame->channel_layout;
336 tmp.nb_samples = frame->nb_samples;
337 ret = av_frame_get_buffer(&tmp, 32);
341 ret = av_frame_copy(&tmp, frame);
343 av_frame_unref(&tmp);
347 ret = av_frame_copy_props(&tmp, frame);
349 av_frame_unref(&tmp);
353 av_frame_unref(frame);
356 if (tmp.data == tmp.extended_data)
357 frame->extended_data = frame->data;
362 int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
366 dst->key_frame = src->key_frame;
367 dst->pict_type = src->pict_type;
368 dst->sample_aspect_ratio = src->sample_aspect_ratio;
370 dst->repeat_pict = src->repeat_pict;
371 dst->interlaced_frame = src->interlaced_frame;
372 dst->top_field_first = src->top_field_first;
373 dst->palette_has_changed = src->palette_has_changed;
374 dst->sample_rate = src->sample_rate;
375 dst->opaque = src->opaque;
376 dst->pkt_pts = src->pkt_pts;
377 dst->pkt_dts = src->pkt_dts;
378 dst->reordered_opaque = src->reordered_opaque;
379 dst->quality = src->quality;
380 dst->coded_picture_number = src->coded_picture_number;
381 dst->display_picture_number = src->display_picture_number;
382 dst->flags = src->flags;
383 #if FF_API_AVFRAME_COLORSPACE
384 dst->color_primaries = src->color_primaries;
385 dst->color_trc = src->color_trc;
386 dst->colorspace = src->colorspace;
387 dst->color_range = src->color_range;
388 dst->chroma_location = src->chroma_location;
391 memcpy(dst->error, src->error, sizeof(dst->error));
393 for (i = 0; i < src->nb_side_data; i++) {
394 const AVFrameSideData *sd_src = src->side_data[i];
395 AVFrameSideData *sd_dst = av_frame_new_side_data(dst, sd_src->type,
398 for (i = 0; i < dst->nb_side_data; i++) {
399 av_freep(&dst->side_data[i]->data);
400 av_dict_free(&dst->side_data[i]->metadata);
401 av_freep(&dst->side_data[i]);
403 av_freep(&dst->side_data);
404 return AVERROR(ENOMEM);
406 memcpy(sd_dst->data, sd_src->data, sd_src->size);
407 av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
413 AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
418 if (frame->nb_samples) {
419 int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
422 planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
426 if (plane < 0 || plane >= planes || !frame->extended_data[plane])
428 data = frame->extended_data[plane];
430 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
431 AVBufferRef *buf = frame->buf[i];
432 if (data >= buf->data && data < buf->data + buf->size)
435 for (i = 0; i < frame->nb_extended_buf; i++) {
436 AVBufferRef *buf = frame->extended_buf[i];
437 if (data >= buf->data && data < buf->data + buf->size)
443 AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
444 enum AVFrameSideDataType type,
447 AVFrameSideData *ret, **tmp;
449 if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
452 tmp = av_realloc(frame->side_data,
453 (frame->nb_side_data + 1) * sizeof(*frame->side_data));
456 frame->side_data = tmp;
458 ret = av_mallocz(sizeof(*ret));
462 ret->data = av_malloc(size);
471 frame->side_data[frame->nb_side_data++] = ret;
476 AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
477 enum AVFrameSideDataType type)
481 for (i = 0; i < frame->nb_side_data; i++) {
482 if (frame->side_data[i]->type == type)
483 return frame->side_data[i];
488 static int frame_copy_video(AVFrame *dst, const AVFrame *src)
490 const uint8_t *src_data[4];
493 if (dst->width != src->width ||
494 dst->height != src->height)
495 return AVERROR(EINVAL);
497 planes = av_pix_fmt_count_planes(dst->format);
498 for (i = 0; i < planes; i++)
499 if (!dst->data[i] || !src->data[i])
500 return AVERROR(EINVAL);
502 memcpy(src_data, src->data, sizeof(src_data));
503 av_image_copy(dst->data, dst->linesize,
504 src_data, src->linesize,
505 dst->format, dst->width, dst->height);
510 static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
512 int planar = av_sample_fmt_is_planar(dst->format);
513 int channels = av_get_channel_layout_nb_channels(dst->channel_layout);
514 int planes = planar ? channels : 1;
517 if (dst->nb_samples != src->nb_samples ||
518 dst->channel_layout != src->channel_layout)
519 return AVERROR(EINVAL);
521 for (i = 0; i < planes; i++)
522 if (!dst->extended_data[i] || !src->extended_data[i])
523 return AVERROR(EINVAL);
525 av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
526 dst->nb_samples, channels, dst->format);
531 int av_frame_copy(AVFrame *dst, const AVFrame *src)
533 if (dst->format != src->format || dst->format < 0)
534 return AVERROR(EINVAL);
536 if (dst->width > 0 && dst->height > 0)
537 return frame_copy_video(dst, src);
538 else if (dst->nb_samples > 0 && dst->channel_layout)
539 return frame_copy_audio(dst, src);
541 return AVERROR(EINVAL);
544 void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
548 for (i = 0; i < frame->nb_side_data; i++) {
549 AVFrameSideData *sd = frame->side_data[i];
550 if (sd->type == type) {
552 av_dict_free(&sd->metadata);
553 av_freep(&frame->side_data[i]);
554 frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
555 frame->nb_side_data--;