speedhqenc.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. * @file
  26. * SpeedHQ encoder.
  27. */
  28. #include "libavutil/avassert.h"
  29. #include "libavutil/thread.h"
  30. #include "avcodec.h"
  31. #include "codec_internal.h"
  32. #include "mathops.h"
  33. #include "mpeg12data.h"
  34. #include "mpeg12vlc.h"
  35. #include "mpegvideo.h"
  36. #include "mpegvideodata.h"
  37. #include "mpegvideoenc.h"
  38. #include "put_bits.h"
  39. #include "rl.h"
  40. #include "speedhq.h"
  41. #include "speedhqenc.h"
  42. static uint8_t speedhq_max_level[MAX_LEVEL + 1];
  43. static uint8_t speedhq_index_run[MAX_RUN + 1];
  44. /* Exactly the same as MPEG-2, except little-endian. */
  45. static const uint16_t mpeg12_vlc_dc_lum_code_reversed[12] = {
  46. 0x1, 0x0, 0x2, 0x5, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF
  47. };
  48. static const uint16_t mpeg12_vlc_dc_chroma_code_reversed[12] = {
  49. 0x0, 0x2, 0x1, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF
  50. };
  51. /* simple include everything table for dc, first byte is bits
  52. * number next 3 are code */
  53. static uint32_t speedhq_lum_dc_uni[512];
  54. static uint32_t speedhq_chr_dc_uni[512];
  55. static uint8_t uni_speedhq_ac_vlc_len[64 * 64 * 2];
  56. typedef struct SpeedHQEncContext {
  57. MPVMainEncContext m;
  58. int slice_start;
  59. } SpeedHQEncContext;
  60. static av_cold void speedhq_init_static_data(void)
  61. {
  62. ff_rl_init_level_run(speedhq_max_level, speedhq_index_run,
  63. ff_speedhq_run, ff_speedhq_level, SPEEDHQ_RL_NB_ELEMS);
  64. /* build unified dc encoding tables */
  65. for (int i = -255; i < 256; i++) {
  66. int adiff, index;
  67. int bits, code;
  68. int diff = i;
  69. adiff = FFABS(diff);
  70. if (diff < 0)
  71. diff--;
  72. index = av_log2(2 * adiff);
  73. bits = ff_mpeg12_vlc_dc_lum_bits[index] + index;
  74. code = mpeg12_vlc_dc_lum_code_reversed[index] +
  75. (av_zero_extend(diff, index) << ff_mpeg12_vlc_dc_lum_bits[index]);
  76. speedhq_lum_dc_uni[i + 255] = bits + (code << 8);
  77. bits = ff_mpeg12_vlc_dc_chroma_bits[index] + index;
  78. code = mpeg12_vlc_dc_chroma_code_reversed[index] +
  79. (av_zero_extend(diff, index) << ff_mpeg12_vlc_dc_chroma_bits[index]);
  80. speedhq_chr_dc_uni[i + 255] = bits + (code << 8);
  81. }
  82. ff_mpeg1_init_uni_ac_vlc(speedhq_max_level, speedhq_index_run,
  83. ff_speedhq_vlc_table, uni_speedhq_ac_vlc_len);
  84. }
  85. static int speedhq_encode_picture_header(MPVMainEncContext *const m)
  86. {
  87. SpeedHQEncContext *const ctx = (SpeedHQEncContext*)m;
  88. MPVEncContext *const s = &m->s;
  89. put_bits_assume_flushed(&s->pb);
  90. put_bits_le(&s->pb, 8, 100 - s->c.qscale * 2); /* FIXME why doubled */
  91. put_bits_le(&s->pb, 24, 4); /* no second field */
  92. ctx->slice_start = 4;
  93. /* length of first slice, will be filled out later */
  94. put_bits_le(&s->pb, 24, 0);
  95. return 0;
  96. }
  97. void ff_speedhq_end_slice(MPVEncContext *const s)
  98. {
  99. SpeedHQEncContext *ctx = (SpeedHQEncContext*)s;
  100. int slice_len;
  101. flush_put_bits_le(&s->pb);
  102. slice_len = put_bytes_output(&s->pb) - ctx->slice_start;
  103. AV_WL24(s->pb.buf + ctx->slice_start, slice_len);
  104. /* length of next slice, will be filled out later */
  105. ctx->slice_start = put_bytes_output(&s->pb);
  106. put_bits_le(&s->pb, 24, 0);
  107. }
  108. static inline void encode_dc(PutBitContext *pb, int diff, int component)
  109. {
  110. unsigned int diff_u = diff + 255;
  111. if (diff_u >= 511) {
  112. int index;
  113. if (diff < 0) {
  114. index = av_log2_16bit(-2 * diff);
  115. diff--;
  116. } else {
  117. index = av_log2_16bit(2 * diff);
  118. }
  119. if (component == 0)
  120. put_bits_le(pb,
  121. ff_mpeg12_vlc_dc_lum_bits[index] + index,
  122. mpeg12_vlc_dc_lum_code_reversed[index] +
  123. (av_zero_extend(diff, index) << ff_mpeg12_vlc_dc_lum_bits[index]));
  124. else
  125. put_bits_le(pb,
  126. ff_mpeg12_vlc_dc_chroma_bits[index] + index,
  127. mpeg12_vlc_dc_chroma_code_reversed[index] +
  128. (av_zero_extend(diff, index) << ff_mpeg12_vlc_dc_chroma_bits[index]));
  129. } else {
  130. if (component == 0)
  131. put_bits_le(pb,
  132. speedhq_lum_dc_uni[diff + 255] & 0xFF,
  133. speedhq_lum_dc_uni[diff + 255] >> 8);
  134. else
  135. put_bits_le(pb,
  136. speedhq_chr_dc_uni[diff + 255] & 0xFF,
  137. speedhq_chr_dc_uni[diff + 255] >> 8);
  138. }
  139. }
  140. static void encode_block(MPVEncContext *const s, const int16_t block[], int n)
  141. {
  142. int alevel, level, last_non_zero, dc, i, j, run, last_index, sign;
  143. int code;
  144. int component, val;
  145. /* DC coef */
  146. component = (n <= 3 ? 0 : (n&1) + 1);
  147. dc = block[0]; /* overflow is impossible */
  148. val = s->last_dc[component] - dc; /* opposite of most codecs */
  149. encode_dc(&s->pb, val, component);
  150. s->last_dc[component] = dc;
  151. /* now quantify & encode AC coefs */
  152. last_non_zero = 0;
  153. last_index = s->c.block_last_index[n];
  154. for (i = 1; i <= last_index; i++) {
  155. j = s->c.intra_scantable.permutated[i];
  156. level = block[j];
  157. /* encode using VLC */
  158. if (level != 0) {
  159. run = i - last_non_zero - 1;
  160. alevel = level;
  161. MASK_ABS(sign, alevel);
  162. sign &= 1;
  163. if (alevel <= speedhq_max_level[run]) {
  164. code = speedhq_index_run[run] + alevel - 1;
  165. /* store the VLC & sign at once */
  166. put_bits_le(&s->pb, ff_speedhq_vlc_table[code][1] + 1,
  167. ff_speedhq_vlc_table[code][0] | (sign << ff_speedhq_vlc_table[code][1]));
  168. } else {
  169. /* escape seems to be pretty rare <5% so I do not optimize it;
  170. * The following encodes the escape value 100000b together with
  171. * run and level. */
  172. put_bits_le(&s->pb, 6 + 6 + 12, 0x20 | run << 6 |
  173. (level + 2048) << 12);
  174. }
  175. last_non_zero = i;
  176. }
  177. }
  178. /* end of block; the values correspond to ff_speedhq_vlc_table[122] */
  179. put_bits_le(&s->pb, 4, 6);
  180. }
  181. static void speedhq_encode_mb(MPVEncContext *const s, int16_t block[12][64],
  182. int unused_x, int unused_y)
  183. {
  184. int i;
  185. for(i=0;i<6;i++) {
  186. encode_block(s, block[i], i);
  187. }
  188. if (s->c.chroma_format == CHROMA_444) {
  189. encode_block(s, block[8], 8);
  190. encode_block(s, block[9], 9);
  191. encode_block(s, block[6], 6);
  192. encode_block(s, block[7], 7);
  193. encode_block(s, block[10], 10);
  194. encode_block(s, block[11], 11);
  195. } else if (s->c.chroma_format == CHROMA_422) {
  196. encode_block(s, block[6], 6);
  197. encode_block(s, block[7], 7);
  198. }
  199. s->i_tex_bits += get_bits_diff(s);
  200. }
  201. static av_cold int speedhq_encode_init(AVCodecContext *avctx)
  202. {
  203. static AVOnce init_static_once = AV_ONCE_INIT;
  204. MPVMainEncContext *const m = avctx->priv_data;
  205. MPVEncContext *const s = &m->s;
  206. int ret;
  207. if (avctx->width > 65500 || avctx->height > 65500) {
  208. av_log(avctx, AV_LOG_ERROR, "SpeedHQ does not support resolutions above 65500x65500\n");
  209. return AVERROR(EINVAL);
  210. }
  211. // border is not implemented correctly at the moment, see ticket #10078
  212. if (avctx->width % 16) {
  213. av_log(avctx, AV_LOG_ERROR, "width must be a multiple of 16\n");
  214. return AVERROR_PATCHWELCOME;
  215. }
  216. switch (avctx->pix_fmt) {
  217. case AV_PIX_FMT_YUV420P:
  218. avctx->codec_tag = MKTAG('S','H','Q','0');
  219. break;
  220. case AV_PIX_FMT_YUV422P:
  221. avctx->codec_tag = MKTAG('S','H','Q','2');
  222. break;
  223. case AV_PIX_FMT_YUV444P:
  224. avctx->codec_tag = MKTAG('S','H','Q','4');
  225. break;
  226. default:
  227. av_unreachable("Already checked via CODEC_PIXFMTS");
  228. }
  229. m->encode_picture_header = speedhq_encode_picture_header;
  230. s->encode_mb = speedhq_encode_mb;
  231. s->min_qcoeff = -2048;
  232. s->max_qcoeff = 2047;
  233. s->intra_ac_vlc_length =
  234. s->intra_ac_vlc_last_length =
  235. s->intra_chroma_ac_vlc_length =
  236. s->intra_chroma_ac_vlc_last_length = uni_speedhq_ac_vlc_len;
  237. s->c.y_dc_scale_table =
  238. s->c.c_dc_scale_table = ff_mpeg12_dc_scale_table[3];
  239. ret = ff_mpv_encode_init(avctx);
  240. if (ret < 0)
  241. return ret;
  242. ff_thread_once(&init_static_once, speedhq_init_static_data);
  243. return 0;
  244. }
  245. const FFCodec ff_speedhq_encoder = {
  246. .p.name = "speedhq",
  247. CODEC_LONG_NAME("NewTek SpeedHQ"),
  248. .p.type = AVMEDIA_TYPE_VIDEO,
  249. .p.id = AV_CODEC_ID_SPEEDHQ,
  250. .p.priv_class = &ff_mpv_enc_class,
  251. .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
  252. .priv_data_size = sizeof(SpeedHQEncContext),
  253. .init = speedhq_encode_init,
  254. FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
  255. .close = ff_mpv_encode_end,
  256. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
  257. .color_ranges = AVCOL_RANGE_MPEG,
  258. CODEC_PIXFMTS(AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P),
  259. };