FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
h264dsp.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Martin Storsjo
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 #include <string.h>
22 #include "checkasm.h"
23 #include "libavcodec/avcodec.h"
24 #include "libavcodec/h264dsp.h"
25 #include "libavutil/common.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/intreadwrite.h"
28 
29 static const uint32_t pixel_mask[3] = { 0xffffffff, 0x01ff01ff, 0x03ff03ff };
30 
31 #define SIZEOF_PIXEL ((bit_depth + 7) / 8)
32 #define SIZEOF_COEF (2 * ((bit_depth + 7) / 8))
33 #define PIXEL_STRIDE 16
34 
35 #define randomize_buffers() \
36  do { \
37  uint32_t mask = pixel_mask[bit_depth - 8]; \
38  for (y = 0; y < sz; y++) { \
39  for (x = 0; x < PIXEL_STRIDE; x += 4) { \
40  AV_WN32A(src + y * PIXEL_STRIDE + x, rnd() & mask); \
41  AV_WN32A(dst + y * PIXEL_STRIDE + x, rnd() & mask); \
42  } \
43  for (x = 0; x < sz; x++) { \
44  if (bit_depth == 8) { \
45  coef[y * sz + x] = src[y * PIXEL_STRIDE + x] - \
46  dst[y * PIXEL_STRIDE + x]; \
47  } else { \
48  ((int32_t *)coef)[y * sz + x] = \
49  ((uint16_t *)src)[y * (PIXEL_STRIDE/2) + x] - \
50  ((uint16_t *)dst)[y * (PIXEL_STRIDE/2) + x]; \
51  } \
52  } \
53  } \
54  } while (0)
55 
56 #define dct4x4_impl(size, dctcoef) \
57 static void dct4x4_##size(dctcoef *coef) \
58 { \
59  int i, y, x; \
60  dctcoef tmp[16]; \
61  for (i = 0; i < 4; i++) { \
62  const int z0 = coef[i*4 + 0] + coef[i*4 + 3]; \
63  const int z1 = coef[i*4 + 1] + coef[i*4 + 2]; \
64  const int z2 = coef[i*4 + 0] - coef[i*4 + 3]; \
65  const int z3 = coef[i*4 + 1] - coef[i*4 + 2]; \
66  tmp[i + 4*0] = z0 + z1; \
67  tmp[i + 4*1] = 2*z2 + z3; \
68  tmp[i + 4*2] = z0 - z1; \
69  tmp[i + 4*3] = z2 - 2*z3; \
70  } \
71  for (i = 0; i < 4; i++) { \
72  const int z0 = tmp[i*4 + 0] + tmp[i*4 + 3]; \
73  const int z1 = tmp[i*4 + 1] + tmp[i*4 + 2]; \
74  const int z2 = tmp[i*4 + 0] - tmp[i*4 + 3]; \
75  const int z3 = tmp[i*4 + 1] - tmp[i*4 + 2]; \
76  coef[i*4 + 0] = z0 + z1; \
77  coef[i*4 + 1] = 2*z2 + z3; \
78  coef[i*4 + 2] = z0 - z1; \
79  coef[i*4 + 3] = z2 - 2*z3; \
80  } \
81  for (y = 0; y < 4; y++) { \
82  for (x = 0; x < 4; x++) { \
83  static const int scale[] = { 13107 * 10, 8066 * 13, 5243 * 16 }; \
84  const int idx = (y & 1) + (x & 1); \
85  coef[y*4 + x] = (coef[y*4 + x] * scale[idx] + (1 << 14)) >> 15; \
86  } \
87  } \
88 }
89 
90 #define DCT8_1D(src, srcstride, dst, dststride) do { \
91  const int a0 = (src)[srcstride * 0] + (src)[srcstride * 7]; \
92  const int a1 = (src)[srcstride * 0] - (src)[srcstride * 7]; \
93  const int a2 = (src)[srcstride * 1] + (src)[srcstride * 6]; \
94  const int a3 = (src)[srcstride * 1] - (src)[srcstride * 6]; \
95  const int a4 = (src)[srcstride * 2] + (src)[srcstride * 5]; \
96  const int a5 = (src)[srcstride * 2] - (src)[srcstride * 5]; \
97  const int a6 = (src)[srcstride * 3] + (src)[srcstride * 4]; \
98  const int a7 = (src)[srcstride * 3] - (src)[srcstride * 4]; \
99  const int b0 = a0 + a6; \
100  const int b1 = a2 + a4; \
101  const int b2 = a0 - a6; \
102  const int b3 = a2 - a4; \
103  const int b4 = a3 + a5 + (a1 + (a1 >> 1)); \
104  const int b5 = a1 - a7 - (a5 + (a5 >> 1)); \
105  const int b6 = a1 + a7 - (a3 + (a3 >> 1)); \
106  const int b7 = a3 - a5 + (a7 + (a7 >> 1)); \
107  (dst)[dststride * 0] = b0 + b1; \
108  (dst)[dststride * 1] = b4 + (b7 >> 2); \
109  (dst)[dststride * 2] = b2 + (b3 >> 1); \
110  (dst)[dststride * 3] = b5 + (b6 >> 2); \
111  (dst)[dststride * 4] = b0 - b1; \
112  (dst)[dststride * 5] = b6 - (b5 >> 2); \
113  (dst)[dststride * 6] = (b2 >> 1) - b3; \
114  (dst)[dststride * 7] = (b4 >> 2) - b7; \
115 } while (0)
116 
117 #define dct8x8_impl(size, dctcoef) \
118 static void dct8x8_##size(dctcoef *coef) \
119 { \
120  int i, x, y; \
121  dctcoef tmp[64]; \
122  for (i = 0; i < 8; i++) \
123  DCT8_1D(coef + i, 8, tmp + i, 8); \
124  \
125  for (i = 0; i < 8; i++) \
126  DCT8_1D(tmp + 8*i, 1, coef + i, 8); \
127  \
128  for (y = 0; y < 8; y++) { \
129  for (x = 0; x < 8; x++) { \
130  static const int scale[] = { \
131  13107 * 20, 11428 * 18, 20972 * 32, \
132  12222 * 19, 16777 * 25, 15481 * 24, \
133  }; \
134  static const int idxmap[] = { \
135  0, 3, 4, 3, \
136  3, 1, 5, 1, \
137  4, 5, 2, 5, \
138  3, 1, 5, 1, \
139  }; \
140  const int idx = idxmap[(y & 3) * 4 + (x & 3)]; \
141  coef[y*8 + x] = ((int64_t)coef[y*8 + x] * \
142  scale[idx] + (1 << 17)) >> 18; \
143  } \
144  } \
145 }
146 
147 dct4x4_impl(16, int16_t)
148 dct4x4_impl(32, int32_t)
149 
150 dct8x8_impl(16, int16_t)
151 dct8x8_impl(32, int32_t)
152 
153 static void dct4x4(int16_t *coef, int bit_depth)
154 {
155  if (bit_depth == 8)
156  dct4x4_16(coef);
157  else
158  dct4x4_32((int32_t *) coef);
159 }
160 
161 static void dct8x8(int16_t *coef, int bit_depth)
162 {
163  if (bit_depth == 8) {
164  dct8x8_16(coef);
165  } else {
166  dct8x8_32((int32_t *) coef);
167  }
168 }
169 
170 
171 static void check_idct(void)
172 {
173  LOCAL_ALIGNED_16(uint8_t, src, [8 * 8 * 2]);
174  LOCAL_ALIGNED_16(uint8_t, dst, [8 * 8 * 2]);
175  LOCAL_ALIGNED_16(uint8_t, dst0, [8 * 8 * 2]);
176  LOCAL_ALIGNED_16(uint8_t, dst1_base, [8 * 8 * 2 + 32]);
177  LOCAL_ALIGNED_16(int16_t, coef, [8 * 8 * 2]);
178  LOCAL_ALIGNED_16(int16_t, subcoef0, [8 * 8 * 2]);
179  LOCAL_ALIGNED_16(int16_t, subcoef1, [8 * 8 * 2]);
181  int bit_depth, sz, align;
182  int x, y, dc;
183  declare_func_emms(AV_CPU_FLAG_MMX, void, uint8_t *dst, int16_t *block, int stride);
184 
185  for (bit_depth = 8; bit_depth <= 10; bit_depth++) {
186  ff_h264dsp_init(&h, bit_depth, 1);
187  for (sz = 4; sz <= 8; sz += 4) {
189 
190  if (sz == 4)
191  dct4x4(coef, bit_depth);
192  else
193  dct8x8(coef, bit_depth);
194 
195  for (dc = 0; dc <= 1; dc++) {
196  void (*idct)(uint8_t *, int16_t *, int) = NULL;
197  switch ((sz << 1) | dc) {
198  case (4 << 1) | 0: idct = h.h264_idct_add; break;
199  case (4 << 1) | 1: idct = h.h264_idct_dc_add; break;
200  case (8 << 1) | 0: idct = h.h264_idct8_add; break;
201  case (8 << 1) | 1: idct = h.h264_idct8_dc_add; break;
202  }
203  if (check_func(idct, "h264_idct%d_add%s_%dbpp", sz, dc ? "_dc" : "", bit_depth)) {
204  for (align = 0; align < 16; align += sz * SIZEOF_PIXEL) {
205  uint8_t *dst1 = dst1_base + align;
206  if (dc) {
207  memset(subcoef0, 0, sz * sz * SIZEOF_COEF);
208  memcpy(subcoef0, coef, SIZEOF_COEF);
209  } else {
210  memcpy(subcoef0, coef, sz * sz * SIZEOF_COEF);
211  }
212  memcpy(dst0, dst, sz * PIXEL_STRIDE);
213  memcpy(dst1, dst, sz * PIXEL_STRIDE);
214  memcpy(subcoef1, subcoef0, sz * sz * SIZEOF_COEF);
215  call_ref(dst0, subcoef0, PIXEL_STRIDE);
216  call_new(dst1, subcoef1, PIXEL_STRIDE);
217  if (memcmp(dst0, dst1, sz * PIXEL_STRIDE) ||
218  memcmp(subcoef0, subcoef1, sz * sz * SIZEOF_COEF))
219  fail();
220  bench_new(dst1, subcoef1, sz * SIZEOF_PIXEL);
221  }
222  }
223  }
224  }
225  }
226  report("idct");
227 }
228 
230 {
231  check_idct();
232 }
void(* h264_idct_add)(uint8_t *dst, int16_t *block, int stride)
Definition: h264dsp.h:81
#define NULL
Definition: coverity.c:32
static void idct(int16_t block[64])
Definition: 4xm.c:163
static void check_idct(void)
Definition: h264dsp.c:171
#define dct8x8_impl(size, dctcoef)
Definition: h264dsp.c:117
static const uint32_t pixel_mask[3]
Definition: h264dsp.c:29
H.264 DSP functions.
#define report
Definition: checkasm.h:86
static int16_t block[64]
Definition: dct.c:113
uint8_t
static void dct8x8(int16_t *coef, int bit_depth)
Definition: h264dsp.c:161
void(* h264_idct8_add)(uint8_t *dst, int16_t *block, int stride)
Definition: h264dsp.h:83
void checkasm_check_h264dsp(void)
Definition: h264dsp.c:229
#define fail()
Definition: checkasm.h:83
common internal API header
Context for storing H.264 DSP functions.
Definition: h264dsp.h:42
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
static void bit_depth(AudioStatsContext *s, uint64_t mask, uint64_t imask, AVRational *depth)
Definition: af_astats.c:150
void(* h264_idct_dc_add)(uint8_t *dst, int16_t *block, int stride)
Definition: h264dsp.h:85
int32_t
#define src
Definition: vp9dsp.c:530
#define declare_func_emms(cpu_flags, ret,...)
Definition: checkasm.h:80
#define call_ref(...)
Definition: checkasm.h:89
const AVS_VideoInfo int align
Definition: avisynth_c.h:795
Libavcodec external API header.
#define PIXEL_STRIDE
Definition: h264dsp.c:33
#define SIZEOF_PIXEL
Definition: h264dsp.c:31
#define AV_CPU_FLAG_MMX
standard MMX
Definition: cpu.h:29
#define check_func(func,...)
Definition: checkasm.h:75
#define SIZEOF_COEF
Definition: h264dsp.c:32
void(* h264_idct8_dc_add)(uint8_t *dst, int16_t *block, int stride)
Definition: h264dsp.h:87
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
common internal and external API header
#define dct4x4_impl(size, dctcoef)
Definition: h264dsp.c:56
#define bench_new(...)
Definition: checkasm.h:176
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> dc
av_cold void ff_h264dsp_init(H264DSPContext *c, const int bit_depth, const int chroma_format_idc)
Definition: h264dsp.c:67
#define LOCAL_ALIGNED_16(t, v,...)
Definition: internal.h:121
#define call_new(...)
Definition: checkasm.h:144
#define randomize_buffers()
Definition: h264dsp.c:35