2 * This file is part of Libav.
4 * Libav is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "channel_layout.h"
26 #include "samplefmt.h"
28 static void get_frame_defaults(AVFrame *frame)
30 if (frame->extended_data != frame->data)
31 av_freep(&frame->extended_data);
33 memset(frame, 0, sizeof(*frame));
35 frame->pts = AV_NOPTS_VALUE;
37 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
38 frame->format = -1; /* unknown */
39 frame->extended_data = frame->data;
40 frame->color_primaries = AVCOL_PRI_UNSPECIFIED;
41 frame->color_trc = AVCOL_TRC_UNSPECIFIED;
42 frame->colorspace = AVCOL_SPC_UNSPECIFIED;
43 frame->color_range = AVCOL_RANGE_UNSPECIFIED;
44 frame->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
47 static void free_side_data(AVFrameSideData **ptr_sd)
49 AVFrameSideData *sd = *ptr_sd;
52 av_dict_free(&sd->metadata);
56 static void wipe_side_data(AVFrame *frame)
60 for (i = 0; i < frame->nb_side_data; i++) {
61 free_side_data(&frame->side_data[i]);
63 frame->nb_side_data = 0;
65 av_freep(&frame->side_data);
68 AVFrame *av_frame_alloc(void)
70 AVFrame *frame = av_mallocz(sizeof(*frame));
75 get_frame_defaults(frame);
80 void av_frame_free(AVFrame **frame)
82 if (!frame || !*frame)
85 av_frame_unref(*frame);
89 static int get_video_buffer(AVFrame *frame, int align)
91 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
95 return AVERROR(EINVAL);
97 if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
100 if (!frame->linesize[0]) {
101 ret = av_image_fill_linesizes(frame->linesize, frame->format,
106 for (i = 0; i < 4 && frame->linesize[i]; i++)
107 frame->linesize[i] = FFALIGN(frame->linesize[i], align);
110 for (i = 0; i < 4 && frame->linesize[i]; i++) {
111 int h = frame->height;
112 if (i == 1 || i == 2)
113 h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
115 frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h);
119 frame->data[i] = frame->buf[i]->data;
121 if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
122 av_buffer_unref(&frame->buf[1]);
123 frame->buf[1] = av_buffer_alloc(1024);
126 frame->data[1] = frame->buf[1]->data;
129 frame->extended_data = frame->data;
133 av_frame_unref(frame);
134 return AVERROR(ENOMEM);
137 static int get_audio_buffer(AVFrame *frame, int align)
139 int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
140 int planar = av_sample_fmt_is_planar(frame->format);
141 int planes = planar ? channels : 1;
144 if (!frame->linesize[0]) {
145 ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
146 frame->nb_samples, frame->format,
152 if (planes > AV_NUM_DATA_POINTERS) {
153 frame->extended_data = av_mallocz(planes *
154 sizeof(*frame->extended_data));
155 frame->extended_buf = av_mallocz((planes - AV_NUM_DATA_POINTERS) *
156 sizeof(*frame->extended_buf));
157 if (!frame->extended_data || !frame->extended_buf) {
158 av_freep(&frame->extended_data);
159 av_freep(&frame->extended_buf);
160 return AVERROR(ENOMEM);
162 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
164 frame->extended_data = frame->data;
166 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
167 frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
168 if (!frame->buf[i]) {
169 av_frame_unref(frame);
170 return AVERROR(ENOMEM);
172 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
174 for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
175 frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
176 if (!frame->extended_buf[i]) {
177 av_frame_unref(frame);
178 return AVERROR(ENOMEM);
180 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
186 int av_frame_get_buffer(AVFrame *frame, int align)
188 if (frame->format < 0)
189 return AVERROR(EINVAL);
191 if (frame->width > 0 && frame->height > 0)
192 return get_video_buffer(frame, align);
193 else if (frame->nb_samples > 0 && frame->channel_layout)
194 return get_audio_buffer(frame, align);
196 return AVERROR(EINVAL);
199 int av_frame_ref(AVFrame *dst, const AVFrame *src)
203 dst->format = src->format;
204 dst->width = src->width;
205 dst->height = src->height;
206 dst->channel_layout = src->channel_layout;
207 dst->nb_samples = src->nb_samples;
209 ret = av_frame_copy_props(dst, src);
213 /* duplicate the frame data if it's not refcounted */
215 ret = av_frame_get_buffer(dst, 32);
219 ret = av_frame_copy(dst, src);
226 /* ref the buffers */
227 for (i = 0; i < FF_ARRAY_ELEMS(src->buf) && src->buf[i]; i++) {
228 dst->buf[i] = av_buffer_ref(src->buf[i]);
230 ret = AVERROR(ENOMEM);
235 if (src->extended_buf) {
236 dst->extended_buf = av_mallocz(sizeof(*dst->extended_buf) *
237 src->nb_extended_buf);
238 if (!dst->extended_buf) {
239 ret = AVERROR(ENOMEM);
242 dst->nb_extended_buf = src->nb_extended_buf;
244 for (i = 0; i < src->nb_extended_buf; i++) {
245 dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
246 if (!dst->extended_buf[i]) {
247 ret = AVERROR(ENOMEM);
253 if (src->hw_frames_ctx) {
254 dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
255 if (!dst->hw_frames_ctx) {
256 ret = AVERROR(ENOMEM);
261 /* duplicate extended data */
262 if (src->extended_data != src->data) {
263 int ch = av_get_channel_layout_nb_channels(src->channel_layout);
266 ret = AVERROR(EINVAL);
270 dst->extended_data = av_malloc(sizeof(*dst->extended_data) * ch);
271 if (!dst->extended_data) {
272 ret = AVERROR(ENOMEM);
275 memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
277 dst->extended_data = dst->data;
279 memcpy(dst->data, src->data, sizeof(src->data));
280 memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
289 AVFrame *av_frame_clone(const AVFrame *src)
291 AVFrame *ret = av_frame_alloc();
296 if (av_frame_ref(ret, src) < 0)
302 void av_frame_unref(AVFrame *frame)
306 wipe_side_data(frame);
308 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
309 av_buffer_unref(&frame->buf[i]);
310 for (i = 0; i < frame->nb_extended_buf; i++)
311 av_buffer_unref(&frame->extended_buf[i]);
312 av_freep(&frame->extended_buf);
314 av_buffer_unref(&frame->hw_frames_ctx);
316 get_frame_defaults(frame);
319 void av_frame_move_ref(AVFrame *dst, AVFrame *src)
322 if (src->extended_data == src->data)
323 dst->extended_data = dst->data;
324 memset(src, 0, sizeof(*src));
325 get_frame_defaults(src);
328 int av_frame_is_writable(AVFrame *frame)
332 /* assume non-refcounted frames are not writable */
336 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++)
337 ret &= !!av_buffer_is_writable(frame->buf[i]);
338 for (i = 0; i < frame->nb_extended_buf; i++)
339 ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
344 int av_frame_make_writable(AVFrame *frame)
350 return AVERROR(EINVAL);
352 if (av_frame_is_writable(frame))
355 memset(&tmp, 0, sizeof(tmp));
356 tmp.format = frame->format;
357 tmp.width = frame->width;
358 tmp.height = frame->height;
359 tmp.channel_layout = frame->channel_layout;
360 tmp.nb_samples = frame->nb_samples;
361 ret = av_frame_get_buffer(&tmp, 32);
365 ret = av_frame_copy(&tmp, frame);
367 av_frame_unref(&tmp);
371 ret = av_frame_copy_props(&tmp, frame);
373 av_frame_unref(&tmp);
377 av_frame_unref(frame);
380 if (tmp.data == tmp.extended_data)
381 frame->extended_data = frame->data;
386 int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
390 dst->key_frame = src->key_frame;
391 dst->pict_type = src->pict_type;
392 dst->sample_aspect_ratio = src->sample_aspect_ratio;
394 dst->repeat_pict = src->repeat_pict;
395 dst->interlaced_frame = src->interlaced_frame;
396 dst->top_field_first = src->top_field_first;
397 dst->palette_has_changed = src->palette_has_changed;
398 dst->sample_rate = src->sample_rate;
399 dst->opaque = src->opaque;
401 FF_DISABLE_DEPRECATION_WARNINGS
402 dst->pkt_pts = src->pkt_pts;
403 FF_ENABLE_DEPRECATION_WARNINGS
405 dst->pkt_dts = src->pkt_dts;
406 dst->reordered_opaque = src->reordered_opaque;
407 dst->quality = src->quality;
408 dst->coded_picture_number = src->coded_picture_number;
409 dst->display_picture_number = src->display_picture_number;
410 dst->flags = src->flags;
411 dst->color_primaries = src->color_primaries;
412 dst->color_trc = src->color_trc;
413 dst->colorspace = src->colorspace;
414 dst->color_range = src->color_range;
415 dst->chroma_location = src->chroma_location;
417 #if FF_API_ERROR_FRAME
418 FF_DISABLE_DEPRECATION_WARNINGS
419 memcpy(dst->error, src->error, sizeof(dst->error));
420 FF_ENABLE_DEPRECATION_WARNINGS
423 for (i = 0; i < src->nb_side_data; i++) {
424 const AVFrameSideData *sd_src = src->side_data[i];
425 AVFrameSideData *sd_dst = av_frame_new_side_data(dst, sd_src->type,
429 return AVERROR(ENOMEM);
431 memcpy(sd_dst->data, sd_src->data, sd_src->size);
432 av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
438 AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
443 if (frame->nb_samples) {
444 int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
447 planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
451 if (plane < 0 || plane >= planes || !frame->extended_data[plane])
453 data = frame->extended_data[plane];
455 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
456 AVBufferRef *buf = frame->buf[i];
457 if (data >= buf->data && data < buf->data + buf->size)
460 for (i = 0; i < frame->nb_extended_buf; i++) {
461 AVBufferRef *buf = frame->extended_buf[i];
462 if (data >= buf->data && data < buf->data + buf->size)
468 AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
469 enum AVFrameSideDataType type,
472 AVFrameSideData *ret, **tmp;
474 if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
477 tmp = av_realloc(frame->side_data,
478 (frame->nb_side_data + 1) * sizeof(*frame->side_data));
481 frame->side_data = tmp;
483 ret = av_mallocz(sizeof(*ret));
487 ret->data = av_malloc(size);
496 frame->side_data[frame->nb_side_data++] = ret;
501 AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
502 enum AVFrameSideDataType type)
506 for (i = 0; i < frame->nb_side_data; i++) {
507 if (frame->side_data[i]->type == type)
508 return frame->side_data[i];
513 static int frame_copy_video(AVFrame *dst, const AVFrame *src)
515 const uint8_t *src_data[4];
518 if (dst->width != src->width ||
519 dst->height != src->height)
520 return AVERROR(EINVAL);
522 planes = av_pix_fmt_count_planes(dst->format);
523 for (i = 0; i < planes; i++)
524 if (!dst->data[i] || !src->data[i])
525 return AVERROR(EINVAL);
527 memcpy(src_data, src->data, sizeof(src_data));
528 av_image_copy(dst->data, dst->linesize,
529 src_data, src->linesize,
530 dst->format, dst->width, dst->height);
535 static int frame_copy_audio(AVFrame *dst, const AVFrame *src)
537 int planar = av_sample_fmt_is_planar(dst->format);
538 int channels = av_get_channel_layout_nb_channels(dst->channel_layout);
539 int planes = planar ? channels : 1;
542 if (dst->nb_samples != src->nb_samples ||
543 dst->channel_layout != src->channel_layout)
544 return AVERROR(EINVAL);
546 for (i = 0; i < planes; i++)
547 if (!dst->extended_data[i] || !src->extended_data[i])
548 return AVERROR(EINVAL);
550 av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
551 dst->nb_samples, channels, dst->format);
556 int av_frame_copy(AVFrame *dst, const AVFrame *src)
558 if (dst->format != src->format || dst->format < 0)
559 return AVERROR(EINVAL);
561 if (dst->width > 0 && dst->height > 0)
562 return frame_copy_video(dst, src);
563 else if (dst->nb_samples > 0 && dst->channel_layout)
564 return frame_copy_audio(dst, src);
566 return AVERROR(EINVAL);
569 void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
573 for (i = 0; i < frame->nb_side_data; i++) {
574 AVFrameSideData *sd = frame->side_data[i];
575 if (sd->type == type) {
576 free_side_data(&frame->side_data[i]);
577 frame->side_data[i] = frame->side_data[frame->nb_side_data - 1];
578 frame->nb_side_data--;