FFmpeg
scale_slice_test.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 <stdio.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22 
23 #include "decode_simple.h"
24 
25 #include "libavutil/common.h"
26 #include "libavutil/pixdesc.h"
27 #include "libavutil/error.h"
28 #include "libavutil/lfg.h"
29 #include "libavutil/random_seed.h"
31 
32 #include "libavformat/avformat.h"
33 
34 #include "libavcodec/avcodec.h"
35 
36 #include "libswscale/swscale.h"
37 
38 typedef struct PrivData {
39  unsigned int random_seed;
41 
42  struct SwsContext *scaler;
43 
46 
49 } PrivData;
50 
52 {
53  PrivData *pd = dc->opaque;
54  int slice_start = 0;
55  int ret;
56 
57  if (!frame)
58  return 0;
59 
60  if (!pd->scaler) {
61  pd->scaler = sws_getContext(frame->width, frame->height, frame->format,
62  pd->frame_ref->width, pd->frame_ref->height,
63  pd->frame_ref->format, 0, NULL, NULL, NULL);
64  if (!pd->scaler)
65  return AVERROR(ENOMEM);
66 
68  }
69 
70  /* scale the whole input frame as reference */
71  ret = sws_scale(pd->scaler, (const uint8_t **)frame->data, frame->linesize, 0, frame->height,
72  pd->frame_ref->data, pd->frame_ref->linesize);
73  if (ret < 0)
74  return ret;
75 
76  /* scale slices with randomly generated heights */
77  while (slice_start < frame->height) {
78  int slice_height;
79  const uint8_t *src[4];
80 
81  slice_height = av_lfg_get(&pd->lfg) % (frame->height - slice_start);
82  slice_height = FFALIGN(FFMAX(1, slice_height), 1 << pd->v_shift_src);
83 
84  for (int j = 0; j < FF_ARRAY_ELEMS(src) && frame->data[j]; j++) {
85  int shift = (j == 1 || j == 2) ? pd->v_shift_src : 0;
86  src[j] = frame->data[j] + frame->linesize[j] * (slice_start >> shift);
87  }
88 
89  ret = sws_scale(pd->scaler, src, frame->linesize, slice_start, slice_height,
90  pd->frame_dst->data, pd->frame_dst->linesize);
91  if (ret < 0)
92  return ret;
93 
94  slice_start += slice_height;
95  }
96 
97  /* compare the two results */
98  for (int i = 0; i < 4 && pd->frame_ref->data[i]; i++) {
99  int shift = (i == 1 || i == 2) ? pd->v_shift_dst : 0;
100 
101  if (memcmp(pd->frame_ref->data[i], pd->frame_dst->data[i],
102  pd->frame_ref->linesize[i] * (pd->frame_ref->height >> shift))) {
103  fprintf(stderr, "mismatch frame %d seed %u\n",
104  dc->decoder->frame_number - 1, pd->random_seed);
105  return AVERROR(EINVAL);
106  }
107  }
108 
109  return 0;
110 }
111 
112 int main(int argc, char **argv)
113 {
114  PrivData pd;
116 
117  int width, height;
118  enum AVPixelFormat pix_fmt;
119  const char *filename;
120  int ret = 0;
121 
122  if (argc <= 4) {
123  fprintf(stderr,
124  "Usage: %s <input file> <dst width> <dst height> <dst pixfmt> [<random seed>] \n",
125  argv[0]);
126  return 0;
127  }
128 
129  memset(&pd, 0, sizeof(pd));
130 
131  filename = argv[1];
132  width = strtol(argv[2], NULL, 0);
133  height = strtol(argv[3], NULL, 0);
134  pix_fmt = av_get_pix_fmt(argv[4]);
135 
136  /* init RNG for generating slice sizes */
137  if (argc >= 6)
138  pd.random_seed = strtoul(argv[5], NULL, 0);
139  else
141 
142  av_lfg_init(&pd.lfg, pd.random_seed);
143 
145 
146  /* allocate the frames for scaler output */
147  for (int i = 0; i < 2; i++) {
149  if (!frame) {
150  fprintf(stderr, "Error allocating frames\n");
151  return AVERROR(ENOMEM);
152  }
153 
154  frame->width = width;
155  frame->height = height;
156  frame->format = pix_fmt;
157 
159  if (ret < 0) {
160  fprintf(stderr, "Error allocating frame data\n");
161  return ret;
162  }
163 
164  /* make sure the padding is zeroed */
165  for (int j = 0; j < 4 && frame->data[j]; j++) {
166  int shift = (j == 1 || j == 2) ? pd.v_shift_dst : 0;
167  memset(frame->data[j], 0,
168  frame->linesize[j] * (height >> shift));
169  }
170  if (i) pd.frame_ref = frame;
171  else pd.frame_dst = frame;
172  }
173 
174  ret = ds_open(&dc, filename, 0);
175  if (ret < 0) {
176  fprintf(stderr, "Error opening the file\n");
177  return ret;
178  }
179 
180  dc.process_frame = process_frame;
181  dc.opaque = &pd;
182 
183  ret = ds_run(&dc);
184 
188  ds_free(&dc);
189  return ret;
190 }
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
av_frame_get_buffer
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition: frame.c:246
av_lfg_init
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:32
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:112
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
pixdesc.h
main
int main(int argc, char **argv)
Definition: scale_slice_test.c:112
AVFrame::width
int width
Definition: frame.h:361
PrivData::v_shift_src
int v_shift_src
Definition: scale_slice_test.c:45
sws_scale
int attribute_align_arg sws_scale(struct SwsContext *c, const uint8_t *const srcSlice[], const int srcStride[], int srcSliceY, int srcSliceH, uint8_t *const dst[], const int dstStride[])
swscale wrapper, so we don't need to export the SwsContext.
Definition: swscale.c:1204
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
ds_open
int ds_open(DecodeContext *dc, const char *url, int stream_idx)
Definition: decode_simple.c:119
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:317
av_get_random_seed
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:120
PrivData::h_shift_src
int h_shift_src
Definition: scale_slice_test.c:45
av_pix_fmt_get_chroma_sub_sample
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: pixdesc.c:2592
ds_run
int ds_run(DecodeContext *dc)
Definition: decode_simple.c:65
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:99
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
width
#define width
av_lfg_get
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:53
lfg.h
pix_fmt
static enum AVPixelFormat pix_fmt
Definition: demuxing_decoding.c:41
PrivData::random_seed
unsigned int random_seed
Definition: scale_slice_test.c:39
if
if(ret)
Definition: filter_design.txt:179
PrivData::scaler
struct SwsContext * scaler
Definition: scale_slice_test.c:42
NULL
#define NULL
Definition: coverity.c:32
PrivData::frame_dst
AVFrame * frame_dst
Definition: scale_slice_test.c:48
src
#define src
Definition: vp8dsp.c:255
error.h
AVLFG
Context structure for the Lagged Fibonacci PRNG.
Definition: lfg.h:33
dc
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled top and top right vectors is used as motion vector prediction the used motion vector is the sum of the predictor and(mvx_diff, mvy_diff) *mv_scale Intra DC Prediction block[y][x] dc[1]
Definition: snow.txt:400
sws_getContext
struct SwsContext * sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat, int dstW, int dstH, enum AVPixelFormat dstFormat, int flags, SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
Allocate and return an SwsContext.
Definition: utils.c:2003
PrivData::frame_ref
AVFrame * frame_ref
Definition: scale_slice_test.c:47
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:376
height
#define height
process_frame
static int process_frame(DecodeContext *dc, AVFrame *frame)
Definition: scale_slice_test.c:51
PrivData::lfg
AVLFG lfg
Definition: scale_slice_test.c:40
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
PrivData::v_shift_dst
int v_shift_dst
Definition: scale_slice_test.c:44
common.h
avcodec.h
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
avformat.h
av_get_pix_fmt
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:2496
AVFrame::height
int height
Definition: frame.h:361
PrivData::h_shift_dst
int h_shift_dst
Definition: scale_slice_test.c:44
random_seed.h
sws_freeContext
void sws_freeContext(struct SwsContext *swsContext)
Free the swscaler context swsContext.
Definition: utils.c:2334
PrivData
Definition: scale_slice_test.c:38
shift
static int shift(int a, int b)
Definition: sonic.c:83
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
ds_free
void ds_free(DecodeContext *dc)
Definition: decode_simple.c:108
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:334
decode_simple.h
SwsContext
Definition: swscale_internal.h:300
DecodeContext
Definition: decode_simple.h:33
swscale.h
video_enc_params.h