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;
43 AVFrame *av_frame_alloc(void)
45 AVFrame *frame = av_mallocz(sizeof(*frame));
50 get_frame_defaults(frame);
55 void av_frame_free(AVFrame **frame)
57 if (!frame || !*frame)
60 av_frame_unref(*frame);
64 static int get_video_buffer(AVFrame *frame, int align)
66 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
70 return AVERROR(EINVAL);
72 if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
75 if (!frame->linesize[0]) {
76 ret = av_image_fill_linesizes(frame->linesize, frame->format,
81 for (i = 0; i < 4 && frame->linesize[i]; i++)
82 frame->linesize[i] = FFALIGN(frame->linesize[i], align);
85 for (i = 0; i < 4 && frame->linesize[i]; i++) {
86 int h = frame->height;
88 h = -((-h) >> desc->log2_chroma_h);
90 frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h);
94 frame->data[i] = frame->buf[i]->data;
96 if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
97 av_buffer_unref(&frame->buf[1]);
98 frame->buf[1] = av_buffer_alloc(1024);
101 frame->data[1] = frame->buf[1]->data;
104 frame->extended_data = frame->data;
108 av_frame_unref(frame);
109 return AVERROR(ENOMEM);
112 static int get_audio_buffer(AVFrame *frame, int align)
114 int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
115 int planar = av_sample_fmt_is_planar(frame->format);
116 int planes = planar ? channels : 1;
119 if (!frame->linesize[0]) {
120 ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
121 frame->nb_samples, frame->format,
127 if (planes > AV_NUM_DATA_POINTERS) {
128 frame->extended_data = av_mallocz(planes *
129 sizeof(*frame->extended_data));
130 frame->extended_buf = av_mallocz((planes - AV_NUM_DATA_POINTERS) *
131 sizeof(*frame->extended_buf));
132 if (!frame->extended_data || !frame->extended_buf) {
133 av_freep(&frame->extended_data);
134 av_freep(&frame->extended_buf);
135 return AVERROR(ENOMEM);
137 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
139 frame->extended_data = frame->data;
141 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
142 frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
143 if (!frame->buf[i]) {
144 av_frame_unref(frame);
145 return AVERROR(ENOMEM);
147 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
149 for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
150 frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
151 if (!frame->extended_buf[i]) {
152 av_frame_unref(frame);
153 return AVERROR(ENOMEM);
155 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
161 int av_frame_get_buffer(AVFrame *frame, int align)
163 if (frame->format < 0)
164 return AVERROR(EINVAL);
166 if (frame->width > 0 && frame->height > 0)
167 return get_video_buffer(frame, align);
168 else if (frame->nb_samples > 0 && frame->channel_layout)
169 return get_audio_buffer(frame, align);
171 return AVERROR(EINVAL);
174 int av_frame_ref(AVFrame *dst, const AVFrame *src)
178 dst->format = src->format;
179 dst->width = src->width;
180 dst->height = src->height;
181 dst->channel_layout = src->channel_layout;
182 dst->nb_samples = src->nb_samples;
184 ret = av_frame_copy_props(dst, src);
188 /* duplicate the frame data if it's not refcounted */
190 ret = av_frame_get_buffer(dst, 32);
194 ret = av_frame_copy(dst, src);
201 /* ref the buffers */
202 for (i = 0; i < FF_ARRAY_ELEMS(src->buf) && src->buf[i]; i++) {
203 dst->buf[i] = av_buffer_ref(src->buf[i]);
205 ret = AVERROR(ENOMEM);
210 if (src->extended_buf) {
211 dst->extended_buf = av_mallocz(sizeof(*dst->extended_buf) *
212 src->nb_extended_buf);
213 if (!dst->extended_buf) {
214 ret = AVERROR(ENOMEM);
217 dst->nb_extended_buf = src->nb_extended_buf;
219 for (i = 0; i < src->nb_extended_buf; i++) {
220 dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
221 if (!dst->extended_buf[i]) {
222 ret = AVERROR(ENOMEM);
228 /* duplicate extended data */
229 if (src->extended_data != src->data) {
230 int ch = av_get_channel_layout_nb_channels(src->channel_layout);
233 ret = AVERROR(EINVAL);
237 dst->extended_data = av_malloc(sizeof(*dst->extended_data) * ch);
238 if (!dst->extended_data) {
239 ret = AVERROR(ENOMEM);
242 memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
244 dst->extended_data = dst->data;
246 memcpy(dst->data, src->data, sizeof(src->data));
247 memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
256 AVFrame *av_frame_clone(const AVFrame *src)
258 AVFrame *ret = av_frame_alloc();
263 if (av_frame_ref(ret, src) < 0)
269 void av_frame_unref(AVFrame *frame)
273 for (i = 0; i < frame->nb_side_data; i++) {
274 av_freep(&frame->side_data[i]->data);
275 av_dict_free(&frame->side_data[i]->metadata);
276 av_freep(&frame->side_data[i]);
278 av_freep(&frame->side_data);
280 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
281 av_buffer_unref(&frame->buf[i]);
282 for (i = 0; i < frame->nb_extended_buf; i++)
283 av_buffer_unref(&frame->extended_buf[i]);
284 av_freep(&frame->extended_buf);
285 get_frame_defaults(frame);
288 void av_frame_move_ref(AVFrame *dst, AVFrame *src)
291 if (src->extended_data == src->data)
292 dst->extended_data = dst->data;
293 memset(src, 0, sizeof(*src));
294 get_frame_defaults(src);
297 int av_frame_is_writable(AVFrame *frame)
301 /* assume non-refcounted frames are not writable */
305 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++)
306 ret &= !!av_buffer_is_writable(frame->buf[i]);
307 for (i = 0; i < frame->nb_extended_buf; i++)
308 ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
313 int av_frame_make_writable(AVFrame *frame)
319 return AVERROR(EINVAL);
321 if (av_frame_is_writable(frame))
324 memset(&tmp, 0, sizeof(tmp));
325 tmp.format = frame->format;
326 tmp.width = frame->width;
327 tmp.height = frame->height;
328 tmp.channel_layout = frame->channel_layout;
329 tmp.nb_samples = frame->nb_samples;
330 ret = av_frame_get_buffer(&tmp, 32);
334 ret = av_frame_copy(&tmp, frame);
336 av_frame_unref(&tmp);
340 ret = av_frame_copy_props(&tmp, frame);
342 av_frame_unref(&tmp);
346 av_frame_unref(frame);
349 if (tmp.data == tmp.extended_data)
350 frame->extended_data = frame->data;
355 int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
359 dst->key_frame = src->key_frame;
360 dst->pict_type = src->pict_type;
361 dst->sample_aspect_ratio = src->sample_aspect_ratio;
363 dst->repeat_pict = src->repeat_pict;
364 dst->interlaced_frame = src->interlaced_frame;
365 dst->top_field_first = src->top_field_first;
366 dst->palette_has_changed = src->palette_has_changed;
367 dst->sample_rate = src->sample_rate;
368 dst->opaque = src->opaque;
369 dst->pkt_pts = src->pkt_pts;
370 dst->pkt_dts = src->pkt_dts;
371 dst->reordered_opaque = src->reordered_opaque;
372 dst->quality = src->quality;
373 dst->coded_picture_number = src->coded_picture_number;
374 dst->display_picture_number = src->display_picture_number;
375 dst->flags = src->flags;
377 memcpy(dst->error, src->error, sizeof(dst->error));
379 for (i = 0; i < src->nb_side_data; i++) {
380 const AVFrameSideData *sd_src = src->side_data[i];
381 AVFrameSideData *sd_dst = av_frame_new_side_data(dst, sd_src->type,
384 for (i = 0; i < dst->nb_side_data; i++) {
385 av_freep(&dst->side_data[i]->data);
386 av_freep(&dst->side_data[i]);
387 av_dict_free(&dst->side_data[i]->metadata);
389 av_freep(&dst->side_data);
390 return AVERROR(ENOMEM);
392 memcpy(sd_dst->data, sd_src->data, sd_src->size);
393 av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
399 AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
404 if (frame->nb_samples) {
405 int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
408 planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
412 if (plane < 0 || plane >= planes || !frame->extended_data[plane])
414 data = frame->extended_data[plane];
416 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
417 AVBufferRef *buf = frame->buf[i];
418 if (data >= buf->data && data < buf->data + buf->size)
421 for (i = 0; i < frame->nb_extended_buf; i++) {
422 AVBufferRef *buf = frame->extended_buf[i];
423 if (data >= buf->data && data < buf->data + buf->size)
429 AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
430 enum AVFrameSideDataType type,
433 AVFrameSideData *ret, **tmp;
435 if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
438 tmp = av_realloc(frame->side_data,
439 (frame->nb_side_data + 1) * sizeof(*frame->side_data));
442 frame->side_data = tmp;
444 ret = av_mallocz(sizeof(*ret));
448 ret->data = av_malloc(size);
457 frame->side_data[frame->nb_side_data++] = ret;
462 AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
463 enum AVFrameSideDataType type)
467 for (i = 0; i < frame->nb_side_data; i++) {
468 if (frame->side_data[i]->type == type)
469 return frame->side_data[i];
474 static int frame_copy_video(AVFrame *dst, const AVFrame *src)
476 const uint8_t *src_data[4];
479 if (dst->width != src->width ||
480 dst->height != src->height)
481 return AVERROR(EINVAL);
483 planes = av_pix_fmt_count_planes(dst->format);
484 for (i = 0; i < planes; i++)
485 if (!dst->data[i] || !src->data[i])
486 return AVERROR(EINVAL);
488 memcpy(src_data, src->data, sizeof(src_data));
489 av_image_copy(dst->data, dst->linesize,
490 src_data, src->linesize,
491 dst->format, dst->width, dst->height);
496 static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
498 int planar = av_sample_fmt_is_planar(dst->format);
499 int channels = av_get_channel_layout_nb_channels(dst->channel_layout);
500 int planes = planar ? channels : 1;
503 if (dst->nb_samples != src->nb_samples ||
504 dst->channel_layout != src->channel_layout)
505 return AVERROR(EINVAL);
507 for (i = 0; i < planes; i++)
508 if (!dst->extended_data[i] || !src->extended_data[i])
509 return AVERROR(EINVAL);
511 av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
512 dst->nb_samples, channels, dst->format);
517 int av_frame_copy(AVFrame *dst, const AVFrame *src)
519 if (dst->format != src->format || dst->format < 0)
520 return AVERROR(EINVAL);
522 if (dst->width > 0 && dst->height > 0)
523 return frame_copy_video(dst, src);
524 else if (dst->nb_samples > 0 && dst->channel_layout)
525 return frame_copy_audio(dst, src);
527 return AVERROR(EINVAL);