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 static void free_side_data(AVFrameSideData **ptr_sd)
52 AVFrameSideData *sd = *ptr_sd;
55 av_dict_free(&sd->metadata);
59 AVFrame *av_frame_alloc(void)
61 AVFrame *frame = av_mallocz(sizeof(*frame));
66 get_frame_defaults(frame);
71 void av_frame_free(AVFrame **frame)
73 if (!frame || !*frame)
76 av_frame_unref(*frame);
80 static int get_video_buffer(AVFrame *frame, int align)
82 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
86 return AVERROR(EINVAL);
88 if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
91 if (!frame->linesize[0]) {
92 ret = av_image_fill_linesizes(frame->linesize, frame->format,
97 for (i = 0; i < 4 && frame->linesize[i]; i++)
98 frame->linesize[i] = FFALIGN(frame->linesize[i], align);
101 for (i = 0; i < 4 && frame->linesize[i]; i++) {
102 int h = frame->height;
103 if (i == 1 || i == 2)
104 h = -((-h) >> desc->log2_chroma_h);
106 frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h);
110 frame->data[i] = frame->buf[i]->data;
112 if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
113 av_buffer_unref(&frame->buf[1]);
114 frame->buf[1] = av_buffer_alloc(1024);
117 frame->data[1] = frame->buf[1]->data;
120 frame->extended_data = frame->data;
124 av_frame_unref(frame);
125 return AVERROR(ENOMEM);
128 static int get_audio_buffer(AVFrame *frame, int align)
130 int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
131 int planar = av_sample_fmt_is_planar(frame->format);
132 int planes = planar ? channels : 1;
135 if (!frame->linesize[0]) {
136 ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
137 frame->nb_samples, frame->format,
143 if (planes > AV_NUM_DATA_POINTERS) {
144 frame->extended_data = av_mallocz(planes *
145 sizeof(*frame->extended_data));
146 frame->extended_buf = av_mallocz((planes - AV_NUM_DATA_POINTERS) *
147 sizeof(*frame->extended_buf));
148 if (!frame->extended_data || !frame->extended_buf) {
149 av_freep(&frame->extended_data);
150 av_freep(&frame->extended_buf);
151 return AVERROR(ENOMEM);
153 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
155 frame->extended_data = frame->data;
157 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
158 frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
159 if (!frame->buf[i]) {
160 av_frame_unref(frame);
161 return AVERROR(ENOMEM);
163 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
165 for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
166 frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
167 if (!frame->extended_buf[i]) {
168 av_frame_unref(frame);
169 return AVERROR(ENOMEM);
171 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
177 int av_frame_get_buffer(AVFrame *frame, int align)
179 if (frame->format < 0)
180 return AVERROR(EINVAL);
182 if (frame->width > 0 && frame->height > 0)
183 return get_video_buffer(frame, align);
184 else if (frame->nb_samples > 0 && frame->channel_layout)
185 return get_audio_buffer(frame, align);
187 return AVERROR(EINVAL);
190 int av_frame_ref(AVFrame *dst, const AVFrame *src)
194 dst->format = src->format;
195 dst->width = src->width;
196 dst->height = src->height;
197 dst->channel_layout = src->channel_layout;
198 dst->nb_samples = src->nb_samples;
200 ret = av_frame_copy_props(dst, src);
204 /* duplicate the frame data if it's not refcounted */
206 ret = av_frame_get_buffer(dst, 32);
210 ret = av_frame_copy(dst, src);
217 /* ref the buffers */
218 for (i = 0; i < FF_ARRAY_ELEMS(src->buf) && src->buf[i]; i++) {
219 dst->buf[i] = av_buffer_ref(src->buf[i]);
221 ret = AVERROR(ENOMEM);
226 if (src->extended_buf) {
227 dst->extended_buf = av_mallocz(sizeof(*dst->extended_buf) *
228 src->nb_extended_buf);
229 if (!dst->extended_buf) {
230 ret = AVERROR(ENOMEM);
233 dst->nb_extended_buf = src->nb_extended_buf;
235 for (i = 0; i < src->nb_extended_buf; i++) {
236 dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
237 if (!dst->extended_buf[i]) {
238 ret = AVERROR(ENOMEM);
244 /* duplicate extended data */
245 if (src->extended_data != src->data) {
246 int ch = av_get_channel_layout_nb_channels(src->channel_layout);
249 ret = AVERROR(EINVAL);
253 dst->extended_data = av_malloc(sizeof(*dst->extended_data) * ch);
254 if (!dst->extended_data) {
255 ret = AVERROR(ENOMEM);
258 memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
260 dst->extended_data = dst->data;
262 memcpy(dst->data, src->data, sizeof(src->data));
263 memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
272 AVFrame *av_frame_clone(const AVFrame *src)
274 AVFrame *ret = av_frame_alloc();
279 if (av_frame_ref(ret, src) < 0)
285 void av_frame_unref(AVFrame *frame)
289 for (i = 0; i < frame->nb_side_data; i++) {
290 free_side_data(&frame->side_data[i]);
292 av_freep(&frame->side_data);
294 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
295 av_buffer_unref(&frame->buf[i]);
296 for (i = 0; i < frame->nb_extended_buf; i++)
297 av_buffer_unref(&frame->extended_buf[i]);
298 av_freep(&frame->extended_buf);
299 get_frame_defaults(frame);
302 void av_frame_move_ref(AVFrame *dst, AVFrame *src)
305 if (src->extended_data == src->data)
306 dst->extended_data = dst->data;
307 memset(src, 0, sizeof(*src));
308 get_frame_defaults(src);
311 int av_frame_is_writable(AVFrame *frame)
315 /* assume non-refcounted frames are not writable */
319 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++)
320 ret &= !!av_buffer_is_writable(frame->buf[i]);
321 for (i = 0; i < frame->nb_extended_buf; i++)
322 ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
327 int av_frame_make_writable(AVFrame *frame)
333 return AVERROR(EINVAL);
335 if (av_frame_is_writable(frame))
338 memset(&tmp, 0, sizeof(tmp));
339 tmp.format = frame->format;
340 tmp.width = frame->width;
341 tmp.height = frame->height;
342 tmp.channel_layout = frame->channel_layout;
343 tmp.nb_samples = frame->nb_samples;
344 ret = av_frame_get_buffer(&tmp, 32);
348 ret = av_frame_copy(&tmp, frame);
350 av_frame_unref(&tmp);
354 ret = av_frame_copy_props(&tmp, frame);
356 av_frame_unref(&tmp);
360 av_frame_unref(frame);
363 if (tmp.data == tmp.extended_data)
364 frame->extended_data = frame->data;
369 int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
373 dst->key_frame = src->key_frame;
374 dst->pict_type = src->pict_type;
375 dst->sample_aspect_ratio = src->sample_aspect_ratio;
377 dst->repeat_pict = src->repeat_pict;
378 dst->interlaced_frame = src->interlaced_frame;
379 dst->top_field_first = src->top_field_first;
380 dst->palette_has_changed = src->palette_has_changed;
381 dst->sample_rate = src->sample_rate;
382 dst->opaque = src->opaque;
383 dst->pkt_pts = src->pkt_pts;
384 dst->pkt_dts = src->pkt_dts;
385 dst->reordered_opaque = src->reordered_opaque;
386 dst->quality = src->quality;
387 dst->coded_picture_number = src->coded_picture_number;
388 dst->display_picture_number = src->display_picture_number;
389 dst->flags = src->flags;
390 #if FF_API_AVFRAME_COLORSPACE
391 dst->color_primaries = src->color_primaries;
392 dst->color_trc = src->color_trc;
393 dst->colorspace = src->colorspace;
394 dst->color_range = src->color_range;
395 dst->chroma_location = src->chroma_location;
398 memcpy(dst->error, src->error, sizeof(dst->error));
400 for (i = 0; i < src->nb_side_data; i++) {
401 const AVFrameSideData *sd_src = src->side_data[i];
402 AVFrameSideData *sd_dst = av_frame_new_side_data(dst, sd_src->type,
405 for (i = 0; i < dst->nb_side_data; i++) {
406 free_side_data(&dst->side_data[i]);
408 av_freep(&dst->side_data);
409 return AVERROR(ENOMEM);
411 memcpy(sd_dst->data, sd_src->data, sd_src->size);
412 av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
418 AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
423 if (frame->nb_samples) {
424 int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
427 planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
431 if (plane < 0 || plane >= planes || !frame->extended_data[plane])
433 data = frame->extended_data[plane];
435 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
436 AVBufferRef *buf = frame->buf[i];
437 if (data >= buf->data && data < buf->data + buf->size)
440 for (i = 0; i < frame->nb_extended_buf; i++) {
441 AVBufferRef *buf = frame->extended_buf[i];
442 if (data >= buf->data && data < buf->data + buf->size)
448 AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
449 enum AVFrameSideDataType type,
452 AVFrameSideData *ret, **tmp;
454 if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
457 tmp = av_realloc(frame->side_data,
458 (frame->nb_side_data + 1) * sizeof(*frame->side_data));
461 frame->side_data = tmp;
463 ret = av_mallocz(sizeof(*ret));
467 ret->data = av_malloc(size);
476 frame->side_data[frame->nb_side_data++] = ret;
481 AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
482 enum AVFrameSideDataType type)
486 for (i = 0; i < frame->nb_side_data; i++) {
487 if (frame->side_data[i]->type == type)
488 return frame->side_data[i];
493 static int frame_copy_video(AVFrame *dst, const AVFrame *src)
495 const uint8_t *src_data[4];
498 if (dst->width != src->width ||
499 dst->height != src->height)
500 return AVERROR(EINVAL);
502 planes = av_pix_fmt_count_planes(dst->format);
503 for (i = 0; i < planes; i++)
504 if (!dst->data[i] || !src->data[i])
505 return AVERROR(EINVAL);
507 memcpy(src_data, src->data, sizeof(src_data));
508 av_image_copy(dst->data, dst->linesize,
509 src_data, src->linesize,
510 dst->format, dst->width, dst->height);
515 static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
517 int planar = av_sample_fmt_is_planar(dst->format);
518 int channels = av_get_channel_layout_nb_channels(dst->channel_layout);
519 int planes = planar ? channels : 1;
522 if (dst->nb_samples != src->nb_samples ||
523 dst->channel_layout != src->channel_layout)
524 return AVERROR(EINVAL);
526 for (i = 0; i < planes; i++)
527 if (!dst->extended_data[i] || !src->extended_data[i])
528 return AVERROR(EINVAL);
530 av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
531 dst->nb_samples, channels, dst->format);
536 int av_frame_copy(AVFrame *dst, const AVFrame *src)
538 if (dst->format != src->format || dst->format < 0)
539 return AVERROR(EINVAL);
541 if (dst->width > 0 && dst->height > 0)
542 return frame_copy_video(dst, src);
543 else if (dst->nb_samples > 0 && dst->channel_layout)
544 return frame_copy_audio(dst, src);
546 return AVERROR(EINVAL);
549 void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
553 for (i = 0; i < frame->nb_side_data; i++) {
554 AVFrameSideData *sd = frame->side_data[i];
555 if (sd->type == type) {
556 free_side_data(&frame->side_data[i]);
557 frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
558 frame->nb_side_data--;