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 AVFrame *av_frame_alloc(void)
59 AVFrame *frame = av_mallocz(sizeof(*frame));
64 get_frame_defaults(frame);
69 void av_frame_free(AVFrame **frame)
71 if (!frame || !*frame)
74 av_frame_unref(*frame);
78 static int get_video_buffer(AVFrame *frame, int align)
80 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
84 return AVERROR(EINVAL);
86 if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
89 if (!frame->linesize[0]) {
90 ret = av_image_fill_linesizes(frame->linesize, frame->format,
95 for (i = 0; i < 4 && frame->linesize[i]; i++)
96 frame->linesize[i] = FFALIGN(frame->linesize[i], align);
99 for (i = 0; i < 4 && frame->linesize[i]; i++) {
100 int h = frame->height;
101 if (i == 1 || i == 2)
102 h = -((-h) >> desc->log2_chroma_h);
104 frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h);
108 frame->data[i] = frame->buf[i]->data;
110 if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
111 av_buffer_unref(&frame->buf[1]);
112 frame->buf[1] = av_buffer_alloc(1024);
115 frame->data[1] = frame->buf[1]->data;
118 frame->extended_data = frame->data;
122 av_frame_unref(frame);
123 return AVERROR(ENOMEM);
126 static int get_audio_buffer(AVFrame *frame, int align)
128 int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
129 int planar = av_sample_fmt_is_planar(frame->format);
130 int planes = planar ? channels : 1;
133 if (!frame->linesize[0]) {
134 ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
135 frame->nb_samples, frame->format,
141 if (planes > AV_NUM_DATA_POINTERS) {
142 frame->extended_data = av_mallocz(planes *
143 sizeof(*frame->extended_data));
144 frame->extended_buf = av_mallocz((planes - AV_NUM_DATA_POINTERS) *
145 sizeof(*frame->extended_buf));
146 if (!frame->extended_data || !frame->extended_buf) {
147 av_freep(&frame->extended_data);
148 av_freep(&frame->extended_buf);
149 return AVERROR(ENOMEM);
151 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
153 frame->extended_data = frame->data;
155 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
156 frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
157 if (!frame->buf[i]) {
158 av_frame_unref(frame);
159 return AVERROR(ENOMEM);
161 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
163 for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
164 frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
165 if (!frame->extended_buf[i]) {
166 av_frame_unref(frame);
167 return AVERROR(ENOMEM);
169 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
175 int av_frame_get_buffer(AVFrame *frame, int align)
177 if (frame->format < 0)
178 return AVERROR(EINVAL);
180 if (frame->width > 0 && frame->height > 0)
181 return get_video_buffer(frame, align);
182 else if (frame->nb_samples > 0 && frame->channel_layout)
183 return get_audio_buffer(frame, align);
185 return AVERROR(EINVAL);
188 int av_frame_ref(AVFrame *dst, const AVFrame *src)
192 dst->format = src->format;
193 dst->width = src->width;
194 dst->height = src->height;
195 dst->channel_layout = src->channel_layout;
196 dst->nb_samples = src->nb_samples;
198 ret = av_frame_copy_props(dst, src);
202 /* duplicate the frame data if it's not refcounted */
204 ret = av_frame_get_buffer(dst, 32);
208 ret = av_frame_copy(dst, src);
215 /* ref the buffers */
216 for (i = 0; i < FF_ARRAY_ELEMS(src->buf) && src->buf[i]; i++) {
217 dst->buf[i] = av_buffer_ref(src->buf[i]);
219 ret = AVERROR(ENOMEM);
224 if (src->extended_buf) {
225 dst->extended_buf = av_mallocz(sizeof(*dst->extended_buf) *
226 src->nb_extended_buf);
227 if (!dst->extended_buf) {
228 ret = AVERROR(ENOMEM);
231 dst->nb_extended_buf = src->nb_extended_buf;
233 for (i = 0; i < src->nb_extended_buf; i++) {
234 dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
235 if (!dst->extended_buf[i]) {
236 ret = AVERROR(ENOMEM);
242 /* duplicate extended data */
243 if (src->extended_data != src->data) {
244 int ch = av_get_channel_layout_nb_channels(src->channel_layout);
247 ret = AVERROR(EINVAL);
251 dst->extended_data = av_malloc(sizeof(*dst->extended_data) * ch);
252 if (!dst->extended_data) {
253 ret = AVERROR(ENOMEM);
256 memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
258 dst->extended_data = dst->data;
260 memcpy(dst->data, src->data, sizeof(src->data));
261 memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
270 AVFrame *av_frame_clone(const AVFrame *src)
272 AVFrame *ret = av_frame_alloc();
277 if (av_frame_ref(ret, src) < 0)
283 void av_frame_unref(AVFrame *frame)
287 for (i = 0; i < frame->nb_side_data; i++) {
288 free_side_data(&frame->side_data[i]);
290 av_freep(&frame->side_data);
292 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
293 av_buffer_unref(&frame->buf[i]);
294 for (i = 0; i < frame->nb_extended_buf; i++)
295 av_buffer_unref(&frame->extended_buf[i]);
296 av_freep(&frame->extended_buf);
297 get_frame_defaults(frame);
300 void av_frame_move_ref(AVFrame *dst, AVFrame *src)
303 if (src->extended_data == src->data)
304 dst->extended_data = dst->data;
305 memset(src, 0, sizeof(*src));
306 get_frame_defaults(src);
309 int av_frame_is_writable(AVFrame *frame)
313 /* assume non-refcounted frames are not writable */
317 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++)
318 ret &= !!av_buffer_is_writable(frame->buf[i]);
319 for (i = 0; i < frame->nb_extended_buf; i++)
320 ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
325 int av_frame_make_writable(AVFrame *frame)
331 return AVERROR(EINVAL);
333 if (av_frame_is_writable(frame))
336 memset(&tmp, 0, sizeof(tmp));
337 tmp.format = frame->format;
338 tmp.width = frame->width;
339 tmp.height = frame->height;
340 tmp.channel_layout = frame->channel_layout;
341 tmp.nb_samples = frame->nb_samples;
342 ret = av_frame_get_buffer(&tmp, 32);
346 ret = av_frame_copy(&tmp, frame);
348 av_frame_unref(&tmp);
352 ret = av_frame_copy_props(&tmp, frame);
354 av_frame_unref(&tmp);
358 av_frame_unref(frame);
361 if (tmp.data == tmp.extended_data)
362 frame->extended_data = frame->data;
367 int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
371 dst->key_frame = src->key_frame;
372 dst->pict_type = src->pict_type;
373 dst->sample_aspect_ratio = src->sample_aspect_ratio;
375 dst->repeat_pict = src->repeat_pict;
376 dst->interlaced_frame = src->interlaced_frame;
377 dst->top_field_first = src->top_field_first;
378 dst->palette_has_changed = src->palette_has_changed;
379 dst->sample_rate = src->sample_rate;
380 dst->opaque = src->opaque;
381 dst->pkt_pts = src->pkt_pts;
382 dst->pkt_dts = src->pkt_dts;
383 dst->reordered_opaque = src->reordered_opaque;
384 dst->quality = src->quality;
385 dst->coded_picture_number = src->coded_picture_number;
386 dst->display_picture_number = src->display_picture_number;
387 dst->flags = src->flags;
388 dst->color_primaries = src->color_primaries;
389 dst->color_trc = src->color_trc;
390 dst->colorspace = src->colorspace;
391 dst->color_range = src->color_range;
392 dst->chroma_location = src->chroma_location;
394 memcpy(dst->error, src->error, sizeof(dst->error));
396 for (i = 0; i < src->nb_side_data; i++) {
397 const AVFrameSideData *sd_src = src->side_data[i];
398 AVFrameSideData *sd_dst = av_frame_new_side_data(dst, sd_src->type,
401 for (i = 0; i < dst->nb_side_data; i++) {
402 free_side_data(&dst->side_data[i]);
404 av_freep(&dst->side_data);
405 return AVERROR(ENOMEM);
407 memcpy(sd_dst->data, sd_src->data, sd_src->size);
408 av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
414 AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
419 if (frame->nb_samples) {
420 int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
423 planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
427 if (plane < 0 || plane >= planes || !frame->extended_data[plane])
429 data = frame->extended_data[plane];
431 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
432 AVBufferRef *buf = frame->buf[i];
433 if (data >= buf->data && data < buf->data + buf->size)
436 for (i = 0; i < frame->nb_extended_buf; i++) {
437 AVBufferRef *buf = frame->extended_buf[i];
438 if (data >= buf->data && data < buf->data + buf->size)
444 AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
445 enum AVFrameSideDataType type,
448 AVFrameSideData *ret, **tmp;
450 if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
453 tmp = av_realloc(frame->side_data,
454 (frame->nb_side_data + 1) * sizeof(*frame->side_data));
457 frame->side_data = tmp;
459 ret = av_mallocz(sizeof(*ret));
463 ret->data = av_malloc(size);
472 frame->side_data[frame->nb_side_data++] = ret;
477 AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
478 enum AVFrameSideDataType type)
482 for (i = 0; i < frame->nb_side_data; i++) {
483 if (frame->side_data[i]->type == type)
484 return frame->side_data[i];
489 static int frame_copy_video(AVFrame *dst, const AVFrame *src)
491 const uint8_t *src_data[4];
494 if (dst->width != src->width ||
495 dst->height != src->height)
496 return AVERROR(EINVAL);
498 planes = av_pix_fmt_count_planes(dst->format);
499 for (i = 0; i < planes; i++)
500 if (!dst->data[i] || !src->data[i])
501 return AVERROR(EINVAL);
503 memcpy(src_data, src->data, sizeof(src_data));
504 av_image_copy(dst->data, dst->linesize,
505 src_data, src->linesize,
506 dst->format, dst->width, dst->height);
511 static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
513 int planar = av_sample_fmt_is_planar(dst->format);
514 int channels = av_get_channel_layout_nb_channels(dst->channel_layout);
515 int planes = planar ? channels : 1;
518 if (dst->nb_samples != src->nb_samples ||
519 dst->channel_layout != src->channel_layout)
520 return AVERROR(EINVAL);
522 for (i = 0; i < planes; i++)
523 if (!dst->extended_data[i] || !src->extended_data[i])
524 return AVERROR(EINVAL);
526 av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
527 dst->nb_samples, channels, dst->format);
532 int av_frame_copy(AVFrame *dst, const AVFrame *src)
534 if (dst->format != src->format || dst->format < 0)
535 return AVERROR(EINVAL);
537 if (dst->width > 0 && dst->height > 0)
538 return frame_copy_video(dst, src);
539 else if (dst->nb_samples > 0 && dst->channel_layout)
540 return frame_copy_audio(dst, src);
542 return AVERROR(EINVAL);
545 void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
549 for (i = 0; i < frame->nb_side_data; i++) {
550 AVFrameSideData *sd = frame->side_data[i];
551 if (sd->type == type) {
552 free_side_data(&frame->side_data[i]);
553 frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
554 frame->nb_side_data--;