FFmpeg
Loading...
Searching...
No Matches
speedhqenc.c
Go to the documentation of this file.
1/*
2 * SpeedHQ encoder
3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2003 Alex Beregszaszi
5 * Copyright (c) 2003-2004 Michael Niedermayer
6 * Copyright (c) 2020 FFmpeg
7 *
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25/**
26 * @file
27 * SpeedHQ encoder.
28 */
29
30#include "libavutil/avassert.h"
31#include "libavutil/thread.h"
32
33#include "avcodec.h"
34#include "codec_internal.h"
35#include "mathops.h"
36#include "mpeg12data.h"
37#include "mpeg12vlc.h"
38#include "mpegvideo.h"
39#include "mpegvideodata.h"
40#include "mpegvideoenc.h"
41#include "put_bits.h"
42#include "rl.h"
43#include "speedhq.h"
44#include "speedhqenc.h"
45
46static uint8_t speedhq_max_level[MAX_LEVEL + 1];
47static uint8_t speedhq_index_run[MAX_RUN + 1];
48
49/* Exactly the same as MPEG-2, except little-endian. */
50static const uint16_t mpeg12_vlc_dc_lum_code_reversed[12] = {
51 0x1, 0x0, 0x2, 0x5, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF
52};
53static const uint16_t mpeg12_vlc_dc_chroma_code_reversed[12] = {
54 0x0, 0x2, 0x1, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF
55};
56
57/* simple include everything table for dc, first byte is bits
58 * number next 3 are code */
59static uint32_t speedhq_lum_dc_uni[512];
60static uint32_t speedhq_chr_dc_uni[512];
61
62static uint8_t uni_speedhq_ac_vlc_len[64 * 64 * 2];
63
69
100
102{
104 MPVEncContext *const s = &m->s;
105
107
108 put_bits_le(&s->pb, 8, 100 - s->c.qscale * 2); /* FIXME why doubled */
109 put_bits_le(&s->pb, 24, 4); /* no second field */
110
111 ctx->slice_start = 4;
112 /* length of first slice, will be filled out later */
113 put_bits_le(&s->pb, 24, 0);
114
115 return 0;
116}
117
119{
121 int slice_len;
122
123 flush_put_bits_le(&s->pb);
124 slice_len = put_bytes_output(&s->pb) - ctx->slice_start;
125 AV_WL24(s->pb.buf + ctx->slice_start, slice_len);
126
127 /* length of next slice, will be filled out later */
128 ctx->slice_start = put_bytes_output(&s->pb);
129 put_bits_le(&s->pb, 24, 0);
130}
131
132static inline void encode_dc(PutBitContext *pb, int diff, int component)
133{
134 unsigned int diff_u = diff + 255;
135 if (diff_u >= 511) {
136 int index;
137
138 if (diff < 0) {
139 index = av_log2_16bit(-2 * diff);
140 diff--;
141 } else {
142 index = av_log2_16bit(2 * diff);
143 }
144 if (component == 0)
145 put_bits_le(pb,
149 else
150 put_bits_le(pb,
154 } else {
155 if (component == 0)
156 put_bits_le(pb,
157 speedhq_lum_dc_uni[diff + 255] & 0xFF,
158 speedhq_lum_dc_uni[diff + 255] >> 8);
159 else
160 put_bits_le(pb,
161 speedhq_chr_dc_uni[diff + 255] & 0xFF,
162 speedhq_chr_dc_uni[diff + 255] >> 8);
163 }
164}
165
166static void encode_block(MPVEncContext *const s, const int16_t block[], int n)
167{
168 int alevel, level, last_non_zero, dc, i, j, run, last_index, sign;
169 int code;
170 int component, val;
171
172 /* DC coef */
173 component = (n <= 3 ? 0 : (n&1) + 1);
174 dc = block[0]; /* overflow is impossible */
175 val = s->last_dc[component] - dc; /* opposite of most codecs */
176 encode_dc(&s->pb, val, component);
177 s->last_dc[component] = dc;
178
179 /* now quantify & encode AC coefs */
180 last_non_zero = 0;
181 last_index = s->c.block_last_index[n];
182
183 for (i = 1; i <= last_index; i++) {
184 j = s->c.intra_scantable.permutated[i];
185 level = block[j];
186
187 /* encode using VLC */
188 if (level != 0) {
189 run = i - last_non_zero - 1;
190
191 alevel = level;
192 MASK_ABS(sign, alevel);
193 sign &= 1;
194
195 if (alevel <= speedhq_max_level[run]) {
196 code = speedhq_index_run[run] + alevel - 1;
197 /* store the VLC & sign at once */
200 } else {
201 /* escape seems to be pretty rare <5% so I do not optimize it;
202 * The following encodes the escape value 100000b together with
203 * run and level. */
204 put_bits_le(&s->pb, 6 + 6 + 12, 0x20 | run << 6 |
205 (level + 2048) << 12);
206 }
207 last_non_zero = i;
208 }
209 }
210 /* end of block; the values correspond to ff_speedhq_vlc_table[122] */
211 put_bits_le(&s->pb, 4, 6);
212}
213
214static void speedhq_encode_mb(MPVEncContext *const s, int16_t block[12][64],
215 int unused_x, int unused_y)
216{
217 int i;
218 for(i=0;i<6;i++) {
219 encode_block(s, block[i], i);
220 }
221 if (s->c.chroma_format == CHROMA_444) {
222 encode_block(s, block[8], 8);
223 encode_block(s, block[9], 9);
224
225 encode_block(s, block[6], 6);
226 encode_block(s, block[7], 7);
227
228 encode_block(s, block[10], 10);
229 encode_block(s, block[11], 11);
230 } else if (s->c.chroma_format == CHROMA_422) {
231 encode_block(s, block[6], 6);
232 encode_block(s, block[7], 7);
233 }
234
235 s->i_tex_bits += get_bits_diff(s);
236}
237
239{
240 static AVOnce init_static_once = AV_ONCE_INIT;
241 MPVMainEncContext *const m = avctx->priv_data;
242 MPVEncContext *const s = &m->s;
243 int ret;
244
245 if (avctx->width > 65500 || avctx->height > 65500) {
246 av_log(avctx, AV_LOG_ERROR, "SpeedHQ does not support resolutions above 65500x65500\n");
247 return AVERROR(EINVAL);
248 }
249
250 // border is not implemented correctly at the moment, see ticket #10078
251 if (avctx->width % 16) {
252 av_log(avctx, AV_LOG_ERROR, "width must be a multiple of 16\n");
254 }
255
256 switch (avctx->pix_fmt) {
258 avctx->codec_tag = MKTAG('S','H','Q','0');
259 break;
261 avctx->codec_tag = MKTAG('S','H','Q','2');
262 break;
264 avctx->codec_tag = MKTAG('S','H','Q','4');
265 break;
266 default:
267 av_unreachable("Already checked via CODEC_PIXFMTS");
268 }
269
271 s->encode_mb = speedhq_encode_mb;
272
273 s->min_qcoeff = -2048;
274 s->max_qcoeff = 2047;
275
276 s->intra_ac_vlc_length =
277 s->intra_ac_vlc_last_length =
278 s->intra_chroma_ac_vlc_length =
279 s->intra_chroma_ac_vlc_last_length = uni_speedhq_ac_vlc_len;
280
281 s->c.y_dc_scale_table =
282 s->c.c_dc_scale_table = ff_mpeg12_dc_scale_table[3];
283
284 ret = ff_mpv_encode_init(avctx);
285 if (ret < 0)
286 return ret;
287
288 ff_thread_once(&init_static_once, speedhq_init_static_data);
289
290 return 0;
291}
292
294 .p.name = "speedhq",
295 CODEC_LONG_NAME("NewTek SpeedHQ"),
296 .p.type = AVMEDIA_TYPE_VIDEO,
297 .p.id = AV_CODEC_ID_SPEEDHQ,
298 .p.priv_class = &ff_mpv_enc_class,
300 .priv_data_size = sizeof(SpeedHQEncContext),
303 .close = ff_mpv_encode_end,
304 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
305 .color_ranges = AVCOL_RANGE_MPEG,
307};
static double val(void *priv, double ch)
Definition aeval.c:77
const FFCodec ff_speedhq_encoder
Definition speedhqenc.c:293
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
Definition avassert.h:109
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define CODEC_PIXFMTS(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define av_zero_extend
Definition common.h:151
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
static int16_t block[64]
Definition dct.c:125
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static const uint8_t bits[8]
Definition fastaudio.c:100
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_SPEEDHQ
Definition codec_id.h:270
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int index
Definition gxfenc.c:90
#define av_log2
Definition intmath.h:84
#define av_log2_16bit
Definition intmath.h:85
#define AV_WL24(p, d)
#define av_cold
Definition attributes.h:117
#define AVOnce
Definition thread.h:202
static int ff_thread_once(char *control, void(*routine)(void))
Definition thread.h:205
#define AV_ONCE_INIT
Definition thread.h:203
#define MKTAG(a, b, c, d)
Definition macros.h:55
#define MASK_ABS(mask, level)
Definition mathops.h:169
const unsigned char ff_mpeg12_vlc_dc_chroma_bits[12]
Definition mpeg12data.c:63
const unsigned char ff_mpeg12_vlc_dc_lum_bits[12]
Definition mpeg12data.c:56
MPEG-1/2 tables.
av_cold void ff_mpeg1_init_uni_ac_vlc(const int8_t max_level[], const uint8_t index_run[], const uint16_t table_vlc[][2], uint8_t uni_ac_vlc_len[])
Definition mpeg12enc.c:103
MPEG-1/2 VLC.
mpegvideo header.
#define CHROMA_444
Definition mpegvideo.h:266
#define CHROMA_422
Definition mpegvideo.h:265
const AVClass ff_mpv_enc_class
av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pic_arg, int *got_packet)
av_cold int ff_mpv_encode_end(AVCodecContext *avctx)
const uint8_t ff_mpeg12_dc_scale_table[4][32]
mpegvideo header.
static int get_bits_diff(MPVEncContext *s)
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition pixfmt.h:766
@ 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_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
bitstream writer API
static void put_bits_le(PutBitContext *s, int n, BitBuf value)
Definition put_bits.h:263
static void flush_put_bits_le(PutBitContext *s)
Definition put_bits.h:174
static int put_bytes_output(const PutBitContext *s)
Definition put_bits.h:99
static void put_bits_assume_flushed(const PutBitContext *s)
Inform the compiler that a PutBitContext is flushed (i.e.
Definition put_bits.h:82
av_cold void ff_rl_init_level_run(uint8_t max_level[MAX_LEVEL+1], uint8_t index_run[MAX_RUN+1], const uint8_t table_run[], const uint8_t table_level[], int n)
Initialize max_level and index_run from table_run and table_level; this is equivalent to initializing...
Definition rl.c:26
rl header.
#define MAX_LEVEL
Definition rl.h:36
#define MAX_RUN
Definition rl.h:35
const uint8_t * code
Definition spdifenc.c:433
const uint8_t ff_speedhq_level[121]
Definition speedhq.c:62
const uint16_t ff_speedhq_vlc_table[SPEEDHQ_RL_NB_ELEMS+2][2]
Definition speedhq.c:26
const uint8_t ff_speedhq_run[121]
Definition speedhq.c:81
#define SPEEDHQ_RL_NB_ELEMS
Definition speedhq.h:27
static uint8_t speedhq_index_run[MAX_RUN+1]
Definition speedhqenc.c:47
static uint32_t speedhq_lum_dc_uni[512]
Definition speedhqenc.c:59
static av_cold int speedhq_encode_init(AVCodecContext *avctx)
Definition speedhqenc.c:238
static void encode_dc(PutBitContext *pb, int diff, int component)
Definition speedhqenc.c:132
static int speedhq_encode_picture_header(MPVMainEncContext *const m)
Definition speedhqenc.c:101
static void encode_block(MPVEncContext *const s, const int16_t block[], int n)
Definition speedhqenc.c:166
static const uint16_t mpeg12_vlc_dc_lum_code_reversed[12]
Definition speedhqenc.c:50
static uint8_t speedhq_max_level[MAX_LEVEL+1]
Definition speedhqenc.c:46
static av_cold void speedhq_init_static_data(void)
Definition speedhqenc.c:70
void ff_speedhq_end_slice(MPVEncContext *const s)
Definition speedhqenc.c:118
static const uint16_t mpeg12_vlc_dc_chroma_code_reversed[12]
Definition speedhqenc.c:53
static uint8_t uni_speedhq_ac_vlc_len[64 *64 *2]
Definition speedhqenc.c:62
static void speedhq_encode_mb(MPVEncContext *const s, int16_t block[12][64], int unused_x, int unused_y)
Definition speedhqenc.c:214
static uint32_t speedhq_chr_dc_uni[512]
Definition speedhqenc.c:60
SpeedHQ encoder.
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition avcodec.h:468
void * priv_data
Definition avcodec.h:470
MPVEncContext s
The main slicecontext.
int(* encode_picture_header)(struct MPVMainEncContext *m)
MPVMainEncContext m
Definition speedhqenc.c:65
uint8_t run
Definition svq3.c:207
uint8_t level
Definition svq3.c:208
#define av_log(a,...)
static AVFormatContext * ctx
Definition movenc.c:49
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)