You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

881 lines
34KB

  1. /*
  2. * Copyright (c) 2020 Paul B Mahol
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * Cineform HD video encoder
  23. */
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/imgutils.h"
  28. #include "libavutil/opt.h"
  29. #include "avcodec.h"
  30. #include "bytestream.h"
  31. #include "cfhd.h"
  32. #include "put_bits.h"
  33. #include "internal.h"
  34. #include "thread.h"
  35. /* Derived from existing tables from decoder */
  36. static const unsigned codebook[256][2] = {
  37. { 1, 0x00000000 }, { 2, 0x00000002 }, { 3, 0x00000007 }, { 5, 0x00000019 }, { 6, 0x00000030 },
  38. { 6, 0x00000036 }, { 7, 0x00000063 }, { 7, 0x0000006B }, { 7, 0x0000006F }, { 8, 0x000000D4 },
  39. { 8, 0x000000DC }, { 9, 0x00000189 }, { 9, 0x000001A0 }, { 9, 0x000001AB }, {10, 0x00000310 },
  40. {10, 0x00000316 }, {10, 0x00000354 }, {10, 0x00000375 }, {10, 0x00000377 }, {11, 0x00000623 },
  41. {11, 0x00000684 }, {11, 0x000006AB }, {11, 0x000006EC }, {12, 0x00000C44 }, {12, 0x00000C5C },
  42. {12, 0x00000C5E }, {12, 0x00000D55 }, {12, 0x00000DD1 }, {12, 0x00000DD3 }, {12, 0x00000DDB },
  43. {13, 0x0000188B }, {13, 0x000018BB }, {13, 0x00001AA8 }, {13, 0x00001BA0 }, {13, 0x00001BA4 },
  44. {13, 0x00001BB5 }, {14, 0x00003115 }, {14, 0x00003175 }, {14, 0x0000317D }, {14, 0x00003553 },
  45. {14, 0x00003768 }, {15, 0x00006228 }, {15, 0x000062E8 }, {15, 0x000062F8 }, {15, 0x00006AA4 },
  46. {15, 0x00006E85 }, {15, 0x00006E87 }, {15, 0x00006ED3 }, {16, 0x0000C453 }, {16, 0x0000C5D3 },
  47. {16, 0x0000C5F3 }, {16, 0x0000DD08 }, {16, 0x0000DD0C }, {16, 0x0000DDA4 }, {17, 0x000188A4 },
  48. {17, 0x00018BA5 }, {17, 0x00018BE5 }, {17, 0x0001AA95 }, {17, 0x0001AA97 }, {17, 0x0001BA13 },
  49. {17, 0x0001BB4A }, {17, 0x0001BB4B }, {18, 0x00031748 }, {18, 0x000317C8 }, {18, 0x00035528 },
  50. {18, 0x0003552C }, {18, 0x00037424 }, {18, 0x00037434 }, {18, 0x00037436 }, {19, 0x00062294 },
  51. {19, 0x00062E92 }, {19, 0x00062F92 }, {19, 0x0006AA52 }, {19, 0x0006AA5A }, {19, 0x0006E84A },
  52. {19, 0x0006E86A }, {19, 0x0006E86E }, {20, 0x000C452A }, {20, 0x000C5D27 }, {20, 0x000C5F26 },
  53. {20, 0x000D54A6 }, {20, 0x000D54B6 }, {20, 0x000DD096 }, {20, 0x000DD0D6 }, {20, 0x000DD0DE },
  54. {21, 0x00188A56 }, {21, 0x0018BA4D }, {21, 0x0018BE4E }, {21, 0x0018BE4F }, {21, 0x001AA96E },
  55. {21, 0x001BA12E }, {21, 0x001BA12F }, {21, 0x001BA1AF }, {21, 0x001BA1BF }, {22, 0x00317498 },
  56. {22, 0x0035529C }, {22, 0x0035529D }, {22, 0x003552DE }, {22, 0x003552DF }, {22, 0x0037435D },
  57. {22, 0x0037437D }, {23, 0x0062295D }, {23, 0x0062E933 }, {23, 0x006AA53D }, {23, 0x006AA53E },
  58. {23, 0x006AA53F }, {23, 0x006E86B9 }, {23, 0x006E86F8 }, {24, 0x00C452B8 }, {24, 0x00C5D265 },
  59. {24, 0x00D54A78 }, {24, 0x00D54A79 }, {24, 0x00DD0D70 }, {24, 0x00DD0D71 }, {24, 0x00DD0DF2 },
  60. {24, 0x00DD0DF3 }, {26, 0x03114BA2 }, {25, 0x0188A5B1 }, {25, 0x0188A58B }, {25, 0x0188A595 },
  61. {25, 0x0188A5D6 }, {25, 0x0188A5D7 }, {25, 0x0188A5A8 }, {25, 0x0188A5AE }, {25, 0x0188A5AF },
  62. {25, 0x0188A5C4 }, {25, 0x0188A5C5 }, {25, 0x0188A587 }, {25, 0x0188A584 }, {25, 0x0188A585 },
  63. {25, 0x0188A5C6 }, {25, 0x0188A5C7 }, {25, 0x0188A5CC }, {25, 0x0188A5CD }, {25, 0x0188A581 },
  64. {25, 0x0188A582 }, {25, 0x0188A583 }, {25, 0x0188A5CE }, {25, 0x0188A5CF }, {25, 0x0188A5C2 },
  65. {25, 0x0188A5C3 }, {25, 0x0188A5C1 }, {25, 0x0188A5B4 }, {25, 0x0188A5B5 }, {25, 0x0188A5E6 },
  66. {25, 0x0188A5E7 }, {25, 0x0188A5E4 }, {25, 0x0188A5E5 }, {25, 0x0188A5AB }, {25, 0x0188A5E0 },
  67. {25, 0x0188A5E1 }, {25, 0x0188A5E2 }, {25, 0x0188A5E3 }, {25, 0x0188A5B6 }, {25, 0x0188A5B7 },
  68. {25, 0x0188A5FD }, {25, 0x0188A57E }, {25, 0x0188A57F }, {25, 0x0188A5EC }, {25, 0x0188A5ED },
  69. {25, 0x0188A5FE }, {25, 0x0188A5FF }, {25, 0x0188A57D }, {25, 0x0188A59C }, {25, 0x0188A59D },
  70. {25, 0x0188A5E8 }, {25, 0x0188A5E9 }, {25, 0x0188A5EA }, {25, 0x0188A5EB }, {25, 0x0188A5EF },
  71. {25, 0x0188A57A }, {25, 0x0188A57B }, {25, 0x0188A578 }, {25, 0x0188A579 }, {25, 0x0188A5BA },
  72. {25, 0x0188A5BB }, {25, 0x0188A5B8 }, {25, 0x0188A5B9 }, {25, 0x0188A588 }, {25, 0x0188A589 },
  73. {25, 0x018BA4C8 }, {25, 0x018BA4C9 }, {25, 0x0188A5FA }, {25, 0x0188A5FB }, {25, 0x0188A5BC },
  74. {25, 0x0188A5BD }, {25, 0x0188A598 }, {25, 0x0188A599 }, {25, 0x0188A5F4 }, {25, 0x0188A5F5 },
  75. {25, 0x0188A59B }, {25, 0x0188A5DE }, {25, 0x0188A5DF }, {25, 0x0188A596 }, {25, 0x0188A597 },
  76. {25, 0x0188A5F8 }, {25, 0x0188A5F9 }, {25, 0x0188A5F1 }, {25, 0x0188A58E }, {25, 0x0188A58F },
  77. {25, 0x0188A5DC }, {25, 0x0188A5DD }, {25, 0x0188A5F2 }, {25, 0x0188A5F3 }, {25, 0x0188A58C },
  78. {25, 0x0188A58D }, {25, 0x0188A5A4 }, {25, 0x0188A5F0 }, {25, 0x0188A5A5 }, {25, 0x0188A5A6 },
  79. {25, 0x0188A5A7 }, {25, 0x0188A59A }, {25, 0x0188A5A2 }, {25, 0x0188A5A3 }, {25, 0x0188A58A },
  80. {25, 0x0188A5B0 }, {25, 0x0188A5A0 }, {25, 0x0188A5A1 }, {25, 0x0188A5DA }, {25, 0x0188A5DB },
  81. {25, 0x0188A59E }, {25, 0x0188A59F }, {25, 0x0188A5D8 }, {25, 0x0188A5EE }, {25, 0x0188A5D9 },
  82. {25, 0x0188A5F6 }, {25, 0x0188A5F7 }, {25, 0x0188A57C }, {25, 0x0188A5C8 }, {25, 0x0188A5C9 },
  83. {25, 0x0188A594 }, {25, 0x0188A5FC }, {25, 0x0188A5CA }, {25, 0x0188A5CB }, {25, 0x0188A5B2 },
  84. {25, 0x0188A5AA }, {25, 0x0188A5B3 }, {25, 0x0188A572 }, {25, 0x0188A573 }, {25, 0x0188A5C0 },
  85. {25, 0x0188A5BE }, {25, 0x0188A5BF }, {25, 0x0188A592 }, {25, 0x0188A580 }, {25, 0x0188A593 },
  86. {25, 0x0188A590 }, {25, 0x0188A591 }, {25, 0x0188A586 }, {25, 0x0188A5A9 }, {25, 0x0188A5D2 },
  87. {25, 0x0188A5D3 }, {25, 0x0188A5D4 }, {25, 0x0188A5D5 }, {25, 0x0188A5AC }, {25, 0x0188A5AD },
  88. {25, 0x0188A5D0 },
  89. };
  90. /* Derived by extracting runcodes from existing tables from decoder */
  91. static const uint16_t runbook[8][3] = {
  92. {1, 0x0000, 1}, {7, 0x0069, 12}, {8, 0x00D1, 20}, {9, 0x018A, 32},
  93. {10, 0x0343, 60}, {11, 0x0685, 100}, {13, 0x18BF, 180}, {13, 0x1BA5, 320},
  94. };
  95. /*
  96. * Derived by inspecting various quality encodes
  97. * and adding some more from scratch.
  98. */
  99. static const uint16_t quantization_per_subband[2][3][13][9] = {
  100. {{
  101. { 16, 16, 8, 4, 4, 2, 3, 3, 4, }, // film3+
  102. { 16, 16, 8, 4, 4, 2, 3, 3, 4, }, // film3
  103. { 16, 16, 8, 4, 4, 2, 4, 4, 6, }, // film2+
  104. { 16, 16, 8, 4, 4, 2, 4, 4, 6, }, // film2
  105. { 16, 16, 8, 4, 4, 2, 8, 8, 12, }, // film1++
  106. { 24, 24, 12, 6, 6, 3, 12, 12, 18, }, // film1+
  107. { 24, 24, 12, 6, 6, 3, 12, 12, 18, }, // film1
  108. { 32, 32, 24, 8, 8, 6, 16, 16, 24, }, // high+
  109. { 32, 32, 24, 8, 8, 6, 16, 16, 24, }, // high
  110. { 48, 48, 32, 12, 12, 8, 32, 32, 48, }, // medium+
  111. { 48, 48, 32, 12, 12, 8, 32, 32, 48, }, // medium
  112. { 64, 64, 48, 16, 16, 12, 48, 48, 64, }, // low+
  113. { 64, 64, 48, 16, 16, 12, 64, 64, 96, }, // low
  114. },
  115. {
  116. { 16, 16, 8, 4, 4, 2, 3, 3, 4, },
  117. { 16, 16, 8, 4, 4, 2, 3, 3, 6, },
  118. { 16, 16, 8, 4, 4, 2, 4, 4, 6, },
  119. { 16, 16, 8, 4, 4, 2, 4, 4, 8, },
  120. { 16, 16, 8, 4, 4, 2, 8, 8, 16, },
  121. { 24, 24, 12, 6, 6, 3, 12, 12, 18, },
  122. { 24, 24, 12, 6, 6, 3, 12, 12, 24, },
  123. { 32, 32, 24, 8, 8, 6, 16, 16, 24, },
  124. { 48, 48, 32, 12, 12, 8, 16, 16, 32, },
  125. { 48, 48, 32, 12, 12, 8, 32, 32, 48, },
  126. { 48, 48, 32, 12, 12, 8, 32, 32, 64, },
  127. { 64, 64, 48, 16, 16, 12, 48, 48, 64, },
  128. { 64, 64, 48, 16, 16, 12, 64, 64, 96, },
  129. },
  130. {
  131. { 16, 16, 8, 4, 4, 2, 3, 3, 4, },
  132. { 16, 16, 8, 4, 4, 2, 3, 3, 6, },
  133. { 16, 16, 8, 4, 4, 2, 4, 4, 6, },
  134. { 16, 16, 8, 4, 4, 2, 4, 4, 8, },
  135. { 16, 16, 8, 4, 4, 2, 8, 8, 16, },
  136. { 24, 24, 12, 6, 6, 3, 12, 12, 18, },
  137. { 24, 24, 12, 6, 6, 3, 12, 12, 24, },
  138. { 32, 32, 24, 8, 8, 6, 16, 16, 24, },
  139. { 48, 48, 32, 12, 12, 8, 16, 16, 32, },
  140. { 48, 48, 32, 12, 12, 8, 32, 32, 48, },
  141. { 48, 48, 32, 12, 12, 8, 32, 32, 64, },
  142. { 64, 64, 48, 16, 16, 12, 48, 48, 64, },
  143. { 64, 64, 48, 16, 16, 12, 64, 64, 96, },
  144. }},
  145. {{
  146. { 16, 16, 8, 16, 16, 8, 24, 24, 36, },
  147. { 16, 16, 8, 16, 16, 8, 32, 32, 48, },
  148. { 16, 16, 8, 16, 16, 8, 48, 48, 72, },
  149. { 16, 16, 8, 16, 16, 8, 64, 64, 96, },
  150. { 16, 16, 8, 20, 20, 10, 80, 80, 128, },
  151. { 24, 24, 12, 24, 24, 12, 96, 96, 144, },
  152. { 24, 24, 12, 24, 24, 12, 128, 128, 192, },
  153. { 32, 32, 24, 32, 32, 24, 192, 192, 288, },
  154. { 32, 32, 24, 32, 32, 24, 256, 256, 384, },
  155. { 48, 48, 32, 48, 48, 32, 256, 256, 384, },
  156. { 48, 48, 32, 48, 48, 32, 512, 512, 768, },
  157. { 56, 56, 40, 56, 56, 40, 512, 512, 768, },
  158. { 64, 64, 48, 64, 64, 48, 512, 512, 768, },
  159. },
  160. {
  161. { 16, 16, 8, 16, 16, 8, 24, 24, 36, },
  162. { 16, 16, 8, 16, 16, 8, 32, 32, 48, },
  163. { 16, 16, 8, 16, 16, 8, 48, 48, 72, },
  164. { 16, 16, 8, 16, 16, 8, 64, 64, 96, },
  165. { 16, 16, 8, 20, 20, 10, 80, 80, 128, },
  166. { 24, 24, 12, 24, 24, 12, 96, 96, 144, },
  167. { 24, 24, 12, 24, 24, 12, 128, 128, 192, },
  168. { 32, 32, 24, 32, 32, 24, 192, 192, 288, },
  169. { 32, 32, 24, 32, 32, 24, 256, 256, 384, },
  170. { 48, 48, 32, 48, 48, 32, 256, 256, 384, },
  171. { 48, 48, 32, 48, 48, 32, 512, 512, 768, },
  172. { 56, 56, 40, 56, 56, 40, 512, 512, 768, },
  173. { 64, 64, 48, 64, 64, 48, 512, 512, 768, },
  174. },
  175. {
  176. { 16, 16, 8, 16, 16, 8, 24, 24, 36, },
  177. { 16, 16, 8, 16, 16, 8, 32, 32, 48, },
  178. { 16, 16, 8, 16, 16, 8, 48, 48, 72, },
  179. { 16, 16, 8, 16, 16, 8, 64, 64, 96, },
  180. { 16, 16, 10, 20, 20, 10, 80, 80, 128, },
  181. { 24, 24, 12, 24, 24, 12, 96, 96, 144, },
  182. { 24, 24, 12, 24, 24, 12, 128, 128, 192, },
  183. { 32, 32, 24, 32, 32, 24, 192, 192, 288, },
  184. { 32, 32, 24, 32, 32, 24, 256, 256, 384, },
  185. { 48, 48, 32, 48, 48, 32, 256, 256, 384, },
  186. { 48, 48, 32, 48, 48, 32, 512, 512, 768, },
  187. { 56, 56, 40, 56, 56, 40, 512, 512, 768, },
  188. { 64, 64, 48, 64, 64, 48, 512, 512, 768, },
  189. }},
  190. };
  191. typedef struct Codebook {
  192. unsigned bits;
  193. unsigned size;
  194. } Codebook;
  195. typedef struct Runbook {
  196. unsigned size;
  197. unsigned bits;
  198. unsigned run;
  199. } Runbook;
  200. typedef struct PlaneEnc {
  201. unsigned size;
  202. int16_t *dwt_buf;
  203. int16_t *dwt_tmp;
  204. unsigned quantization[SUBBAND_COUNT];
  205. int16_t *subband[SUBBAND_COUNT];
  206. int16_t *l_h[8];
  207. SubBand band[DWT_LEVELS][4];
  208. } PlaneEnc;
  209. typedef struct CFHDEncContext {
  210. const AVClass *class;
  211. PutBitContext pb;
  212. PutByteContext pby;
  213. int quality;
  214. int planes;
  215. int chroma_h_shift;
  216. int chroma_v_shift;
  217. PlaneEnc plane[4];
  218. uint16_t lut[1024];
  219. Runbook rb[321];
  220. Codebook cb[513];
  221. } CFHDEncContext;
  222. static av_cold int cfhd_encode_init(AVCodecContext *avctx)
  223. {
  224. CFHDEncContext *s = avctx->priv_data;
  225. const int sign_mask = 256;
  226. const int twos_complement = -sign_mask;
  227. const int mag_mask = sign_mask - 1;
  228. int ret, last = 0;
  229. ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt,
  230. &s->chroma_h_shift,
  231. &s->chroma_v_shift);
  232. if (ret < 0)
  233. return ret;
  234. if (avctx->width & 15) {
  235. av_log(avctx, AV_LOG_ERROR, "Width must be multiple of 16.\n");
  236. return AVERROR_INVALIDDATA;
  237. }
  238. s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
  239. for (int i = 0; i < s->planes; i++) {
  240. int w8, h8, w4, h4, w2, h2;
  241. int width = i ? avctx->width >> s->chroma_h_shift : avctx->width;
  242. int height = i ? FFALIGN(avctx->height >> s->chroma_v_shift, 8) :
  243. FFALIGN(avctx->height >> s->chroma_v_shift, 8);
  244. ptrdiff_t stride = FFALIGN(width / 8, 8) * 8;
  245. w8 = FFALIGN(width / 8, 8);
  246. h8 = height / 8;
  247. w4 = w8 * 2;
  248. h4 = h8 * 2;
  249. w2 = w4 * 2;
  250. h2 = h4 * 2;
  251. s->plane[i].dwt_buf =
  252. av_mallocz_array(height * stride, sizeof(*s->plane[i].dwt_buf));
  253. s->plane[i].dwt_tmp =
  254. av_malloc_array(height * stride, sizeof(*s->plane[i].dwt_tmp));
  255. if (!s->plane[i].dwt_buf || !s->plane[i].dwt_tmp)
  256. return AVERROR(ENOMEM);
  257. s->plane[i].subband[0] = s->plane[i].dwt_buf;
  258. s->plane[i].subband[1] = s->plane[i].dwt_buf + 2 * w8 * h8;
  259. s->plane[i].subband[2] = s->plane[i].dwt_buf + 1 * w8 * h8;
  260. s->plane[i].subband[3] = s->plane[i].dwt_buf + 3 * w8 * h8;
  261. s->plane[i].subband[4] = s->plane[i].dwt_buf + 2 * w4 * h4;
  262. s->plane[i].subband[5] = s->plane[i].dwt_buf + 1 * w4 * h4;
  263. s->plane[i].subband[6] = s->plane[i].dwt_buf + 3 * w4 * h4;
  264. s->plane[i].subband[7] = s->plane[i].dwt_buf + 2 * w2 * h2;
  265. s->plane[i].subband[8] = s->plane[i].dwt_buf + 1 * w2 * h2;
  266. s->plane[i].subband[9] = s->plane[i].dwt_buf + 3 * w2 * h2;
  267. for (int j = 0; j < DWT_LEVELS; j++) {
  268. for (int k = 0; k < FF_ARRAY_ELEMS(s->plane[i].band[j]); k++) {
  269. s->plane[i].band[j][k].width = (width / 8) << j;
  270. s->plane[i].band[j][k].height = (height / 8) << j;
  271. s->plane[i].band[j][k].a_width = w8 << j;
  272. s->plane[i].band[j][k].a_height = h8 << j;
  273. }
  274. }
  275. /* ll2 and ll1 commented out because they are done in-place */
  276. s->plane[i].l_h[0] = s->plane[i].dwt_tmp;
  277. s->plane[i].l_h[1] = s->plane[i].dwt_tmp + 2 * w8 * h8;
  278. // s->plane[i].l_h[2] = ll2;
  279. s->plane[i].l_h[3] = s->plane[i].dwt_tmp;
  280. s->plane[i].l_h[4] = s->plane[i].dwt_tmp + 2 * w4 * h4;
  281. // s->plane[i].l_h[5] = ll1;
  282. s->plane[i].l_h[6] = s->plane[i].dwt_tmp;
  283. s->plane[i].l_h[7] = s->plane[i].dwt_tmp + 2 * w2 * h2;
  284. }
  285. for (int i = 0; i < 512; i++) {
  286. int value = (i & sign_mask) ? twos_complement + (i & mag_mask): i;
  287. int mag = FFMIN(FFABS(value), 255);
  288. if (mag) {
  289. s->cb[i].bits = (codebook[mag][1] << 1) | (value > 0 ? 0 : 1);
  290. s->cb[i].size = codebook[mag][0] + 1;
  291. } else {
  292. s->cb[i].bits = codebook[mag][1];
  293. s->cb[i].size = codebook[mag][0];
  294. }
  295. }
  296. s->cb[512].bits = 0x3114ba3;
  297. s->cb[512].size = 26;
  298. s->rb[0].run = 0;
  299. for (int i = 1, j = 0; i < 320 && j < 7; j++) {
  300. int run = runbook[j][2];
  301. int end = runbook[j+1][2];
  302. while (i < end) {
  303. s->rb[i].run = run;
  304. s->rb[i].bits = runbook[j][1];
  305. s->rb[i++].size = runbook[j][0];
  306. }
  307. }
  308. s->rb[320].bits = runbook[7][1];
  309. s->rb[320].size = runbook[7][0];
  310. s->rb[320].run = 320;
  311. for (int i = 0; i < 256; i++) {
  312. int idx = i + ((768LL * i * i * i) / (256 * 256 * 256));
  313. s->lut[idx] = i;
  314. }
  315. for (int i = 0; i < 1024; i++) {
  316. if (s->lut[i])
  317. last = s->lut[i];
  318. else
  319. s->lut[i] = last;
  320. }
  321. return 0;
  322. }
  323. static inline void filter(int16_t *input, ptrdiff_t in_stride,
  324. int16_t *low, ptrdiff_t low_stride,
  325. int16_t *high, ptrdiff_t high_stride,
  326. int len)
  327. {
  328. low[(0>>1) * low_stride] = av_clip_int16(input[0*in_stride] + input[1*in_stride]);
  329. high[(0>>1) * high_stride] = av_clip_int16((5 * input[0*in_stride] - 11 * input[1*in_stride] +
  330. 4 * input[2*in_stride] + 4 * input[3*in_stride] -
  331. 1 * input[4*in_stride] - 1 * input[5*in_stride] + 4) >> 3);
  332. for (int i = 2; i < len - 2; i += 2) {
  333. low[(i>>1) * low_stride] = av_clip_int16(input[i*in_stride] + input[(i+1)*in_stride]);
  334. high[(i>>1) * high_stride] = av_clip_int16(((-input[(i-2)*in_stride] - input[(i-1)*in_stride] +
  335. input[(i+2)*in_stride] + input[(i+3)*in_stride] + 4) >> 3) +
  336. input[(i+0)*in_stride] - input[(i+1)*in_stride]);
  337. }
  338. low[((len-2)>>1) * low_stride] = av_clip_int16(input[((len-2)+0)*in_stride] + input[((len-2)+1)*in_stride]);
  339. high[((len-2)>>1) * high_stride] = av_clip_int16((11* input[((len-2)+0)*in_stride] - 5 * input[((len-2)+1)*in_stride] -
  340. 4 * input[((len-2)-1)*in_stride] - 4 * input[((len-2)-2)*in_stride] +
  341. 1 * input[((len-2)-3)*in_stride] + 1 * input[((len-2)-4)*in_stride] + 4) >> 3);
  342. }
  343. static void horiz_filter(int16_t *input, int16_t *low, int16_t *high,
  344. int width)
  345. {
  346. filter(input, 1, low, 1, high, 1, width);
  347. }
  348. static void vert_filter(int16_t *input, ptrdiff_t in_stride,
  349. int16_t *low, ptrdiff_t low_stride,
  350. int16_t *high, ptrdiff_t high_stride, int len)
  351. {
  352. filter(input, in_stride, low, low_stride, high, high_stride, len);
  353. }
  354. static void quantize_band(int16_t *input, int width, int a_width,
  355. int height, unsigned quantization)
  356. {
  357. const int factor = (1 << 16) / quantization;
  358. for (int i = 0; i < height; i++) {
  359. for (int j = 0; j < width; j++)
  360. input[j] = av_clip_intp2((input[j] * factor) / 65536, 10);
  361. input += a_width;
  362. }
  363. }
  364. static int put_runcode(PutBitContext *pb, int count, const Runbook *const rb)
  365. {
  366. while (count > 0) {
  367. const int index = FFMIN(320, count);
  368. put_bits(pb, rb[index].size, rb[index].bits);
  369. count -= rb[index].run;
  370. }
  371. return 0;
  372. }
  373. static int cfhd_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  374. const AVFrame *frame, int *got_packet)
  375. {
  376. CFHDEncContext *s = avctx->priv_data;
  377. PutByteContext *pby = &s->pby;
  378. PutBitContext *pb = &s->pb;
  379. const Codebook *const cb = s->cb;
  380. const Runbook *const rb = s->rb;
  381. const uint16_t *lut = s->lut;
  382. unsigned pos;
  383. int ret = 0;
  384. for (int plane = 0; plane < s->planes && !ret; plane++) {
  385. int width = s->plane[plane].band[2][0].width;
  386. int a_width = s->plane[plane].band[2][0].a_width;
  387. int height = s->plane[plane].band[2][0].height;
  388. int act_plane = plane == 1 ? 2 : plane == 2 ? 1 : plane;
  389. int16_t *input = (int16_t *)frame->data[act_plane];
  390. int16_t *low = s->plane[plane].l_h[6];
  391. int16_t *high = s->plane[plane].l_h[7];
  392. const ptrdiff_t in_stride = frame->linesize[act_plane] / 2;
  393. int low_stride, high_stride;
  394. for (int i = 0; i < height * 2; i++) {
  395. horiz_filter(input, low, high, width * 2);
  396. input += in_stride;
  397. low += a_width;
  398. high += a_width;
  399. }
  400. input = s->plane[plane].l_h[7];
  401. low = s->plane[plane].subband[7];
  402. low_stride = s->plane[plane].band[2][0].a_width;
  403. high = s->plane[plane].subband[9];
  404. high_stride = s->plane[plane].band[2][0].a_width;
  405. for (int i = 0; i < width; i++) {
  406. vert_filter(input, a_width, low, low_stride, high, high_stride, height * 2);
  407. input++;
  408. low++;
  409. high++;
  410. }
  411. input = s->plane[plane].l_h[6];
  412. low = s->plane[plane].l_h[7];
  413. high = s->plane[plane].subband[8];
  414. for (int i = 0; i < width; i++) {
  415. vert_filter(input, a_width, low, low_stride, high, high_stride, height * 2);
  416. input++;
  417. low++;
  418. high++;
  419. }
  420. a_width = s->plane[plane].band[1][0].a_width;
  421. width = s->plane[plane].band[1][0].width;
  422. height = s->plane[plane].band[1][0].height;
  423. input = s->plane[plane].l_h[7];
  424. low = s->plane[plane].l_h[3];
  425. low_stride = s->plane[plane].band[1][0].a_width;
  426. high = s->plane[plane].l_h[4];
  427. high_stride = s->plane[plane].band[1][0].a_width;
  428. for (int i = 0; i < height * 2; i++) {
  429. for (int j = 0; j < width * 2; j++)
  430. input[j] /= 4;
  431. input += a_width * 2;
  432. }
  433. input = s->plane[plane].l_h[7];
  434. for (int i = 0; i < height * 2; i++) {
  435. horiz_filter(input, low, high, width * 2);
  436. input += a_width * 2;
  437. low += low_stride;
  438. high += high_stride;
  439. }
  440. input = s->plane[plane].l_h[4];
  441. low = s->plane[plane].subband[4];
  442. high = s->plane[plane].subband[6];
  443. for (int i = 0; i < width; i++) {
  444. vert_filter(input, a_width, low, low_stride, high, high_stride, height * 2);
  445. input++;
  446. low++;
  447. high++;
  448. }
  449. input = s->plane[plane].l_h[3];
  450. low = s->plane[plane].l_h[4];
  451. high = s->plane[plane].subband[5];
  452. for (int i = 0; i < width; i++) {
  453. vert_filter(input, a_width, low, low_stride, high, high_stride, height * 2);
  454. input++;
  455. low++;
  456. high++;
  457. }
  458. a_width = s->plane[plane].band[0][0].a_width;
  459. width = s->plane[plane].band[0][0].width;
  460. height = s->plane[plane].band[0][0].height;
  461. input = s->plane[plane].l_h[4];
  462. low = s->plane[plane].l_h[0];
  463. low_stride = s->plane[plane].band[0][0].a_width;
  464. high = s->plane[plane].l_h[1];
  465. high_stride = s->plane[plane].band[0][0].a_width;
  466. if (avctx->pix_fmt != AV_PIX_FMT_YUV422P10) {
  467. for (int i = 0; i < height * 2; i++) {
  468. for (int j = 0; j < width * 2; j++)
  469. input[j] /= 4;
  470. input += a_width * 2;
  471. }
  472. }
  473. input = s->plane[plane].l_h[4];
  474. for (int i = 0; i < height * 2; i++) {
  475. horiz_filter(input, low, high, width * 2);
  476. input += a_width * 2;
  477. low += low_stride;
  478. high += high_stride;
  479. }
  480. low = s->plane[plane].subband[1];
  481. high = s->plane[plane].subband[3];
  482. input = s->plane[plane].l_h[1];
  483. for (int i = 0; i < width; i++) {
  484. vert_filter(input, a_width, low, low_stride, high, high_stride, height * 2);
  485. input++;
  486. low++;
  487. high++;
  488. }
  489. low = s->plane[plane].subband[0];
  490. high = s->plane[plane].subband[2];
  491. input = s->plane[plane].l_h[0];
  492. for (int i = 0; i < width; i++) {
  493. vert_filter(input, a_width, low, low_stride, high, high_stride, height * 2);
  494. input++;
  495. low++;
  496. high++;
  497. }
  498. }
  499. ret = ff_alloc_packet2(avctx, pkt, 64LL + s->planes * (2LL * avctx->width * avctx->height + 1000LL), 0);
  500. if (ret < 0)
  501. return ret;
  502. bytestream2_init_writer(pby, pkt->data, pkt->size);
  503. bytestream2_put_be16(pby, SampleType);
  504. bytestream2_put_be16(pby, 9);
  505. bytestream2_put_be16(pby, SampleIndexTable);
  506. bytestream2_put_be16(pby, s->planes);
  507. for (int i = 0; i < s->planes; i++)
  508. bytestream2_put_be32(pby, 0);
  509. bytestream2_put_be16(pby, TransformType);
  510. bytestream2_put_be16(pby, 0);
  511. bytestream2_put_be16(pby, NumFrames);
  512. bytestream2_put_be16(pby, 1);
  513. bytestream2_put_be16(pby, ChannelCount);
  514. bytestream2_put_be16(pby, s->planes);
  515. bytestream2_put_be16(pby, EncodedFormat);
  516. bytestream2_put_be16(pby, avctx->pix_fmt == AV_PIX_FMT_YUV422P10 ? 1 : 3);
  517. bytestream2_put_be16(pby, WaveletCount);
  518. bytestream2_put_be16(pby, 3);
  519. bytestream2_put_be16(pby, SubbandCount);
  520. bytestream2_put_be16(pby, SUBBAND_COUNT);
  521. bytestream2_put_be16(pby, NumSpatial);
  522. bytestream2_put_be16(pby, 2);
  523. bytestream2_put_be16(pby, FirstWavelet);
  524. bytestream2_put_be16(pby, 3);
  525. bytestream2_put_be16(pby, ImageWidth);
  526. bytestream2_put_be16(pby, avctx->width);
  527. bytestream2_put_be16(pby, ImageHeight);
  528. bytestream2_put_be16(pby, avctx->height);
  529. bytestream2_put_be16(pby, -FrameNumber);
  530. bytestream2_put_be16(pby, avctx->frame_number);
  531. bytestream2_put_be16(pby, Precision);
  532. bytestream2_put_be16(pby, avctx->pix_fmt == AV_PIX_FMT_YUV422P10 ? 10 : 12);
  533. bytestream2_put_be16(pby, PrescaleTable);
  534. bytestream2_put_be16(pby, avctx->pix_fmt == AV_PIX_FMT_YUV422P10 ? 0x2000 : 0x2800);
  535. bytestream2_put_be16(pby, SampleFlags);
  536. bytestream2_put_be16(pby, 1);
  537. for (int p = 0; p < s->planes; p++) {
  538. int width = s->plane[p].band[0][0].width;
  539. int a_width = s->plane[p].band[0][0].a_width;
  540. int height = s->plane[p].band[0][0].height;
  541. int16_t *data = s->plane[p].subband[0];
  542. if (p) {
  543. bytestream2_put_be16(pby, SampleType);
  544. bytestream2_put_be16(pby, 3);
  545. bytestream2_put_be16(pby, ChannelNumber);
  546. bytestream2_put_be16(pby, p);
  547. }
  548. bytestream2_put_be16(pby, BitstreamMarker);
  549. bytestream2_put_be16(pby, 0x1a4a);
  550. pos = bytestream2_tell_p(pby);
  551. bytestream2_put_be16(pby, LowpassSubband);
  552. bytestream2_put_be16(pby, 0);
  553. bytestream2_put_be16(pby, NumLevels);
  554. bytestream2_put_be16(pby, 3);
  555. bytestream2_put_be16(pby, LowpassWidth);
  556. bytestream2_put_be16(pby, width);
  557. bytestream2_put_be16(pby, LowpassHeight);
  558. bytestream2_put_be16(pby, height);
  559. bytestream2_put_be16(pby, PixelOffset);
  560. bytestream2_put_be16(pby, 0);
  561. bytestream2_put_be16(pby, LowpassQuantization);
  562. bytestream2_put_be16(pby, 1);
  563. bytestream2_put_be16(pby, LowpassPrecision);
  564. bytestream2_put_be16(pby, 16);
  565. bytestream2_put_be16(pby, BitstreamMarker);
  566. bytestream2_put_be16(pby, 0x0f0f);
  567. for (int i = 0; i < height; i++) {
  568. for (int j = 0; j < width; j++)
  569. bytestream2_put_be16(pby, data[j]);
  570. data += a_width;
  571. }
  572. bytestream2_put_be16(pby, BitstreamMarker);
  573. bytestream2_put_be16(pby, 0x1b4b);
  574. for (int l = 0; l < 3; l++) {
  575. for (int i = 0; i < 3; i++) {
  576. s->plane[p].quantization[1 + l * 3 + i] = quantization_per_subband[avctx->pix_fmt != AV_PIX_FMT_YUV422P10][p][s->quality][l * 3 + i];
  577. }
  578. }
  579. for (int l = 0; l < 3; l++) {
  580. int a_width = s->plane[p].band[l][0].a_width;
  581. int width = s->plane[p].band[l][0].width;
  582. int stride = FFALIGN(width, 8);
  583. int height = s->plane[p].band[l][0].height;
  584. bytestream2_put_be16(pby, BitstreamMarker);
  585. bytestream2_put_be16(pby, 0x0d0d);
  586. bytestream2_put_be16(pby, WaveletType);
  587. bytestream2_put_be16(pby, 3 + 2 * (l == 2));
  588. bytestream2_put_be16(pby, WaveletNumber);
  589. bytestream2_put_be16(pby, 3 - l);
  590. bytestream2_put_be16(pby, WaveletLevel);
  591. bytestream2_put_be16(pby, 3 - l);
  592. bytestream2_put_be16(pby, NumBands);
  593. bytestream2_put_be16(pby, 4);
  594. bytestream2_put_be16(pby, HighpassWidth);
  595. bytestream2_put_be16(pby, width);
  596. bytestream2_put_be16(pby, HighpassHeight);
  597. bytestream2_put_be16(pby, height);
  598. bytestream2_put_be16(pby, LowpassBorder);
  599. bytestream2_put_be16(pby, 0);
  600. bytestream2_put_be16(pby, HighpassBorder);
  601. bytestream2_put_be16(pby, 0);
  602. bytestream2_put_be16(pby, LowpassScale);
  603. bytestream2_put_be16(pby, 1);
  604. bytestream2_put_be16(pby, LowpassDivisor);
  605. bytestream2_put_be16(pby, 1);
  606. for (int i = 0; i < 3; i++) {
  607. int16_t *data = s->plane[p].subband[1 + l * 3 + i];
  608. int count = 0, padd = 0;
  609. bytestream2_put_be16(pby, BitstreamMarker);
  610. bytestream2_put_be16(pby, 0x0e0e);
  611. bytestream2_put_be16(pby, SubbandNumber);
  612. bytestream2_put_be16(pby, i + 1);
  613. bytestream2_put_be16(pby, BandCodingFlags);
  614. bytestream2_put_be16(pby, 1);
  615. bytestream2_put_be16(pby, BandWidth);
  616. bytestream2_put_be16(pby, width);
  617. bytestream2_put_be16(pby, BandHeight);
  618. bytestream2_put_be16(pby, height);
  619. bytestream2_put_be16(pby, SubbandBand);
  620. bytestream2_put_be16(pby, 1 + l * 3 + i);
  621. bytestream2_put_be16(pby, BandEncoding);
  622. bytestream2_put_be16(pby, 3);
  623. bytestream2_put_be16(pby, Quantization);
  624. bytestream2_put_be16(pby, s->plane[p].quantization[1 + l * 3 + i]);
  625. bytestream2_put_be16(pby, BandScale);
  626. bytestream2_put_be16(pby, 1);
  627. bytestream2_put_be16(pby, BandHeader);
  628. bytestream2_put_be16(pby, 0);
  629. quantize_band(data, width, a_width, height,
  630. s->plane[p].quantization[1 + l * 3 + i]);
  631. init_put_bits(pb, pkt->data + bytestream2_tell_p(pby), bytestream2_get_bytes_left_p(pby));
  632. for (int m = 0; m < height; m++) {
  633. for (int j = 0; j < stride; j++) {
  634. int16_t index = FFSIGN(data[j]) * lut[FFABS(data[j])];
  635. if (index < 0)
  636. index += 512;
  637. if (index == 0) {
  638. count++;
  639. continue;
  640. } else if (count > 0) {
  641. count = put_runcode(pb, count, rb);
  642. }
  643. put_bits(pb, cb[index].size, cb[index].bits);
  644. }
  645. data += a_width;
  646. }
  647. if (count > 0) {
  648. count = put_runcode(pb, count, rb);
  649. }
  650. put_bits(pb, cb[512].size, cb[512].bits);
  651. flush_put_bits(pb);
  652. bytestream2_skip_p(pby, put_bits_count(pb) >> 3);
  653. padd = (4 - (bytestream2_tell_p(pby) & 3)) & 3;
  654. while (padd--)
  655. bytestream2_put_byte(pby, 0);
  656. bytestream2_put_be16(pby, BandTrailer);
  657. bytestream2_put_be16(pby, 0);
  658. }
  659. bytestream2_put_be16(pby, BitstreamMarker);
  660. bytestream2_put_be16(pby, 0x0c0c);
  661. }
  662. s->plane[p].size = bytestream2_tell_p(pby) - pos;
  663. }
  664. bytestream2_put_be16(pby, GroupTrailer);
  665. bytestream2_put_be16(pby, 0);
  666. av_shrink_packet(pkt, bytestream2_tell_p(pby));
  667. pkt->flags |= AV_PKT_FLAG_KEY;
  668. bytestream2_seek_p(pby, 8, SEEK_SET);
  669. for (int i = 0; i < s->planes; i++)
  670. bytestream2_put_be32(pby, s->plane[i].size);
  671. *got_packet = 1;
  672. return 0;
  673. }
  674. static av_cold int cfhd_encode_close(AVCodecContext *avctx)
  675. {
  676. CFHDEncContext *s = avctx->priv_data;
  677. for (int i = 0; i < s->planes; i++) {
  678. av_freep(&s->plane[i].dwt_buf);
  679. av_freep(&s->plane[i].dwt_tmp);
  680. for (int j = 0; j < SUBBAND_COUNT; j++)
  681. s->plane[i].subband[j] = NULL;
  682. for (int j = 0; j < 8; j++)
  683. s->plane[i].l_h[j] = NULL;
  684. }
  685. return 0;
  686. }
  687. #define OFFSET(x) offsetof(CFHDEncContext, x)
  688. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  689. static const AVOption options[] = {
  690. { "quality", "set quality", OFFSET(quality), AV_OPT_TYPE_INT, {.i64= 0}, 0, 12, VE, "q" },
  691. { "film3+", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 0}, 0, 0, VE, "q" },
  692. { "film3", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 1}, 0, 0, VE, "q" },
  693. { "film2+", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 2}, 0, 0, VE, "q" },
  694. { "film2", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 3}, 0, 0, VE, "q" },
  695. { "film1.5", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 4}, 0, 0, VE, "q" },
  696. { "film1+", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 5}, 0, 0, VE, "q" },
  697. { "film1", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 6}, 0, 0, VE, "q" },
  698. { "high+", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 7}, 0, 0, VE, "q" },
  699. { "high", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 8}, 0, 0, VE, "q" },
  700. { "medium+", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 9}, 0, 0, VE, "q" },
  701. { "medium", NULL, 0, AV_OPT_TYPE_CONST, {.i64=10}, 0, 0, VE, "q" },
  702. { "low+", NULL, 0, AV_OPT_TYPE_CONST, {.i64=11}, 0, 0, VE, "q" },
  703. { "low", NULL, 0, AV_OPT_TYPE_CONST, {.i64=12}, 0, 0, VE, "q" },
  704. { NULL},
  705. };
  706. static const AVClass cfhd_class = {
  707. .class_name = "cfhd",
  708. .item_name = av_default_item_name,
  709. .option = options,
  710. .version = LIBAVUTIL_VERSION_INT,
  711. };
  712. AVCodec ff_cfhd_encoder = {
  713. .name = "cfhd",
  714. .long_name = NULL_IF_CONFIG_SMALL("Cineform HD"),
  715. .type = AVMEDIA_TYPE_VIDEO,
  716. .id = AV_CODEC_ID_CFHD,
  717. .priv_data_size = sizeof(CFHDEncContext),
  718. .priv_class = &cfhd_class,
  719. .init = cfhd_encode_init,
  720. .close = cfhd_encode_close,
  721. .encode2 = cfhd_encode_frame,
  722. .capabilities = AV_CODEC_CAP_FRAME_THREADS,
  723. .pix_fmts = (const enum AVPixelFormat[]) {
  724. AV_PIX_FMT_YUV422P10,
  725. AV_PIX_FMT_GBRP12,
  726. AV_PIX_FMT_NONE
  727. },
  728. };