FFmpeg
Loading...
Searching...
No Matches
frame.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg 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.
8 *
9 * FFmpeg 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.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include <inttypes.h>
20#include <limits.h>
21#include <stdint.h>
22#include <stdio.h>
23#include <string.h>
24
25#include "libavutil/adler32.h"
26#include "libavutil/avassert.h"
28#include "libavutil/error.h"
29#include "libavutil/frame.h"
30#include "libavutil/macros.h"
31#include "libavutil/pixdesc.h"
32#include "libavutil/samplefmt.h"
33
34/* strerror() text differs between C libraries */
35static const char *err(int ret)
36{
37 return ret >= 0 ? "0" :
38 ret == AVERROR(EINVAL) ? "EINVAL" :
39 ret == AVERROR(ERANGE) ? "ERANGE" :
40 ret == AVERROR(ENOMEM) ? "ENOMEM" : "unexpected error";
41}
42
43static AVFrame *video(enum AVPixelFormat fmt, int w, int h)
44{
47 f->format = fmt;
48 f->width = w;
49 f->height = h;
51 return f;
52}
53
54static AVFrame *audio(enum AVSampleFormat fmt, int channels, int nb_samples)
55{
58 int size = nb_samples * av_get_bytes_per_sample(fmt) * (channels / planes);
59
61 f->format = fmt;
62 f->nb_samples = nb_samples;
65 for (int p = 0; p < planes; p++)
66 for (int i = 0; i < size; i++)
67 f->extended_data[p][i] = i * 5 + p * 13;
68 return f;
69}
70
71static uint32_t audio_sum(const AVFrame *f)
72{
73 int planar = av_sample_fmt_is_planar(f->format);
74 int planes = planar ? f->ch_layout.nb_channels : 1;
75 int size = f->nb_samples * av_get_bytes_per_sample(f->format) *
76 (planar ? 1 : f->ch_layout.nb_channels);
77 uint32_t sum = 1;
78
79 for (int p = 0; p < planes; p++)
80 sum = av_adler32_update(sum, f->extended_data[p], size);
81 return sum;
82}
83
84static void print_audio(const char *name, const AVFrame *f)
85{
86 printf("%s: %d ch, %d samples, nb_extended_buf %d, extended_data %s, sum %08"PRIx32"\n",
87 name, f->ch_layout.nb_channels, f->nb_samples, f->nb_extended_buf,
88 f->extended_data == f->data ? "data" : "own", audio_sum(f));
89}
90
91static void test_replace(void)
92{
93 AVFrame *stereo = audio(AV_SAMPLE_FMT_FLTP, 2, 64);
94 AVFrame *ten = audio(AV_SAMPLE_FMT_FLTP, 10, 64);
95 AVFrame *twelve = audio(AV_SAMPLE_FMT_S16P, 12, 32);
96 AVFrame *packed = audio(AV_SAMPLE_FMT_S16, 10, 64);
98
100 print_audio("stereo", stereo);
101 print_audio("ten", ten);
102 print_audio("twelve", twelve);
103 print_audio("packed", packed);
104
105 /* grow and shrink extended_buf on one destination */
106 printf("replace twelve: %s\n", err(av_frame_replace(dst, twelve)));
107 print_audio("dst", dst);
108 printf("replace ten: %s\n", err(av_frame_replace(dst, ten)));
109 print_audio("dst", dst);
110 printf("replace stereo: %s\n", err(av_frame_replace(dst, stereo)));
111 print_audio("dst", dst);
112 printf("replace twelve: %s\n", err(av_frame_replace(dst, twelve)));
113 print_audio("dst", dst);
114 printf("replace packed: %s\n", err(av_frame_replace(dst, packed)));
115 print_audio("dst", dst);
117 printf("replace ten: %s\n", err(av_frame_replace(dst, ten)));
118 print_audio("dst", dst);
119
121 av_frame_free(&packed);
122 av_frame_free(&twelve);
123 av_frame_free(&ten);
124 av_frame_free(&stereo);
125}
126
127static void test_cropping(void)
128{
129 static const struct {
130 const char *name;
131 enum AVPixelFormat fmt;
132 size_t left, right, top, bottom;
133 int flags;
134 } tests[] = {
135 { "2/4/2/6", AV_PIX_FMT_YUV420P, 2, 4, 2, 6, 0 },
136 { "2/4/2/6", AV_PIX_FMT_YUV420P, 2, 4, 2, 6, AV_FRAME_CROP_UNALIGNED },
137 { "40/0/0/0", AV_PIX_FMT_YUV420P, 40, 0, 0, 0, 0 },
138 { "3/1/1/1", AV_PIX_FMT_RGB24, 3, 1, 1, 1, AV_FRAME_CROP_UNALIGNED },
139 { "5/0/3/0", AV_PIX_FMT_PAL8, 5, 0, 3, 0, AV_FRAME_CROP_UNALIGNED },
140 { "0/8/0/4", AV_PIX_FMT_MONOWHITE, 0, 8, 0, 4, 0 },
141 { "32/32/0/0", AV_PIX_FMT_YUV420P, 32, 32, 0, 0, 0 },
142 { "0/0/24/24", AV_PIX_FMT_YUV420P, 0, 0, 24, 24, 0 },
143 { "INT_MAX/1/0/0", AV_PIX_FMT_YUV420P, INT_MAX, 1, 0, 0, 0 },
144 { "1/SIZE_MAX/0/0", AV_PIX_FMT_YUV420P, 1, SIZE_MAX, 0, 0, AV_FRAME_CROP_UNALIGNED },
145 { "0/0/1/SIZE_MAX", AV_PIX_FMT_YUV420P, 0, 0, 1, SIZE_MAX, AV_FRAME_CROP_UNALIGNED },
146 };
147
148 for (int i = 0; i < FF_ARRAY_ELEMS(tests); i++) {
149 AVFrame *f = video(tests[i].fmt, 64, 48);
150 uint8_t *data[4];
151 int ret;
152
153 memcpy(data, f->data, sizeof(data));
154 f->crop_left = tests[i].left;
155 f->crop_right = tests[i].right;
156 f->crop_top = tests[i].top;
157 f->crop_bottom = tests[i].bottom;
159 printf("crop %s %s%s: %s, %dx%d, crop fields %s, offsets",
161 tests[i].flags ? " unaligned" : "", err(ret), f->width, f->height,
162 !(f->crop_left | f->crop_right | f->crop_top | f->crop_bottom) ? "cleared" :
163 f->crop_left == tests[i].left && f->crop_right == tests[i].right &&
164 f->crop_top == tests[i].top && f->crop_bottom == tests[i].bottom ? "kept" : "changed");
165 for (int p = 0; p < 4 && data[p]; p++) {
166 ptrdiff_t off = f->data[p] - data[p];
167 /* linesize depends on the build, so print rows and bytes */
168 if (f->linesize[p])
169 printf(" %td+%td", off / f->linesize[p], off % f->linesize[p]);
170 else
171 printf(" %td", off);
172 }
173 printf("\n");
175 }
176
177 {
179 av_assert0(f);
180 printf("crop empty frame: %s\n", err(av_frame_apply_cropping(f, 0)));
182 }
183}
184
185int main(void)
186{
187 test_replace();
189 return 0;
190}
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
Public header for Adler-32 hash function implementation.
channels
Definition aptx.h:31
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
Public libavutil channel layout APIs header.
__device__ int printf(const char *,...)
error code definitions
int ret
Definition ffplay.c:391
const TestCase tests[]
Definition fifo_muxer.c:363
reference-counted frame API
AVAdler av_adler32_update(AVAdler adler, const uint8_t *buf, size_t len)
Calculate the Adler32 checksum of a buffer.
Definition adler32.c:44
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
#define AVERROR(e)
Definition error.h:45
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition frame.c:501
int av_frame_replace(AVFrame *dst, const AVFrame *src)
Ensure the destination frame refers to the same data described by the source frame,...
Definition frame.c:380
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition frame.c:210
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:53
int av_frame_apply_cropping(AVFrame *frame, int flags)
Crop the given video AVFrame according to its crop_left/crop_top/crop_right/ crop_bottom fields.
Definition frame.c:765
@ AV_FRAME_CROP_UNALIGNED
Apply the maximum possible cropping, even if it requires setting the AVFrame.data[] entries to unalig...
Definition frame.h:1057
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition samplefmt.c:115
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition samplefmt.c:109
AVSampleFormat
Audio sample formats.
Definition samplefmt.h:55
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition samplefmt.h:64
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition samplefmt.h:58
static const struct @257111027162314367033347246032313251342043035002 planes[]
uint8_t w
Definition llvidencdsp.c:39
Utility Preprocessor macros.
const char data[16]
Definition mxf.c:155
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition pixdesc.c:3404
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition pixfmt.h:75
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition pixfmt.h:84
@ AV_PIX_FMT_MONOWHITE
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb.
Definition pixfmt.h:82
const char * name
Definition qsvenc.c:142
#define FF_ARRAY_ELEMS(a)
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
static uint32_t audio_sum(const AVFrame *f)
Definition frame.c:71
static void print_audio(const char *name, const AVFrame *f)
Definition frame.c:84
static AVFrame * audio(enum AVSampleFormat fmt, int channels, int nb_samples)
Definition frame.c:54
static AVFrame * video(enum AVPixelFormat fmt, int w, int h)
Definition frame.c:43
int main(void)
Definition frame.c:185
static const char * err(int ret)
Definition frame.c:35
static void test_cropping(void)
Definition frame.c:127
static void test_replace(void)
Definition frame.c:91
int size