3 * This file is part of FFmpeg.
5 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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"
28 #include "samplefmt.h"
30 MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
31 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
32 MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
33 MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
34 MAKE_ACCESSORS(AVFrame, frame, int, channels)
35 MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
36 MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
37 MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
38 MAKE_ACCESSORS(AVFrame, frame, int, pkt_size)
39 MAKE_ACCESSORS(AVFrame, frame, enum AVColorSpace, colorspace)
40 MAKE_ACCESSORS(AVFrame, frame, enum AVColorRange, color_range)
42 #define CHECK_CHANNELS_CONSISTENCY(frame) \
43 av_assert2(!(frame)->channel_layout || \
44 (frame)->channels == \
45 av_get_channel_layout_nb_channels((frame)->channel_layout))
47 AVDictionary **avpriv_frame_get_metadatap(AVFrame *frame) {return &frame->metadata;};
49 int av_frame_set_qp_table(AVFrame *f, AVBufferRef *buf, int stride, int qp_type)
51 av_buffer_unref(&f->qp_table_buf);
53 f->qp_table_buf = buf;
55 f->qscale_table = buf->data;
57 f->qscale_type = qp_type;
62 int8_t *av_frame_get_qp_table(AVFrame *f, int *stride, int *type)
65 *type = f->qscale_type;
70 return f->qp_table_buf->data;
73 const char *av_get_colorspace_name(enum AVColorSpace val)
75 static const char *name[] = {
76 [AVCOL_SPC_RGB] = "GBR",
77 [AVCOL_SPC_BT709] = "bt709",
78 [AVCOL_SPC_FCC] = "fcc",
79 [AVCOL_SPC_BT470BG] = "bt470bg",
80 [AVCOL_SPC_SMPTE170M] = "smpte170m",
81 [AVCOL_SPC_SMPTE240M] = "smpte240m",
82 [AVCOL_SPC_YCOCG] = "YCgCo",
84 if ((unsigned)val >= FF_ARRAY_ELEMS(name))
89 static void get_frame_defaults(AVFrame *frame)
91 if (frame->extended_data != frame->data)
92 av_freep(&frame->extended_data);
94 memset(frame, 0, sizeof(*frame));
98 frame->pkt_pts = AV_NOPTS_VALUE;
99 av_frame_set_best_effort_timestamp(frame, AV_NOPTS_VALUE);
100 av_frame_set_pkt_duration (frame, 0);
101 av_frame_set_pkt_pos (frame, -1);
102 av_frame_set_pkt_size (frame, -1);
103 frame->key_frame = 1;
104 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
105 frame->format = -1; /* unknown */
106 frame->colorspace = AVCOL_SPC_UNSPECIFIED;
107 frame->extended_data = frame->data;
110 AVFrame *av_frame_alloc(void)
112 AVFrame *frame = av_mallocz(sizeof(*frame));
117 frame->extended_data = NULL;
118 get_frame_defaults(frame);
123 void av_frame_free(AVFrame **frame)
125 if (!frame || !*frame)
128 av_frame_unref(*frame);
132 static int get_video_buffer(AVFrame *frame, int align)
134 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
138 return AVERROR(EINVAL);
140 if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
143 if (!frame->linesize[0]) {
144 for(i=1; i<=align; i+=i) {
145 ret = av_image_fill_linesizes(frame->linesize, frame->format,
146 FFALIGN(frame->width, i));
149 if (!(frame->linesize[0] & (align-1)))
153 for (i = 0; i < 4 && frame->linesize[i]; i++)
154 frame->linesize[i] = FFALIGN(frame->linesize[i], align);
157 for (i = 0; i < 4 && frame->linesize[i]; i++) {
158 int h = FFALIGN(frame->height, 32);
159 if (i == 1 || i == 2)
160 h = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
162 frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h + 16);
166 frame->data[i] = frame->buf[i]->data;
168 if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
169 av_buffer_unref(&frame->buf[1]);
170 frame->buf[1] = av_buffer_alloc(1024);
173 frame->data[1] = frame->buf[1]->data;
176 frame->extended_data = frame->data;
180 av_frame_unref(frame);
181 return AVERROR(ENOMEM);
184 static int get_audio_buffer(AVFrame *frame, int align)
186 int channels = frame->channels;
187 int planar = av_sample_fmt_is_planar(frame->format);
188 int planes = planar ? channels : 1;
191 CHECK_CHANNELS_CONSISTENCY(frame);
192 if (!frame->linesize[0]) {
193 ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
194 frame->nb_samples, frame->format,
200 if (planes > AV_NUM_DATA_POINTERS) {
201 frame->extended_data = av_mallocz(planes *
202 sizeof(*frame->extended_data));
203 frame->extended_buf = av_mallocz((planes - AV_NUM_DATA_POINTERS) *
204 sizeof(*frame->extended_buf));
205 if (!frame->extended_data || !frame->extended_buf) {
206 av_freep(&frame->extended_data);
207 av_freep(&frame->extended_buf);
208 return AVERROR(ENOMEM);
210 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
212 frame->extended_data = frame->data;
214 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
215 frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
216 if (!frame->buf[i]) {
217 av_frame_unref(frame);
218 return AVERROR(ENOMEM);
220 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
222 for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
223 frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
224 if (!frame->extended_buf[i]) {
225 av_frame_unref(frame);
226 return AVERROR(ENOMEM);
228 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
234 int av_frame_get_buffer(AVFrame *frame, int align)
236 if (frame->format < 0)
237 return AVERROR(EINVAL);
239 if (frame->width > 0 && frame->height > 0)
240 return get_video_buffer(frame, align);
241 else if (frame->nb_samples > 0 && (frame->channel_layout || frame->channels > 0))
242 return get_audio_buffer(frame, align);
244 return AVERROR(EINVAL);
247 int av_frame_ref(AVFrame *dst, AVFrame *src)
251 dst->format = src->format;
252 dst->width = src->width;
253 dst->height = src->height;
254 dst->channels = src->channels;
255 dst->channel_layout = src->channel_layout;
256 dst->nb_samples = src->nb_samples;
258 ret = av_frame_copy_props(dst, src);
262 /* duplicate the frame data if it's not refcounted */
264 ret = av_frame_get_buffer(dst, 32);
268 if (src->nb_samples) {
269 int ch = src->channels;
270 CHECK_CHANNELS_CONSISTENCY(src);
271 av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
272 dst->nb_samples, ch, dst->format);
274 av_image_copy(dst->data, dst->linesize, src->data, src->linesize,
275 dst->format, dst->width, dst->height);
280 /* ref the buffers */
281 for (i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) {
284 dst->buf[i] = av_buffer_ref(src->buf[i]);
286 ret = AVERROR(ENOMEM);
291 if (src->extended_buf) {
292 dst->extended_buf = av_mallocz(sizeof(*dst->extended_buf) *
293 src->nb_extended_buf);
294 if (!dst->extended_buf) {
295 ret = AVERROR(ENOMEM);
298 dst->nb_extended_buf = src->nb_extended_buf;
300 for (i = 0; i < src->nb_extended_buf; i++) {
301 dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
302 if (!dst->extended_buf[i]) {
303 ret = AVERROR(ENOMEM);
309 /* duplicate extended data */
310 if (src->extended_data != src->data) {
311 int ch = src->channels;
314 ret = AVERROR(EINVAL);
317 CHECK_CHANNELS_CONSISTENCY(src);
319 dst->extended_data = av_malloc(sizeof(*dst->extended_data) * ch);
320 if (!dst->extended_data) {
321 ret = AVERROR(ENOMEM);
324 memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
326 dst->extended_data = dst->data;
328 memcpy(dst->data, src->data, sizeof(src->data));
329 memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
338 AVFrame *av_frame_clone(AVFrame *src)
340 AVFrame *ret = av_frame_alloc();
345 if (av_frame_ref(ret, src) < 0)
351 void av_frame_unref(AVFrame *frame)
355 for (i = 0; i < frame->nb_side_data; i++) {
356 av_freep(&frame->side_data[i]->data);
357 av_dict_free(&frame->side_data[i]->metadata);
358 av_freep(&frame->side_data[i]);
360 av_freep(&frame->side_data);
362 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
363 av_buffer_unref(&frame->buf[i]);
364 for (i = 0; i < frame->nb_extended_buf; i++)
365 av_buffer_unref(&frame->extended_buf[i]);
366 av_freep(&frame->extended_buf);
367 av_dict_free(&frame->metadata);
368 av_buffer_unref(&frame->qp_table_buf);
370 get_frame_defaults(frame);
373 void av_frame_move_ref(AVFrame *dst, AVFrame *src)
376 if (src->extended_data == src->data)
377 dst->extended_data = dst->data;
378 memset(src, 0, sizeof(*src));
379 get_frame_defaults(src);
382 int av_frame_is_writable(AVFrame *frame)
386 /* assume non-refcounted frames are not writable */
390 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
392 ret &= !!av_buffer_is_writable(frame->buf[i]);
393 for (i = 0; i < frame->nb_extended_buf; i++)
394 ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
399 int av_frame_make_writable(AVFrame *frame)
405 return AVERROR(EINVAL);
407 if (av_frame_is_writable(frame))
410 memset(&tmp, 0, sizeof(tmp));
411 tmp.format = frame->format;
412 tmp.width = frame->width;
413 tmp.height = frame->height;
414 tmp.channels = frame->channels;
415 tmp.channel_layout = frame->channel_layout;
416 tmp.nb_samples = frame->nb_samples;
417 ret = av_frame_get_buffer(&tmp, 32);
421 if (tmp.nb_samples) {
422 int ch = tmp.channels;
423 CHECK_CHANNELS_CONSISTENCY(&tmp);
424 av_samples_copy(tmp.extended_data, frame->extended_data, 0, 0,
425 frame->nb_samples, ch, frame->format);
427 av_image_copy(tmp.data, tmp.linesize, frame->data, frame->linesize,
428 frame->format, frame->width, frame->height);
431 ret = av_frame_copy_props(&tmp, frame);
433 av_frame_unref(&tmp);
437 av_frame_unref(frame);
440 if (tmp.data == tmp.extended_data)
441 frame->extended_data = frame->data;
446 int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
450 dst->key_frame = src->key_frame;
451 dst->pict_type = src->pict_type;
452 dst->sample_aspect_ratio = src->sample_aspect_ratio;
454 dst->repeat_pict = src->repeat_pict;
455 dst->interlaced_frame = src->interlaced_frame;
456 dst->top_field_first = src->top_field_first;
457 dst->palette_has_changed = src->palette_has_changed;
458 dst->sample_rate = src->sample_rate;
459 dst->opaque = src->opaque;
460 #if FF_API_AVFRAME_LAVC
461 dst->type = src->type;
463 dst->pkt_pts = src->pkt_pts;
464 dst->pkt_dts = src->pkt_dts;
465 dst->pkt_pos = src->pkt_pos;
466 dst->pkt_size = src->pkt_size;
467 dst->pkt_duration = src->pkt_duration;
468 dst->reordered_opaque = src->reordered_opaque;
469 dst->quality = src->quality;
470 dst->best_effort_timestamp = src->best_effort_timestamp;
471 dst->coded_picture_number = src->coded_picture_number;
472 dst->display_picture_number = src->display_picture_number;
473 dst->decode_error_flags = src->decode_error_flags;
474 dst->colorspace = src->colorspace;
475 dst->color_range = src->color_range;
477 av_dict_copy(&dst->metadata, src->metadata, 0);
479 memcpy(dst->error, src->error, sizeof(dst->error));
481 for (i = 0; i < src->nb_side_data; i++) {
482 const AVFrameSideData *sd_src = src->side_data[i];
483 AVFrameSideData *sd_dst = av_frame_new_side_data(dst, sd_src->type,
486 for (i = 0; i < dst->nb_side_data; i++) {
487 av_freep(&dst->side_data[i]->data);
488 av_freep(&dst->side_data[i]);
489 av_dict_free(&dst->side_data[i]->metadata);
491 av_freep(&dst->side_data);
492 return AVERROR(ENOMEM);
494 memcpy(sd_dst->data, sd_src->data, sd_src->size);
495 av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
498 dst->qscale_table = NULL;
500 dst->qscale_type = 0;
501 if (src->qp_table_buf) {
502 dst->qp_table_buf = av_buffer_ref(src->qp_table_buf);
503 if (dst->qp_table_buf) {
504 dst->qscale_table = dst->qp_table_buf->data;
505 dst->qstride = src->qstride;
506 dst->qscale_type = src->qscale_type;
513 AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
518 if (frame->nb_samples) {
519 int channels = frame->channels;
522 CHECK_CHANNELS_CONSISTENCY(frame);
523 planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
527 if (plane < 0 || plane >= planes || !frame->extended_data[plane])
529 data = frame->extended_data[plane];
531 for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
532 AVBufferRef *buf = frame->buf[i];
533 if (data >= buf->data && data < buf->data + buf->size)
536 for (i = 0; i < frame->nb_extended_buf; i++) {
537 AVBufferRef *buf = frame->extended_buf[i];
538 if (data >= buf->data && data < buf->data + buf->size)
544 AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
545 enum AVFrameSideDataType type,
548 AVFrameSideData *ret, **tmp;
550 if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
553 tmp = av_realloc(frame->side_data,
554 (frame->nb_side_data + 1) * sizeof(*frame->side_data));
557 frame->side_data = tmp;
559 ret = av_mallocz(sizeof(*ret));
563 ret->data = av_malloc(size);
572 frame->side_data[frame->nb_side_data++] = ret;
577 AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
578 enum AVFrameSideDataType type)
582 for (i = 0; i < frame->nb_side_data; i++) {
583 if (frame->side_data[i]->type == type)
584 return frame->side_data[i];