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.

1397 lines
50KB

  1. /*
  2. * FFV1 encoder
  3. *
  4. * Copyright (c) 2003-2013 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * FF Video Codec 1 (a lossless codec) encoder
  25. */
  26. #include "libavutil/attributes.h"
  27. #include "libavutil/avassert.h"
  28. #include "libavutil/crc.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/imgutils.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "libavutil/timer.h"
  33. #include "avcodec.h"
  34. #include "internal.h"
  35. #include "put_bits.h"
  36. #include "rangecoder.h"
  37. #include "golomb.h"
  38. #include "mathops.h"
  39. #include "ffv1.h"
  40. static const int8_t quant5_10bit[256] = {
  41. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
  42. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  43. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  44. 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  45. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  46. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  47. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  48. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  49. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  50. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  51. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  52. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  53. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1,
  54. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  55. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  56. -1, -1, -1, -1, -1, -1, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0,
  57. };
  58. static const int8_t quant5[256] = {
  59. 0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  60. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  61. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  62. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  63. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  64. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  65. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  66. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  67. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  68. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  69. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  70. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  71. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  72. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  73. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  74. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -1,
  75. };
  76. static const int8_t quant9_10bit[256] = {
  77. 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
  78. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3,
  79. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  80. 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  81. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  82. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  83. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  84. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  85. -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
  86. -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
  87. -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
  88. -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
  89. -4, -4, -4, -4, -4, -4, -4, -4, -4, -3, -3, -3, -3, -3, -3, -3,
  90. -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
  91. -3, -3, -3, -3, -3, -3, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  92. -2, -2, -2, -2, -1, -1, -1, -1, -1, -1, -1, -1, -0, -0, -0, -0,
  93. };
  94. static const int8_t quant11[256] = {
  95. 0, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
  96. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  97. 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  98. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  99. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  100. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  101. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  102. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  103. -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
  104. -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
  105. -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
  106. -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
  107. -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
  108. -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -4, -4,
  109. -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
  110. -4, -4, -4, -4, -4, -3, -3, -3, -3, -3, -3, -3, -2, -2, -2, -1,
  111. };
  112. static const uint8_t ver2_state[256] = {
  113. 0, 10, 10, 10, 10, 16, 16, 16, 28, 16, 16, 29, 42, 49, 20, 49,
  114. 59, 25, 26, 26, 27, 31, 33, 33, 33, 34, 34, 37, 67, 38, 39, 39,
  115. 40, 40, 41, 79, 43, 44, 45, 45, 48, 48, 64, 50, 51, 52, 88, 52,
  116. 53, 74, 55, 57, 58, 58, 74, 60, 101, 61, 62, 84, 66, 66, 68, 69,
  117. 87, 82, 71, 97, 73, 73, 82, 75, 111, 77, 94, 78, 87, 81, 83, 97,
  118. 85, 83, 94, 86, 99, 89, 90, 99, 111, 92, 93, 134, 95, 98, 105, 98,
  119. 105, 110, 102, 108, 102, 118, 103, 106, 106, 113, 109, 112, 114, 112, 116, 125,
  120. 115, 116, 117, 117, 126, 119, 125, 121, 121, 123, 145, 124, 126, 131, 127, 129,
  121. 165, 130, 132, 138, 133, 135, 145, 136, 137, 139, 146, 141, 143, 142, 144, 148,
  122. 147, 155, 151, 149, 151, 150, 152, 157, 153, 154, 156, 168, 158, 162, 161, 160,
  123. 172, 163, 169, 164, 166, 184, 167, 170, 177, 174, 171, 173, 182, 176, 180, 178,
  124. 175, 189, 179, 181, 186, 183, 192, 185, 200, 187, 191, 188, 190, 197, 193, 196,
  125. 197, 194, 195, 196, 198, 202, 199, 201, 210, 203, 207, 204, 205, 206, 208, 214,
  126. 209, 211, 221, 212, 213, 215, 224, 216, 217, 218, 219, 220, 222, 228, 223, 225,
  127. 226, 224, 227, 229, 240, 230, 231, 232, 233, 234, 235, 236, 238, 239, 237, 242,
  128. 241, 243, 242, 244, 245, 246, 247, 248, 249, 250, 251, 252, 252, 253, 254, 255,
  129. };
  130. static void find_best_state(uint8_t best_state[256][256],
  131. const uint8_t one_state[256])
  132. {
  133. int i, j, k, m;
  134. double l2tab[256];
  135. for (i = 1; i < 256; i++)
  136. l2tab[i] = log2(i / 256.0);
  137. for (i = 0; i < 256; i++) {
  138. double best_len[256];
  139. double p = i / 256.0;
  140. for (j = 0; j < 256; j++)
  141. best_len[j] = 1 << 30;
  142. for (j = FFMAX(i - 10, 1); j < FFMIN(i + 11, 256); j++) {
  143. double occ[256] = { 0 };
  144. double len = 0;
  145. occ[j] = 1.0;
  146. for (k = 0; k < 256; k++) {
  147. double newocc[256] = { 0 };
  148. for (m = 1; m < 256; m++)
  149. if (occ[m]) {
  150. len -=occ[m]*( p *l2tab[ m]
  151. + (1-p)*l2tab[256-m]);
  152. }
  153. if (len < best_len[k]) {
  154. best_len[k] = len;
  155. best_state[i][k] = j;
  156. }
  157. for (m = 1; m < 256; m++)
  158. if (occ[m]) {
  159. newocc[ one_state[ m]] += occ[m] * p;
  160. newocc[256 - one_state[256 - m]] += occ[m] * (1 - p);
  161. }
  162. memcpy(occ, newocc, sizeof(occ));
  163. }
  164. }
  165. }
  166. }
  167. static av_always_inline av_flatten void put_symbol_inline(RangeCoder *c,
  168. uint8_t *state, int v,
  169. int is_signed,
  170. uint64_t rc_stat[256][2],
  171. uint64_t rc_stat2[32][2])
  172. {
  173. int i;
  174. #define put_rac(C, S, B) \
  175. do { \
  176. if (rc_stat) { \
  177. rc_stat[*(S)][B]++; \
  178. rc_stat2[(S) - state][B]++; \
  179. } \
  180. put_rac(C, S, B); \
  181. } while (0)
  182. if (v) {
  183. const int a = FFABS(v);
  184. const int e = av_log2(a);
  185. put_rac(c, state + 0, 0);
  186. if (e <= 9) {
  187. for (i = 0; i < e; i++)
  188. put_rac(c, state + 1 + i, 1); // 1..10
  189. put_rac(c, state + 1 + i, 0);
  190. for (i = e - 1; i >= 0; i--)
  191. put_rac(c, state + 22 + i, (a >> i) & 1); // 22..31
  192. if (is_signed)
  193. put_rac(c, state + 11 + e, v < 0); // 11..21
  194. } else {
  195. for (i = 0; i < e; i++)
  196. put_rac(c, state + 1 + FFMIN(i, 9), 1); // 1..10
  197. put_rac(c, state + 1 + 9, 0);
  198. for (i = e - 1; i >= 0; i--)
  199. put_rac(c, state + 22 + FFMIN(i, 9), (a >> i) & 1); // 22..31
  200. if (is_signed)
  201. put_rac(c, state + 11 + 10, v < 0); // 11..21
  202. }
  203. } else {
  204. put_rac(c, state + 0, 1);
  205. }
  206. #undef put_rac
  207. }
  208. static av_noinline void put_symbol(RangeCoder *c, uint8_t *state,
  209. int v, int is_signed)
  210. {
  211. put_symbol_inline(c, state, v, is_signed, NULL, NULL);
  212. }
  213. static inline void put_vlc_symbol(PutBitContext *pb, VlcState *const state,
  214. int v, int bits)
  215. {
  216. int i, k, code;
  217. v = fold(v - state->bias, bits);
  218. i = state->count;
  219. k = 0;
  220. while (i < state->error_sum) { // FIXME: optimize
  221. k++;
  222. i += i;
  223. }
  224. av_assert2(k <= 13);
  225. #if 0 // JPEG LS
  226. if (k == 0 && 2 * state->drift <= -state->count)
  227. code = v ^ (-1);
  228. else
  229. code = v;
  230. #else
  231. code = v ^ ((2 * state->drift + state->count) >> 31);
  232. #endif
  233. ff_dlog(NULL, "v:%d/%d bias:%d error:%d drift:%d count:%d k:%d\n", v, code,
  234. state->bias, state->error_sum, state->drift, state->count, k);
  235. set_sr_golomb(pb, code, k, 12, bits);
  236. update_vlc_state(state, v);
  237. }
  238. static av_always_inline int encode_line(FFV1Context *s, int w,
  239. int16_t *sample[3],
  240. int plane_index, int bits)
  241. {
  242. PlaneContext *const p = &s->plane[plane_index];
  243. RangeCoder *const c = &s->c;
  244. int x;
  245. int run_index = s->run_index;
  246. int run_count = 0;
  247. int run_mode = 0;
  248. if (s->ac) {
  249. if (c->bytestream_end - c->bytestream < w * 35) {
  250. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  251. return AVERROR_INVALIDDATA;
  252. }
  253. } else {
  254. if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < w * 4) {
  255. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  256. return AVERROR_INVALIDDATA;
  257. }
  258. }
  259. if (s->slice_coding_mode == 1) {
  260. for (x = 0; x < w; x++) {
  261. int i;
  262. int v = sample[0][x];
  263. for (i = bits-1; i>=0; i--) {
  264. uint8_t state = 128;
  265. put_rac(c, &state, (v>>i) & 1);
  266. }
  267. }
  268. return 0;
  269. }
  270. for (x = 0; x < w; x++) {
  271. int diff, context;
  272. context = get_context(p, sample[0] + x, sample[1] + x, sample[2] + x);
  273. diff = sample[0][x] - predict(sample[0] + x, sample[1] + x);
  274. if (context < 0) {
  275. context = -context;
  276. diff = -diff;
  277. }
  278. diff = fold(diff, bits);
  279. if (s->ac) {
  280. if (s->flags & AV_CODEC_FLAG_PASS1) {
  281. put_symbol_inline(c, p->state[context], diff, 1, s->rc_stat,
  282. s->rc_stat2[p->quant_table_index][context]);
  283. } else {
  284. put_symbol_inline(c, p->state[context], diff, 1, NULL, NULL);
  285. }
  286. } else {
  287. if (context == 0)
  288. run_mode = 1;
  289. if (run_mode) {
  290. if (diff) {
  291. while (run_count >= 1 << ff_log2_run[run_index]) {
  292. run_count -= 1 << ff_log2_run[run_index];
  293. run_index++;
  294. put_bits(&s->pb, 1, 1);
  295. }
  296. put_bits(&s->pb, 1 + ff_log2_run[run_index], run_count);
  297. if (run_index)
  298. run_index--;
  299. run_count = 0;
  300. run_mode = 0;
  301. if (diff > 0)
  302. diff--;
  303. } else {
  304. run_count++;
  305. }
  306. }
  307. ff_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
  308. run_count, run_index, run_mode, x,
  309. (int)put_bits_count(&s->pb));
  310. if (run_mode == 0)
  311. put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
  312. }
  313. }
  314. if (run_mode) {
  315. while (run_count >= 1 << ff_log2_run[run_index]) {
  316. run_count -= 1 << ff_log2_run[run_index];
  317. run_index++;
  318. put_bits(&s->pb, 1, 1);
  319. }
  320. if (run_count)
  321. put_bits(&s->pb, 1, 1);
  322. }
  323. s->run_index = run_index;
  324. return 0;
  325. }
  326. static int encode_plane(FFV1Context *s, uint8_t *src, int w, int h,
  327. int stride, int plane_index)
  328. {
  329. int x, y, i, ret;
  330. const int ring_size = s->avctx->context_model ? 3 : 2;
  331. int16_t *sample[3];
  332. s->run_index = 0;
  333. memset(s->sample_buffer, 0, ring_size * (w + 6) * sizeof(*s->sample_buffer));
  334. for (y = 0; y < h; y++) {
  335. for (i = 0; i < ring_size; i++)
  336. sample[i] = s->sample_buffer + (w + 6) * ((h + i - y) % ring_size) + 3;
  337. sample[0][-1]= sample[1][0 ];
  338. sample[1][ w]= sample[1][w-1];
  339. // { START_TIMER
  340. if (s->bits_per_raw_sample <= 8) {
  341. for (x = 0; x < w; x++)
  342. sample[0][x] = src[x + stride * y];
  343. if((ret = encode_line(s, w, sample, plane_index, 8)) < 0)
  344. return ret;
  345. } else {
  346. if (s->packed_at_lsb) {
  347. for (x = 0; x < w; x++) {
  348. sample[0][x] = ((uint16_t*)(src + stride*y))[x];
  349. }
  350. } else {
  351. for (x = 0; x < w; x++) {
  352. sample[0][x] = ((uint16_t*)(src + stride*y))[x] >> (16 - s->bits_per_raw_sample);
  353. }
  354. }
  355. if((ret = encode_line(s, w, sample, plane_index, s->bits_per_raw_sample)) < 0)
  356. return ret;
  357. }
  358. // STOP_TIMER("encode line") }
  359. }
  360. return 0;
  361. }
  362. static int encode_rgb_frame(FFV1Context *s, const uint8_t *src[3],
  363. int w, int h, const int stride[3])
  364. {
  365. int x, y, p, i;
  366. const int ring_size = s->avctx->context_model ? 3 : 2;
  367. int16_t *sample[4][3];
  368. int lbd = s->bits_per_raw_sample <= 8;
  369. int bits = s->bits_per_raw_sample > 0 ? s->bits_per_raw_sample : 8;
  370. int offset = 1 << bits;
  371. s->run_index = 0;
  372. memset(s->sample_buffer, 0, ring_size * MAX_PLANES *
  373. (w + 6) * sizeof(*s->sample_buffer));
  374. for (y = 0; y < h; y++) {
  375. for (i = 0; i < ring_size; i++)
  376. for (p = 0; p < MAX_PLANES; p++)
  377. sample[p][i]= s->sample_buffer + p*ring_size*(w+6) + ((h+i-y)%ring_size)*(w+6) + 3;
  378. for (x = 0; x < w; x++) {
  379. int b, g, r, av_uninit(a);
  380. if (lbd) {
  381. unsigned v = *((const uint32_t*)(src[0] + x*4 + stride[0]*y));
  382. b = v & 0xFF;
  383. g = (v >> 8) & 0xFF;
  384. r = (v >> 16) & 0xFF;
  385. a = v >> 24;
  386. } else {
  387. b = *((const uint16_t *)(src[0] + x*2 + stride[0]*y));
  388. g = *((const uint16_t *)(src[1] + x*2 + stride[1]*y));
  389. r = *((const uint16_t *)(src[2] + x*2 + stride[2]*y));
  390. }
  391. if (s->slice_coding_mode != 1) {
  392. b -= g;
  393. r -= g;
  394. g += (b * s->slice_rct_by_coef + r * s->slice_rct_ry_coef) >> 2;
  395. b += offset;
  396. r += offset;
  397. }
  398. sample[0][0][x] = g;
  399. sample[1][0][x] = b;
  400. sample[2][0][x] = r;
  401. sample[3][0][x] = a;
  402. }
  403. for (p = 0; p < 3 + s->transparency; p++) {
  404. int ret;
  405. sample[p][0][-1] = sample[p][1][0 ];
  406. sample[p][1][ w] = sample[p][1][w-1];
  407. if (lbd && s->slice_coding_mode == 0)
  408. ret = encode_line(s, w, sample[p], (p + 1) / 2, 9);
  409. else
  410. ret = encode_line(s, w, sample[p], (p + 1) / 2, bits + (s->slice_coding_mode != 1));
  411. if (ret < 0)
  412. return ret;
  413. }
  414. }
  415. return 0;
  416. }
  417. static void write_quant_table(RangeCoder *c, int16_t *quant_table)
  418. {
  419. int last = 0;
  420. int i;
  421. uint8_t state[CONTEXT_SIZE];
  422. memset(state, 128, sizeof(state));
  423. for (i = 1; i < 128; i++)
  424. if (quant_table[i] != quant_table[i - 1]) {
  425. put_symbol(c, state, i - last - 1, 0);
  426. last = i;
  427. }
  428. put_symbol(c, state, i - last - 1, 0);
  429. }
  430. static void write_quant_tables(RangeCoder *c,
  431. int16_t quant_table[MAX_CONTEXT_INPUTS][256])
  432. {
  433. int i;
  434. for (i = 0; i < 5; i++)
  435. write_quant_table(c, quant_table[i]);
  436. }
  437. static void write_header(FFV1Context *f)
  438. {
  439. uint8_t state[CONTEXT_SIZE];
  440. int i, j;
  441. RangeCoder *const c = &f->slice_context[0]->c;
  442. memset(state, 128, sizeof(state));
  443. if (f->version < 2) {
  444. put_symbol(c, state, f->version, 0);
  445. put_symbol(c, state, f->ac, 0);
  446. if (f->ac > 1) {
  447. for (i = 1; i < 256; i++)
  448. put_symbol(c, state,
  449. f->state_transition[i] - c->one_state[i], 1);
  450. }
  451. put_symbol(c, state, f->colorspace, 0); //YUV cs type
  452. if (f->version > 0)
  453. put_symbol(c, state, f->bits_per_raw_sample, 0);
  454. put_rac(c, state, f->chroma_planes);
  455. put_symbol(c, state, f->chroma_h_shift, 0);
  456. put_symbol(c, state, f->chroma_v_shift, 0);
  457. put_rac(c, state, f->transparency);
  458. write_quant_tables(c, f->quant_table);
  459. } else if (f->version < 3) {
  460. put_symbol(c, state, f->slice_count, 0);
  461. for (i = 0; i < f->slice_count; i++) {
  462. FFV1Context *fs = f->slice_context[i];
  463. put_symbol(c, state,
  464. (fs->slice_x + 1) * f->num_h_slices / f->width, 0);
  465. put_symbol(c, state,
  466. (fs->slice_y + 1) * f->num_v_slices / f->height, 0);
  467. put_symbol(c, state,
  468. (fs->slice_width + 1) * f->num_h_slices / f->width - 1,
  469. 0);
  470. put_symbol(c, state,
  471. (fs->slice_height + 1) * f->num_v_slices / f->height - 1,
  472. 0);
  473. for (j = 0; j < f->plane_count; j++) {
  474. put_symbol(c, state, f->plane[j].quant_table_index, 0);
  475. av_assert0(f->plane[j].quant_table_index == f->avctx->context_model);
  476. }
  477. }
  478. }
  479. }
  480. static int write_extradata(FFV1Context *f)
  481. {
  482. RangeCoder *const c = &f->c;
  483. uint8_t state[CONTEXT_SIZE];
  484. int i, j, k;
  485. uint8_t state2[32][CONTEXT_SIZE];
  486. unsigned v;
  487. memset(state2, 128, sizeof(state2));
  488. memset(state, 128, sizeof(state));
  489. f->avctx->extradata_size = 10000 + 4 +
  490. (11 * 11 * 5 * 5 * 5 + 11 * 11 * 11) * 32;
  491. f->avctx->extradata = av_malloc(f->avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  492. if (!f->avctx->extradata)
  493. return AVERROR(ENOMEM);
  494. ff_init_range_encoder(c, f->avctx->extradata, f->avctx->extradata_size);
  495. ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
  496. put_symbol(c, state, f->version, 0);
  497. if (f->version > 2) {
  498. if (f->version == 3) {
  499. f->micro_version = 4;
  500. } else if (f->version == 4)
  501. f->micro_version = 2;
  502. put_symbol(c, state, f->micro_version, 0);
  503. }
  504. put_symbol(c, state, f->ac, 0);
  505. if (f->ac > 1)
  506. for (i = 1; i < 256; i++)
  507. put_symbol(c, state, f->state_transition[i] - c->one_state[i], 1);
  508. put_symbol(c, state, f->colorspace, 0); // YUV cs type
  509. put_symbol(c, state, f->bits_per_raw_sample, 0);
  510. put_rac(c, state, f->chroma_planes);
  511. put_symbol(c, state, f->chroma_h_shift, 0);
  512. put_symbol(c, state, f->chroma_v_shift, 0);
  513. put_rac(c, state, f->transparency);
  514. put_symbol(c, state, f->num_h_slices - 1, 0);
  515. put_symbol(c, state, f->num_v_slices - 1, 0);
  516. put_symbol(c, state, f->quant_table_count, 0);
  517. for (i = 0; i < f->quant_table_count; i++)
  518. write_quant_tables(c, f->quant_tables[i]);
  519. for (i = 0; i < f->quant_table_count; i++) {
  520. for (j = 0; j < f->context_count[i] * CONTEXT_SIZE; j++)
  521. if (f->initial_states[i] && f->initial_states[i][0][j] != 128)
  522. break;
  523. if (j < f->context_count[i] * CONTEXT_SIZE) {
  524. put_rac(c, state, 1);
  525. for (j = 0; j < f->context_count[i]; j++)
  526. for (k = 0; k < CONTEXT_SIZE; k++) {
  527. int pred = j ? f->initial_states[i][j - 1][k] : 128;
  528. put_symbol(c, state2[k],
  529. (int8_t)(f->initial_states[i][j][k] - pred), 1);
  530. }
  531. } else {
  532. put_rac(c, state, 0);
  533. }
  534. }
  535. if (f->version > 2) {
  536. put_symbol(c, state, f->ec, 0);
  537. put_symbol(c, state, f->intra = (f->avctx->gop_size < 2), 0);
  538. }
  539. f->avctx->extradata_size = ff_rac_terminate(c);
  540. v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, f->avctx->extradata, f->avctx->extradata_size);
  541. AV_WL32(f->avctx->extradata + f->avctx->extradata_size, v);
  542. f->avctx->extradata_size += 4;
  543. return 0;
  544. }
  545. static int sort_stt(FFV1Context *s, uint8_t stt[256])
  546. {
  547. int i, i2, changed, print = 0;
  548. do {
  549. changed = 0;
  550. for (i = 12; i < 244; i++) {
  551. for (i2 = i + 1; i2 < 245 && i2 < i + 4; i2++) {
  552. #define COST(old, new) \
  553. s->rc_stat[old][0] * -log2((256 - (new)) / 256.0) + \
  554. s->rc_stat[old][1] * -log2((new) / 256.0)
  555. #define COST2(old, new) \
  556. COST(old, new) + COST(256 - (old), 256 - (new))
  557. double size0 = COST2(i, i) + COST2(i2, i2);
  558. double sizeX = COST2(i, i2) + COST2(i2, i);
  559. if (size0 - sizeX > size0*(1e-14) && i != 128 && i2 != 128) {
  560. int j;
  561. FFSWAP(int, stt[i], stt[i2]);
  562. FFSWAP(int, s->rc_stat[i][0], s->rc_stat[i2][0]);
  563. FFSWAP(int, s->rc_stat[i][1], s->rc_stat[i2][1]);
  564. if (i != 256 - i2) {
  565. FFSWAP(int, stt[256 - i], stt[256 - i2]);
  566. FFSWAP(int, s->rc_stat[256 - i][0], s->rc_stat[256 - i2][0]);
  567. FFSWAP(int, s->rc_stat[256 - i][1], s->rc_stat[256 - i2][1]);
  568. }
  569. for (j = 1; j < 256; j++) {
  570. if (stt[j] == i)
  571. stt[j] = i2;
  572. else if (stt[j] == i2)
  573. stt[j] = i;
  574. if (i != 256 - i2) {
  575. if (stt[256 - j] == 256 - i)
  576. stt[256 - j] = 256 - i2;
  577. else if (stt[256 - j] == 256 - i2)
  578. stt[256 - j] = 256 - i;
  579. }
  580. }
  581. print = changed = 1;
  582. }
  583. }
  584. }
  585. } while (changed);
  586. return print;
  587. }
  588. static av_cold int encode_init(AVCodecContext *avctx)
  589. {
  590. FFV1Context *s = avctx->priv_data;
  591. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  592. int i, j, k, m, ret;
  593. if ((ret = ff_ffv1_common_init(avctx)) < 0)
  594. return ret;
  595. s->version = 0;
  596. if ((avctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) ||
  597. avctx->slices > 1)
  598. s->version = FFMAX(s->version, 2);
  599. // Unspecified level & slices, we choose version 1.2+ to ensure multithreaded decodability
  600. if (avctx->slices == 0 && avctx->level < 0 && avctx->width * avctx->height > 720*576)
  601. s->version = FFMAX(s->version, 2);
  602. if (avctx->level <= 0 && s->version == 2) {
  603. s->version = 3;
  604. }
  605. if (avctx->level >= 0 && avctx->level <= 4) {
  606. if (avctx->level < s->version) {
  607. av_log(avctx, AV_LOG_ERROR, "Version %d needed for requested features but %d requested\n", s->version, avctx->level);
  608. return AVERROR(EINVAL);
  609. }
  610. s->version = avctx->level;
  611. }
  612. if (s->ec < 0) {
  613. s->ec = (s->version >= 3);
  614. }
  615. if ((s->version == 2 || s->version>3) && avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  616. av_log(avctx, AV_LOG_ERROR, "Version 2 needed for requested features but version 2 is experimental and not enabled\n");
  617. return AVERROR_INVALIDDATA;
  618. }
  619. s->ac = avctx->coder_type > 0 ? 2 : 0;
  620. s->plane_count = 3;
  621. switch(avctx->pix_fmt) {
  622. case AV_PIX_FMT_YUV444P9:
  623. case AV_PIX_FMT_YUV422P9:
  624. case AV_PIX_FMT_YUV420P9:
  625. case AV_PIX_FMT_YUVA444P9:
  626. case AV_PIX_FMT_YUVA422P9:
  627. case AV_PIX_FMT_YUVA420P9:
  628. if (!avctx->bits_per_raw_sample)
  629. s->bits_per_raw_sample = 9;
  630. case AV_PIX_FMT_YUV444P10:
  631. case AV_PIX_FMT_YUV420P10:
  632. case AV_PIX_FMT_YUV422P10:
  633. case AV_PIX_FMT_YUVA444P10:
  634. case AV_PIX_FMT_YUVA422P10:
  635. case AV_PIX_FMT_YUVA420P10:
  636. s->packed_at_lsb = 1;
  637. if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
  638. s->bits_per_raw_sample = 10;
  639. case AV_PIX_FMT_GRAY16:
  640. case AV_PIX_FMT_YUV444P16:
  641. case AV_PIX_FMT_YUV422P16:
  642. case AV_PIX_FMT_YUV420P16:
  643. case AV_PIX_FMT_YUVA444P16:
  644. case AV_PIX_FMT_YUVA422P16:
  645. case AV_PIX_FMT_YUVA420P16:
  646. if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample) {
  647. s->bits_per_raw_sample = 16;
  648. } else if (!s->bits_per_raw_sample) {
  649. s->bits_per_raw_sample = avctx->bits_per_raw_sample;
  650. }
  651. if (s->bits_per_raw_sample <= 8) {
  652. av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
  653. return AVERROR_INVALIDDATA;
  654. }
  655. if (!s->ac && avctx->coder_type == -1) {
  656. av_log(avctx, AV_LOG_INFO, "bits_per_raw_sample > 8, forcing coder 1\n");
  657. s->ac = 2;
  658. }
  659. if (!s->ac) {
  660. av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample of more than 8 needs -coder 1 currently\n");
  661. return AVERROR(ENOSYS);
  662. }
  663. s->version = FFMAX(s->version, 1);
  664. case AV_PIX_FMT_GRAY8:
  665. case AV_PIX_FMT_YUV444P:
  666. case AV_PIX_FMT_YUV440P:
  667. case AV_PIX_FMT_YUV422P:
  668. case AV_PIX_FMT_YUV420P:
  669. case AV_PIX_FMT_YUV411P:
  670. case AV_PIX_FMT_YUV410P:
  671. case AV_PIX_FMT_YUVA444P:
  672. case AV_PIX_FMT_YUVA422P:
  673. case AV_PIX_FMT_YUVA420P:
  674. s->chroma_planes = desc->nb_components < 3 ? 0 : 1;
  675. s->colorspace = 0;
  676. s->transparency = desc->nb_components == 4;
  677. if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
  678. s->bits_per_raw_sample = 8;
  679. else if (!s->bits_per_raw_sample)
  680. s->bits_per_raw_sample = 8;
  681. break;
  682. case AV_PIX_FMT_RGB32:
  683. s->colorspace = 1;
  684. s->transparency = 1;
  685. s->chroma_planes = 1;
  686. if (!avctx->bits_per_raw_sample)
  687. s->bits_per_raw_sample = 8;
  688. break;
  689. case AV_PIX_FMT_0RGB32:
  690. s->colorspace = 1;
  691. s->chroma_planes = 1;
  692. if (!avctx->bits_per_raw_sample)
  693. s->bits_per_raw_sample = 8;
  694. break;
  695. case AV_PIX_FMT_GBRP9:
  696. if (!avctx->bits_per_raw_sample)
  697. s->bits_per_raw_sample = 9;
  698. case AV_PIX_FMT_GBRP10:
  699. if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
  700. s->bits_per_raw_sample = 10;
  701. case AV_PIX_FMT_GBRP12:
  702. if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
  703. s->bits_per_raw_sample = 12;
  704. case AV_PIX_FMT_GBRP14:
  705. if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
  706. s->bits_per_raw_sample = 14;
  707. else if (!s->bits_per_raw_sample)
  708. s->bits_per_raw_sample = avctx->bits_per_raw_sample;
  709. s->colorspace = 1;
  710. s->chroma_planes = 1;
  711. s->version = FFMAX(s->version, 1);
  712. if (!s->ac && avctx->coder_type == -1) {
  713. av_log(avctx, AV_LOG_INFO, "bits_per_raw_sample > 8, forcing coder 1\n");
  714. s->ac = 2;
  715. }
  716. if (!s->ac) {
  717. av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample of more than 8 needs -coder 1 currently\n");
  718. return AVERROR(ENOSYS);
  719. }
  720. break;
  721. default:
  722. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  723. return AVERROR(ENOSYS);
  724. }
  725. av_assert0(s->bits_per_raw_sample >= 8);
  726. if (s->transparency) {
  727. av_log(avctx, AV_LOG_WARNING, "Storing alpha plane, this will require a recent FFV1 decoder to playback!\n");
  728. }
  729. if (avctx->context_model > 1U) {
  730. av_log(avctx, AV_LOG_ERROR, "Invalid context model %d, valid values are 0 and 1\n", avctx->context_model);
  731. return AVERROR(EINVAL);
  732. }
  733. if (s->ac > 1)
  734. for (i = 1; i < 256; i++)
  735. s->state_transition[i] = ver2_state[i];
  736. for (i = 0; i < 256; i++) {
  737. s->quant_table_count = 2;
  738. if (s->bits_per_raw_sample <= 8) {
  739. s->quant_tables[0][0][i]= quant11[i];
  740. s->quant_tables[0][1][i]= 11*quant11[i];
  741. s->quant_tables[0][2][i]= 11*11*quant11[i];
  742. s->quant_tables[1][0][i]= quant11[i];
  743. s->quant_tables[1][1][i]= 11*quant11[i];
  744. s->quant_tables[1][2][i]= 11*11*quant5 [i];
  745. s->quant_tables[1][3][i]= 5*11*11*quant5 [i];
  746. s->quant_tables[1][4][i]= 5*5*11*11*quant5 [i];
  747. } else {
  748. s->quant_tables[0][0][i]= quant9_10bit[i];
  749. s->quant_tables[0][1][i]= 11*quant9_10bit[i];
  750. s->quant_tables[0][2][i]= 11*11*quant9_10bit[i];
  751. s->quant_tables[1][0][i]= quant9_10bit[i];
  752. s->quant_tables[1][1][i]= 11*quant9_10bit[i];
  753. s->quant_tables[1][2][i]= 11*11*quant5_10bit[i];
  754. s->quant_tables[1][3][i]= 5*11*11*quant5_10bit[i];
  755. s->quant_tables[1][4][i]= 5*5*11*11*quant5_10bit[i];
  756. }
  757. }
  758. s->context_count[0] = (11 * 11 * 11 + 1) / 2;
  759. s->context_count[1] = (11 * 11 * 5 * 5 * 5 + 1) / 2;
  760. memcpy(s->quant_table, s->quant_tables[avctx->context_model],
  761. sizeof(s->quant_table));
  762. for (i = 0; i < s->plane_count; i++) {
  763. PlaneContext *const p = &s->plane[i];
  764. memcpy(p->quant_table, s->quant_table, sizeof(p->quant_table));
  765. p->quant_table_index = avctx->context_model;
  766. p->context_count = s->context_count[p->quant_table_index];
  767. }
  768. if ((ret = ff_ffv1_allocate_initial_states(s)) < 0)
  769. return ret;
  770. #if FF_API_CODED_FRAME
  771. FF_DISABLE_DEPRECATION_WARNINGS
  772. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  773. FF_ENABLE_DEPRECATION_WARNINGS
  774. #endif
  775. if (!s->transparency)
  776. s->plane_count = 2;
  777. if (!s->chroma_planes && s->version > 3)
  778. s->plane_count--;
  779. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  780. s->picture_number = 0;
  781. if (avctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) {
  782. for (i = 0; i < s->quant_table_count; i++) {
  783. s->rc_stat2[i] = av_mallocz(s->context_count[i] *
  784. sizeof(*s->rc_stat2[i]));
  785. if (!s->rc_stat2[i])
  786. return AVERROR(ENOMEM);
  787. }
  788. }
  789. if (avctx->stats_in) {
  790. char *p = avctx->stats_in;
  791. uint8_t (*best_state)[256] = av_malloc_array(256, 256);
  792. int gob_count = 0;
  793. char *next;
  794. if (!best_state)
  795. return AVERROR(ENOMEM);
  796. av_assert0(s->version >= 2);
  797. for (;;) {
  798. for (j = 0; j < 256; j++)
  799. for (i = 0; i < 2; i++) {
  800. s->rc_stat[j][i] = strtol(p, &next, 0);
  801. if (next == p) {
  802. av_log(avctx, AV_LOG_ERROR,
  803. "2Pass file invalid at %d %d [%s]\n", j, i, p);
  804. av_freep(&best_state);
  805. return AVERROR_INVALIDDATA;
  806. }
  807. p = next;
  808. }
  809. for (i = 0; i < s->quant_table_count; i++)
  810. for (j = 0; j < s->context_count[i]; j++) {
  811. for (k = 0; k < 32; k++)
  812. for (m = 0; m < 2; m++) {
  813. s->rc_stat2[i][j][k][m] = strtol(p, &next, 0);
  814. if (next == p) {
  815. av_log(avctx, AV_LOG_ERROR,
  816. "2Pass file invalid at %d %d %d %d [%s]\n",
  817. i, j, k, m, p);
  818. av_freep(&best_state);
  819. return AVERROR_INVALIDDATA;
  820. }
  821. p = next;
  822. }
  823. }
  824. gob_count = strtol(p, &next, 0);
  825. if (next == p || gob_count <= 0) {
  826. av_log(avctx, AV_LOG_ERROR, "2Pass file invalid\n");
  827. av_freep(&best_state);
  828. return AVERROR_INVALIDDATA;
  829. }
  830. p = next;
  831. while (*p == '\n' || *p == ' ')
  832. p++;
  833. if (p[0] == 0)
  834. break;
  835. }
  836. sort_stt(s, s->state_transition);
  837. find_best_state(best_state, s->state_transition);
  838. for (i = 0; i < s->quant_table_count; i++) {
  839. for (k = 0; k < 32; k++) {
  840. double a=0, b=0;
  841. int jp = 0;
  842. for (j = 0; j < s->context_count[i]; j++) {
  843. double p = 128;
  844. if (s->rc_stat2[i][j][k][0] + s->rc_stat2[i][j][k][1] > 200 && j || a+b > 200) {
  845. if (a+b)
  846. p = 256.0 * b / (a + b);
  847. s->initial_states[i][jp][k] =
  848. best_state[av_clip(round(p), 1, 255)][av_clip_uint8((a + b) / gob_count)];
  849. for(jp++; jp<j; jp++)
  850. s->initial_states[i][jp][k] = s->initial_states[i][jp-1][k];
  851. a=b=0;
  852. }
  853. a += s->rc_stat2[i][j][k][0];
  854. b += s->rc_stat2[i][j][k][1];
  855. if (a+b) {
  856. p = 256.0 * b / (a + b);
  857. }
  858. s->initial_states[i][j][k] =
  859. best_state[av_clip(round(p), 1, 255)][av_clip_uint8((a + b) / gob_count)];
  860. }
  861. }
  862. }
  863. av_freep(&best_state);
  864. }
  865. if (s->version > 1) {
  866. s->num_v_slices = (avctx->width > 352 || avctx->height > 288 || !avctx->slices) ? 2 : 1;
  867. for (; s->num_v_slices < 9; s->num_v_slices++) {
  868. for (s->num_h_slices = s->num_v_slices; s->num_h_slices < 2*s->num_v_slices; s->num_h_slices++) {
  869. if (avctx->slices == s->num_h_slices * s->num_v_slices && avctx->slices <= 64 || !avctx->slices)
  870. goto slices_ok;
  871. }
  872. }
  873. av_log(avctx, AV_LOG_ERROR,
  874. "Unsupported number %d of slices requested, please specify a "
  875. "supported number with -slices (ex:4,6,9,12,16, ...)\n",
  876. avctx->slices);
  877. return AVERROR(ENOSYS);
  878. slices_ok:
  879. if ((ret = write_extradata(s)) < 0)
  880. return ret;
  881. }
  882. if ((ret = ff_ffv1_init_slice_contexts(s)) < 0)
  883. return ret;
  884. s->slice_count = s->max_slice_count;
  885. if ((ret = ff_ffv1_init_slices_state(s)) < 0)
  886. return ret;
  887. #define STATS_OUT_SIZE 1024 * 1024 * 6
  888. if (avctx->flags & AV_CODEC_FLAG_PASS1) {
  889. avctx->stats_out = av_mallocz(STATS_OUT_SIZE);
  890. if (!avctx->stats_out)
  891. return AVERROR(ENOMEM);
  892. for (i = 0; i < s->quant_table_count; i++)
  893. for (j = 0; j < s->max_slice_count; j++) {
  894. FFV1Context *sf = s->slice_context[j];
  895. av_assert0(!sf->rc_stat2[i]);
  896. sf->rc_stat2[i] = av_mallocz(s->context_count[i] *
  897. sizeof(*sf->rc_stat2[i]));
  898. if (!sf->rc_stat2[i])
  899. return AVERROR(ENOMEM);
  900. }
  901. }
  902. return 0;
  903. }
  904. static void encode_slice_header(FFV1Context *f, FFV1Context *fs)
  905. {
  906. RangeCoder *c = &fs->c;
  907. uint8_t state[CONTEXT_SIZE];
  908. int j;
  909. memset(state, 128, sizeof(state));
  910. put_symbol(c, state, (fs->slice_x +1)*f->num_h_slices / f->width , 0);
  911. put_symbol(c, state, (fs->slice_y +1)*f->num_v_slices / f->height , 0);
  912. put_symbol(c, state, (fs->slice_width +1)*f->num_h_slices / f->width -1, 0);
  913. put_symbol(c, state, (fs->slice_height+1)*f->num_v_slices / f->height-1, 0);
  914. for (j=0; j<f->plane_count; j++) {
  915. put_symbol(c, state, f->plane[j].quant_table_index, 0);
  916. av_assert0(f->plane[j].quant_table_index == f->avctx->context_model);
  917. }
  918. if (!f->picture.f->interlaced_frame)
  919. put_symbol(c, state, 3, 0);
  920. else
  921. put_symbol(c, state, 1 + !f->picture.f->top_field_first, 0);
  922. put_symbol(c, state, f->picture.f->sample_aspect_ratio.num, 0);
  923. put_symbol(c, state, f->picture.f->sample_aspect_ratio.den, 0);
  924. if (f->version > 3) {
  925. put_rac(c, state, fs->slice_coding_mode == 1);
  926. if (fs->slice_coding_mode == 1)
  927. ff_ffv1_clear_slice_state(f, fs);
  928. put_symbol(c, state, fs->slice_coding_mode, 0);
  929. if (fs->slice_coding_mode != 1) {
  930. put_symbol(c, state, fs->slice_rct_by_coef, 0);
  931. put_symbol(c, state, fs->slice_rct_ry_coef, 0);
  932. }
  933. }
  934. }
  935. static void choose_rct_params(FFV1Context *fs, const uint8_t *src[3], const int stride[3], int w, int h)
  936. {
  937. #define NB_Y_COEFF 15
  938. static const int rct_y_coeff[15][2] = {
  939. {0, 0}, // 4G
  940. {1, 1}, // R + 2G + B
  941. {2, 2}, // 2R + 2B
  942. {0, 2}, // 2G + 2B
  943. {2, 0}, // 2R + 2G
  944. {4, 0}, // 4R
  945. {0, 4}, // 4B
  946. {0, 3}, // 1G + 3B
  947. {3, 0}, // 3R + 1G
  948. {3, 1}, // 3R + B
  949. {1, 3}, // R + 3B
  950. {1, 2}, // R + G + 2B
  951. {2, 1}, // 2R + G + B
  952. {0, 1}, // 3G + B
  953. {1, 0}, // R + 3G
  954. };
  955. int stat[NB_Y_COEFF] = {0};
  956. int x, y, i, p, best;
  957. int16_t *sample[3];
  958. int lbd = fs->bits_per_raw_sample <= 8;
  959. for (y = 0; y < h; y++) {
  960. int lastr=0, lastg=0, lastb=0;
  961. for (p = 0; p < 3; p++)
  962. sample[p] = fs->sample_buffer + p*w;
  963. for (x = 0; x < w; x++) {
  964. int b, g, r;
  965. int ab, ag, ar;
  966. if (lbd) {
  967. unsigned v = *((const uint32_t*)(src[0] + x*4 + stride[0]*y));
  968. b = v & 0xFF;
  969. g = (v >> 8) & 0xFF;
  970. r = (v >> 16) & 0xFF;
  971. } else {
  972. b = *((const uint16_t*)(src[0] + x*2 + stride[0]*y));
  973. g = *((const uint16_t*)(src[1] + x*2 + stride[1]*y));
  974. r = *((const uint16_t*)(src[2] + x*2 + stride[2]*y));
  975. }
  976. ar = r - lastr;
  977. ag = g - lastg;
  978. ab = b - lastb;
  979. if (x && y) {
  980. int bg = ag - sample[0][x];
  981. int bb = ab - sample[1][x];
  982. int br = ar - sample[2][x];
  983. br -= bg;
  984. bb -= bg;
  985. for (i = 0; i<NB_Y_COEFF; i++) {
  986. stat[i] += FFABS(bg + ((br*rct_y_coeff[i][0] + bb*rct_y_coeff[i][1])>>2));
  987. }
  988. }
  989. sample[0][x] = ag;
  990. sample[1][x] = ab;
  991. sample[2][x] = ar;
  992. lastr = r;
  993. lastg = g;
  994. lastb = b;
  995. }
  996. }
  997. best = 0;
  998. for (i=1; i<NB_Y_COEFF; i++) {
  999. if (stat[i] < stat[best])
  1000. best = i;
  1001. }
  1002. fs->slice_rct_by_coef = rct_y_coeff[best][1];
  1003. fs->slice_rct_ry_coef = rct_y_coeff[best][0];
  1004. }
  1005. static int encode_slice(AVCodecContext *c, void *arg)
  1006. {
  1007. FFV1Context *fs = *(void **)arg;
  1008. FFV1Context *f = fs->avctx->priv_data;
  1009. int width = fs->slice_width;
  1010. int height = fs->slice_height;
  1011. int x = fs->slice_x;
  1012. int y = fs->slice_y;
  1013. const AVFrame *const p = f->picture.f;
  1014. const int ps = av_pix_fmt_desc_get(c->pix_fmt)->comp[0].step;
  1015. int ret;
  1016. RangeCoder c_bak = fs->c;
  1017. const uint8_t *planes[3] = {p->data[0] + ps*x + y*p->linesize[0],
  1018. p->data[1] + ps*x + y*p->linesize[1],
  1019. p->data[2] + ps*x + y*p->linesize[2]};
  1020. fs->slice_coding_mode = 0;
  1021. if (f->version > 3) {
  1022. choose_rct_params(fs, planes, p->linesize, width, height);
  1023. } else {
  1024. fs->slice_rct_by_coef = 1;
  1025. fs->slice_rct_ry_coef = 1;
  1026. }
  1027. retry:
  1028. if (f->key_frame)
  1029. ff_ffv1_clear_slice_state(f, fs);
  1030. if (f->version > 2) {
  1031. encode_slice_header(f, fs);
  1032. }
  1033. if (!fs->ac) {
  1034. if (f->version > 2)
  1035. put_rac(&fs->c, (uint8_t[]) { 129 }, 0);
  1036. fs->ac_byte_count = f->version > 2 || (!x && !y) ? ff_rac_terminate(&fs->c) : 0;
  1037. init_put_bits(&fs->pb,
  1038. fs->c.bytestream_start + fs->ac_byte_count,
  1039. fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count);
  1040. }
  1041. if (f->colorspace == 0) {
  1042. const int chroma_width = FF_CEIL_RSHIFT(width, f->chroma_h_shift);
  1043. const int chroma_height = FF_CEIL_RSHIFT(height, f->chroma_v_shift);
  1044. const int cx = x >> f->chroma_h_shift;
  1045. const int cy = y >> f->chroma_v_shift;
  1046. ret = encode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0);
  1047. if (f->chroma_planes) {
  1048. ret |= encode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
  1049. ret |= encode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1);
  1050. }
  1051. if (fs->transparency)
  1052. ret |= encode_plane(fs, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2);
  1053. } else {
  1054. ret = encode_rgb_frame(fs, planes, width, height, p->linesize);
  1055. }
  1056. emms_c();
  1057. if (ret < 0) {
  1058. av_assert0(fs->slice_coding_mode == 0);
  1059. if (fs->version < 4 || !fs->ac) {
  1060. av_log(c, AV_LOG_ERROR, "Buffer too small\n");
  1061. return ret;
  1062. }
  1063. av_log(c, AV_LOG_DEBUG, "Coding slice as PCM\n");
  1064. fs->slice_coding_mode = 1;
  1065. fs->c = c_bak;
  1066. goto retry;
  1067. }
  1068. return 0;
  1069. }
  1070. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  1071. const AVFrame *pict, int *got_packet)
  1072. {
  1073. FFV1Context *f = avctx->priv_data;
  1074. RangeCoder *const c = &f->slice_context[0]->c;
  1075. AVFrame *const p = f->picture.f;
  1076. int used_count = 0;
  1077. uint8_t keystate = 128;
  1078. uint8_t *buf_p;
  1079. int i, ret;
  1080. int64_t maxsize = AV_INPUT_BUFFER_MIN_SIZE
  1081. + avctx->width*avctx->height*35LL*4;
  1082. if(!pict) {
  1083. if (avctx->flags & AV_CODEC_FLAG_PASS1) {
  1084. int j, k, m;
  1085. char *p = avctx->stats_out;
  1086. char *end = p + STATS_OUT_SIZE;
  1087. memset(f->rc_stat, 0, sizeof(f->rc_stat));
  1088. for (i = 0; i < f->quant_table_count; i++)
  1089. memset(f->rc_stat2[i], 0, f->context_count[i] * sizeof(*f->rc_stat2[i]));
  1090. av_assert0(f->slice_count == f->max_slice_count);
  1091. for (j = 0; j < f->slice_count; j++) {
  1092. FFV1Context *fs = f->slice_context[j];
  1093. for (i = 0; i < 256; i++) {
  1094. f->rc_stat[i][0] += fs->rc_stat[i][0];
  1095. f->rc_stat[i][1] += fs->rc_stat[i][1];
  1096. }
  1097. for (i = 0; i < f->quant_table_count; i++) {
  1098. for (k = 0; k < f->context_count[i]; k++)
  1099. for (m = 0; m < 32; m++) {
  1100. f->rc_stat2[i][k][m][0] += fs->rc_stat2[i][k][m][0];
  1101. f->rc_stat2[i][k][m][1] += fs->rc_stat2[i][k][m][1];
  1102. }
  1103. }
  1104. }
  1105. for (j = 0; j < 256; j++) {
  1106. snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
  1107. f->rc_stat[j][0], f->rc_stat[j][1]);
  1108. p += strlen(p);
  1109. }
  1110. snprintf(p, end - p, "\n");
  1111. for (i = 0; i < f->quant_table_count; i++) {
  1112. for (j = 0; j < f->context_count[i]; j++)
  1113. for (m = 0; m < 32; m++) {
  1114. snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
  1115. f->rc_stat2[i][j][m][0], f->rc_stat2[i][j][m][1]);
  1116. p += strlen(p);
  1117. }
  1118. }
  1119. snprintf(p, end - p, "%d\n", f->gob_count);
  1120. }
  1121. return 0;
  1122. }
  1123. if (f->version > 3)
  1124. maxsize = AV_INPUT_BUFFER_MIN_SIZE + avctx->width*avctx->height*3LL*4;
  1125. if ((ret = ff_alloc_packet2(avctx, pkt, maxsize, 0)) < 0)
  1126. return ret;
  1127. ff_init_range_encoder(c, pkt->data, pkt->size);
  1128. ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
  1129. av_frame_unref(p);
  1130. if ((ret = av_frame_ref(p, pict)) < 0)
  1131. return ret;
  1132. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  1133. if (avctx->gop_size == 0 || f->picture_number % avctx->gop_size == 0) {
  1134. put_rac(c, &keystate, 1);
  1135. f->key_frame = 1;
  1136. f->gob_count++;
  1137. write_header(f);
  1138. } else {
  1139. put_rac(c, &keystate, 0);
  1140. f->key_frame = 0;
  1141. }
  1142. if (f->ac > 1) {
  1143. int i;
  1144. for (i = 1; i < 256; i++) {
  1145. c->one_state[i] = f->state_transition[i];
  1146. c->zero_state[256 - i] = 256 - c->one_state[i];
  1147. }
  1148. }
  1149. for (i = 1; i < f->slice_count; i++) {
  1150. FFV1Context *fs = f->slice_context[i];
  1151. uint8_t *start = pkt->data + (pkt->size - used_count) * (int64_t)i / f->slice_count;
  1152. int len = pkt->size / f->slice_count;
  1153. ff_init_range_encoder(&fs->c, start, len);
  1154. }
  1155. avctx->execute(avctx, encode_slice, &f->slice_context[0], NULL,
  1156. f->slice_count, sizeof(void *));
  1157. buf_p = pkt->data;
  1158. for (i = 0; i < f->slice_count; i++) {
  1159. FFV1Context *fs = f->slice_context[i];
  1160. int bytes;
  1161. if (fs->ac) {
  1162. uint8_t state = 129;
  1163. put_rac(&fs->c, &state, 0);
  1164. bytes = ff_rac_terminate(&fs->c);
  1165. } else {
  1166. flush_put_bits(&fs->pb); // FIXME: nicer padding
  1167. bytes = fs->ac_byte_count + (put_bits_count(&fs->pb) + 7) / 8;
  1168. }
  1169. if (i > 0 || f->version > 2) {
  1170. av_assert0(bytes < pkt->size / f->slice_count);
  1171. memmove(buf_p, fs->c.bytestream_start, bytes);
  1172. av_assert0(bytes < (1 << 24));
  1173. AV_WB24(buf_p + bytes, bytes);
  1174. bytes += 3;
  1175. }
  1176. if (f->ec) {
  1177. unsigned v;
  1178. buf_p[bytes++] = 0;
  1179. v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, bytes);
  1180. AV_WL32(buf_p + bytes, v);
  1181. bytes += 4;
  1182. }
  1183. buf_p += bytes;
  1184. }
  1185. if (avctx->flags & AV_CODEC_FLAG_PASS1)
  1186. avctx->stats_out[0] = '\0';
  1187. #if FF_API_CODED_FRAME
  1188. FF_DISABLE_DEPRECATION_WARNINGS
  1189. avctx->coded_frame->key_frame = f->key_frame;
  1190. FF_ENABLE_DEPRECATION_WARNINGS
  1191. #endif
  1192. f->picture_number++;
  1193. pkt->size = buf_p - pkt->data;
  1194. pkt->pts =
  1195. pkt->dts = pict->pts;
  1196. pkt->flags |= AV_PKT_FLAG_KEY * f->key_frame;
  1197. *got_packet = 1;
  1198. return 0;
  1199. }
  1200. static av_cold int encode_close(AVCodecContext *avctx)
  1201. {
  1202. ff_ffv1_close(avctx);
  1203. return 0;
  1204. }
  1205. #define OFFSET(x) offsetof(FFV1Context, x)
  1206. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  1207. static const AVOption options[] = {
  1208. { "slicecrc", "Protect slices with CRCs", OFFSET(ec), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE },
  1209. { NULL }
  1210. };
  1211. static const AVClass ffv1_class = {
  1212. .class_name = "ffv1 encoder",
  1213. .item_name = av_default_item_name,
  1214. .option = options,
  1215. .version = LIBAVUTIL_VERSION_INT,
  1216. };
  1217. static const AVCodecDefault ffv1_defaults[] = {
  1218. { "coder", "-1" },
  1219. { NULL },
  1220. };
  1221. AVCodec ff_ffv1_encoder = {
  1222. .name = "ffv1",
  1223. .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  1224. .type = AVMEDIA_TYPE_VIDEO,
  1225. .id = AV_CODEC_ID_FFV1,
  1226. .priv_data_size = sizeof(FFV1Context),
  1227. .init = encode_init,
  1228. .encode2 = encode_frame,
  1229. .close = encode_close,
  1230. .capabilities = AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_DELAY,
  1231. .pix_fmts = (const enum AVPixelFormat[]) {
  1232. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV444P,
  1233. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV411P,
  1234. AV_PIX_FMT_YUV410P, AV_PIX_FMT_0RGB32, AV_PIX_FMT_RGB32, AV_PIX_FMT_YUV420P16,
  1235. AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV422P9,
  1236. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  1237. AV_PIX_FMT_YUVA444P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA420P16,
  1238. AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10,
  1239. AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA420P9,
  1240. AV_PIX_FMT_GRAY16, AV_PIX_FMT_GRAY8, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  1241. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14,
  1242. AV_PIX_FMT_NONE
  1243. },
  1244. .defaults = ffv1_defaults,
  1245. .priv_class = &ffv1_class,
  1246. };