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.

1386 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 & 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 + FF_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 = ffv1_common_init(avctx)) < 0)
  594. return ret;
  595. s->version = 0;
  596. if ((avctx->flags & (CODEC_FLAG_PASS1|CODEC_FLAG_PASS2)) || avctx->slices>1)
  597. s->version = FFMAX(s->version, 2);
  598. // Unspecified level & slices, we choose version 1.2+ to ensure multithreaded decodability
  599. if (avctx->slices == 0 && avctx->level < 0 && avctx->width * avctx->height > 720*576)
  600. s->version = FFMAX(s->version, 2);
  601. if (avctx->level <= 0 && s->version == 2) {
  602. s->version = 3;
  603. }
  604. if (avctx->level >= 0 && avctx->level <= 4)
  605. s->version = FFMAX(s->version, avctx->level);
  606. if (s->ec < 0) {
  607. s->ec = (s->version >= 3);
  608. }
  609. if ((s->version == 2 || s->version>3) && avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  610. av_log(avctx, AV_LOG_ERROR, "Version 2 needed for requested features but version 2 is experimental and not enabled\n");
  611. return AVERROR_INVALIDDATA;
  612. }
  613. s->ac = avctx->coder_type > 0 ? 2 : 0;
  614. s->plane_count = 3;
  615. switch(avctx->pix_fmt) {
  616. case AV_PIX_FMT_YUV444P9:
  617. case AV_PIX_FMT_YUV422P9:
  618. case AV_PIX_FMT_YUV420P9:
  619. case AV_PIX_FMT_YUVA444P9:
  620. case AV_PIX_FMT_YUVA422P9:
  621. case AV_PIX_FMT_YUVA420P9:
  622. if (!avctx->bits_per_raw_sample)
  623. s->bits_per_raw_sample = 9;
  624. case AV_PIX_FMT_YUV444P10:
  625. case AV_PIX_FMT_YUV420P10:
  626. case AV_PIX_FMT_YUV422P10:
  627. case AV_PIX_FMT_YUVA444P10:
  628. case AV_PIX_FMT_YUVA422P10:
  629. case AV_PIX_FMT_YUVA420P10:
  630. s->packed_at_lsb = 1;
  631. if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
  632. s->bits_per_raw_sample = 10;
  633. case AV_PIX_FMT_GRAY16:
  634. case AV_PIX_FMT_YUV444P16:
  635. case AV_PIX_FMT_YUV422P16:
  636. case AV_PIX_FMT_YUV420P16:
  637. case AV_PIX_FMT_YUVA444P16:
  638. case AV_PIX_FMT_YUVA422P16:
  639. case AV_PIX_FMT_YUVA420P16:
  640. if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample) {
  641. s->bits_per_raw_sample = 16;
  642. } else if (!s->bits_per_raw_sample) {
  643. s->bits_per_raw_sample = avctx->bits_per_raw_sample;
  644. }
  645. if (s->bits_per_raw_sample <= 8) {
  646. av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
  647. return AVERROR_INVALIDDATA;
  648. }
  649. if (!s->ac && avctx->coder_type == -1) {
  650. av_log(avctx, AV_LOG_INFO, "bits_per_raw_sample > 8, forcing coder 1\n");
  651. s->ac = 2;
  652. }
  653. if (!s->ac) {
  654. av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample of more than 8 needs -coder 1 currently\n");
  655. return AVERROR(ENOSYS);
  656. }
  657. s->version = FFMAX(s->version, 1);
  658. case AV_PIX_FMT_GRAY8:
  659. case AV_PIX_FMT_YUV444P:
  660. case AV_PIX_FMT_YUV440P:
  661. case AV_PIX_FMT_YUV422P:
  662. case AV_PIX_FMT_YUV420P:
  663. case AV_PIX_FMT_YUV411P:
  664. case AV_PIX_FMT_YUV410P:
  665. case AV_PIX_FMT_YUVA444P:
  666. case AV_PIX_FMT_YUVA422P:
  667. case AV_PIX_FMT_YUVA420P:
  668. s->chroma_planes = desc->nb_components < 3 ? 0 : 1;
  669. s->colorspace = 0;
  670. s->transparency = desc->nb_components == 4;
  671. if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
  672. s->bits_per_raw_sample = 8;
  673. else if (!s->bits_per_raw_sample)
  674. s->bits_per_raw_sample = 8;
  675. break;
  676. case AV_PIX_FMT_RGB32:
  677. s->colorspace = 1;
  678. s->transparency = 1;
  679. s->chroma_planes = 1;
  680. if (!avctx->bits_per_raw_sample)
  681. s->bits_per_raw_sample = 8;
  682. break;
  683. case AV_PIX_FMT_0RGB32:
  684. s->colorspace = 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_GBRP9:
  690. if (!avctx->bits_per_raw_sample)
  691. s->bits_per_raw_sample = 9;
  692. case AV_PIX_FMT_GBRP10:
  693. if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
  694. s->bits_per_raw_sample = 10;
  695. case AV_PIX_FMT_GBRP12:
  696. if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
  697. s->bits_per_raw_sample = 12;
  698. case AV_PIX_FMT_GBRP14:
  699. if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
  700. s->bits_per_raw_sample = 14;
  701. else if (!s->bits_per_raw_sample)
  702. s->bits_per_raw_sample = avctx->bits_per_raw_sample;
  703. s->colorspace = 1;
  704. s->chroma_planes = 1;
  705. s->version = FFMAX(s->version, 1);
  706. if (!s->ac && avctx->coder_type == -1) {
  707. av_log(avctx, AV_LOG_INFO, "bits_per_raw_sample > 8, forcing coder 1\n");
  708. s->ac = 2;
  709. }
  710. if (!s->ac) {
  711. av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample of more than 8 needs -coder 1 currently\n");
  712. return AVERROR(ENOSYS);
  713. }
  714. break;
  715. default:
  716. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  717. return AVERROR(ENOSYS);
  718. }
  719. av_assert0(s->bits_per_raw_sample >= 8);
  720. if (s->transparency) {
  721. av_log(avctx, AV_LOG_WARNING, "Storing alpha plane, this will require a recent FFV1 decoder to playback!\n");
  722. }
  723. if (avctx->context_model > 1U) {
  724. av_log(avctx, AV_LOG_ERROR, "Invalid context model %d, valid values are 0 and 1\n", avctx->context_model);
  725. return AVERROR(EINVAL);
  726. }
  727. if (s->ac > 1)
  728. for (i = 1; i < 256; i++)
  729. s->state_transition[i] = ver2_state[i];
  730. for (i = 0; i < 256; i++) {
  731. s->quant_table_count = 2;
  732. if (s->bits_per_raw_sample <= 8) {
  733. s->quant_tables[0][0][i]= quant11[i];
  734. s->quant_tables[0][1][i]= 11*quant11[i];
  735. s->quant_tables[0][2][i]= 11*11*quant11[i];
  736. s->quant_tables[1][0][i]= quant11[i];
  737. s->quant_tables[1][1][i]= 11*quant11[i];
  738. s->quant_tables[1][2][i]= 11*11*quant5 [i];
  739. s->quant_tables[1][3][i]= 5*11*11*quant5 [i];
  740. s->quant_tables[1][4][i]= 5*5*11*11*quant5 [i];
  741. } else {
  742. s->quant_tables[0][0][i]= quant9_10bit[i];
  743. s->quant_tables[0][1][i]= 11*quant9_10bit[i];
  744. s->quant_tables[0][2][i]= 11*11*quant9_10bit[i];
  745. s->quant_tables[1][0][i]= quant9_10bit[i];
  746. s->quant_tables[1][1][i]= 11*quant9_10bit[i];
  747. s->quant_tables[1][2][i]= 11*11*quant5_10bit[i];
  748. s->quant_tables[1][3][i]= 5*11*11*quant5_10bit[i];
  749. s->quant_tables[1][4][i]= 5*5*11*11*quant5_10bit[i];
  750. }
  751. }
  752. s->context_count[0] = (11 * 11 * 11 + 1) / 2;
  753. s->context_count[1] = (11 * 11 * 5 * 5 * 5 + 1) / 2;
  754. memcpy(s->quant_table, s->quant_tables[avctx->context_model],
  755. sizeof(s->quant_table));
  756. for (i = 0; i < s->plane_count; i++) {
  757. PlaneContext *const p = &s->plane[i];
  758. memcpy(p->quant_table, s->quant_table, sizeof(p->quant_table));
  759. p->quant_table_index = avctx->context_model;
  760. p->context_count = s->context_count[p->quant_table_index];
  761. }
  762. if ((ret = ffv1_allocate_initial_states(s)) < 0)
  763. return ret;
  764. avctx->coded_frame = av_frame_alloc();
  765. if (!avctx->coded_frame)
  766. return AVERROR(ENOMEM);
  767. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  768. if (!s->transparency)
  769. s->plane_count = 2;
  770. if (!s->chroma_planes && s->version > 3)
  771. s->plane_count--;
  772. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  773. s->picture_number = 0;
  774. if (avctx->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2)) {
  775. for (i = 0; i < s->quant_table_count; i++) {
  776. s->rc_stat2[i] = av_mallocz(s->context_count[i] *
  777. sizeof(*s->rc_stat2[i]));
  778. if (!s->rc_stat2[i])
  779. return AVERROR(ENOMEM);
  780. }
  781. }
  782. if (avctx->stats_in) {
  783. char *p = avctx->stats_in;
  784. uint8_t (*best_state)[256] = av_malloc_array(256, 256);
  785. int gob_count = 0;
  786. char *next;
  787. if (!best_state)
  788. return AVERROR(ENOMEM);
  789. av_assert0(s->version >= 2);
  790. for (;;) {
  791. for (j = 0; j < 256; j++)
  792. for (i = 0; i < 2; i++) {
  793. s->rc_stat[j][i] = strtol(p, &next, 0);
  794. if (next == p) {
  795. av_log(avctx, AV_LOG_ERROR,
  796. "2Pass file invalid at %d %d [%s]\n", j, i, p);
  797. av_freep(&best_state);
  798. return AVERROR_INVALIDDATA;
  799. }
  800. p = next;
  801. }
  802. for (i = 0; i < s->quant_table_count; i++)
  803. for (j = 0; j < s->context_count[i]; j++) {
  804. for (k = 0; k < 32; k++)
  805. for (m = 0; m < 2; m++) {
  806. s->rc_stat2[i][j][k][m] = strtol(p, &next, 0);
  807. if (next == p) {
  808. av_log(avctx, AV_LOG_ERROR,
  809. "2Pass file invalid at %d %d %d %d [%s]\n",
  810. i, j, k, m, p);
  811. av_freep(&best_state);
  812. return AVERROR_INVALIDDATA;
  813. }
  814. p = next;
  815. }
  816. }
  817. gob_count = strtol(p, &next, 0);
  818. if (next == p || gob_count <= 0) {
  819. av_log(avctx, AV_LOG_ERROR, "2Pass file invalid\n");
  820. av_freep(&best_state);
  821. return AVERROR_INVALIDDATA;
  822. }
  823. p = next;
  824. while (*p == '\n' || *p == ' ')
  825. p++;
  826. if (p[0] == 0)
  827. break;
  828. }
  829. sort_stt(s, s->state_transition);
  830. find_best_state(best_state, s->state_transition);
  831. for (i = 0; i < s->quant_table_count; i++) {
  832. for (k = 0; k < 32; k++) {
  833. double a=0, b=0;
  834. int jp = 0;
  835. for (j = 0; j < s->context_count[i]; j++) {
  836. double p = 128;
  837. if (s->rc_stat2[i][j][k][0] + s->rc_stat2[i][j][k][1] > 200 && j || a+b > 200) {
  838. if (a+b)
  839. p = 256.0 * b / (a + b);
  840. s->initial_states[i][jp][k] =
  841. best_state[av_clip(round(p), 1, 255)][av_clip_uint8((a + b) / gob_count)];
  842. for(jp++; jp<j; jp++)
  843. s->initial_states[i][jp][k] = s->initial_states[i][jp-1][k];
  844. a=b=0;
  845. }
  846. a += s->rc_stat2[i][j][k][0];
  847. b += s->rc_stat2[i][j][k][1];
  848. if (a+b) {
  849. p = 256.0 * b / (a + b);
  850. }
  851. s->initial_states[i][j][k] =
  852. best_state[av_clip(round(p), 1, 255)][av_clip_uint8((a + b) / gob_count)];
  853. }
  854. }
  855. }
  856. av_freep(&best_state);
  857. }
  858. if (s->version > 1) {
  859. s->num_v_slices = (avctx->width > 352 || avctx->height > 288 || !avctx->slices) ? 2 : 1;
  860. for (; s->num_v_slices < 9; s->num_v_slices++) {
  861. for (s->num_h_slices = s->num_v_slices; s->num_h_slices < 2*s->num_v_slices; s->num_h_slices++) {
  862. if (avctx->slices == s->num_h_slices * s->num_v_slices && avctx->slices <= 64 || !avctx->slices)
  863. goto slices_ok;
  864. }
  865. }
  866. av_log(avctx, AV_LOG_ERROR,
  867. "Unsupported number %d of slices requested, please specify a "
  868. "supported number with -slices (ex:4,6,9,12,16, ...)\n",
  869. avctx->slices);
  870. return AVERROR(ENOSYS);
  871. slices_ok:
  872. if ((ret = write_extradata(s)) < 0)
  873. return ret;
  874. }
  875. if ((ret = ffv1_init_slice_contexts(s)) < 0)
  876. return ret;
  877. if ((ret = ffv1_init_slices_state(s)) < 0)
  878. return ret;
  879. #define STATS_OUT_SIZE 1024 * 1024 * 6
  880. if (avctx->flags & CODEC_FLAG_PASS1) {
  881. avctx->stats_out = av_mallocz(STATS_OUT_SIZE);
  882. if (!avctx->stats_out)
  883. return AVERROR(ENOMEM);
  884. for (i = 0; i < s->quant_table_count; i++)
  885. for (j = 0; j < s->slice_count; j++) {
  886. FFV1Context *sf = s->slice_context[j];
  887. av_assert0(!sf->rc_stat2[i]);
  888. sf->rc_stat2[i] = av_mallocz(s->context_count[i] *
  889. sizeof(*sf->rc_stat2[i]));
  890. if (!sf->rc_stat2[i])
  891. return AVERROR(ENOMEM);
  892. }
  893. }
  894. return 0;
  895. }
  896. static void encode_slice_header(FFV1Context *f, FFV1Context *fs)
  897. {
  898. RangeCoder *c = &fs->c;
  899. uint8_t state[CONTEXT_SIZE];
  900. int j;
  901. memset(state, 128, sizeof(state));
  902. put_symbol(c, state, (fs->slice_x +1)*f->num_h_slices / f->width , 0);
  903. put_symbol(c, state, (fs->slice_y +1)*f->num_v_slices / f->height , 0);
  904. put_symbol(c, state, (fs->slice_width +1)*f->num_h_slices / f->width -1, 0);
  905. put_symbol(c, state, (fs->slice_height+1)*f->num_v_slices / f->height-1, 0);
  906. for (j=0; j<f->plane_count; j++) {
  907. put_symbol(c, state, f->plane[j].quant_table_index, 0);
  908. av_assert0(f->plane[j].quant_table_index == f->avctx->context_model);
  909. }
  910. if (!f->picture.f->interlaced_frame)
  911. put_symbol(c, state, 3, 0);
  912. else
  913. put_symbol(c, state, 1 + !f->picture.f->top_field_first, 0);
  914. put_symbol(c, state, f->picture.f->sample_aspect_ratio.num, 0);
  915. put_symbol(c, state, f->picture.f->sample_aspect_ratio.den, 0);
  916. if (f->version > 3) {
  917. put_rac(c, state, fs->slice_coding_mode == 1);
  918. if (fs->slice_coding_mode == 1)
  919. ffv1_clear_slice_state(f, fs);
  920. put_symbol(c, state, fs->slice_coding_mode, 0);
  921. if (fs->slice_coding_mode != 1) {
  922. put_symbol(c, state, fs->slice_rct_by_coef, 0);
  923. put_symbol(c, state, fs->slice_rct_ry_coef, 0);
  924. }
  925. }
  926. }
  927. static void choose_rct_params(FFV1Context *fs, const uint8_t *src[3], const int stride[3], int w, int h)
  928. {
  929. #define NB_Y_COEFF 15
  930. static const int rct_y_coeff[15][2] = {
  931. {0, 0}, // 4G
  932. {1, 1}, // R + 2G + B
  933. {2, 2}, // 2R + 2B
  934. {0, 2}, // 2G + 2B
  935. {2, 0}, // 2R + 2G
  936. {4, 0}, // 4R
  937. {0, 4}, // 4B
  938. {0, 3}, // 1G + 3B
  939. {3, 0}, // 3R + 1G
  940. {3, 1}, // 3R + B
  941. {1, 3}, // R + 3B
  942. {1, 2}, // R + G + 2B
  943. {2, 1}, // 2R + G + B
  944. {0, 1}, // 3G + B
  945. {1, 0}, // R + 3G
  946. };
  947. int stat[NB_Y_COEFF] = {0};
  948. int x, y, i, p, best;
  949. int16_t *sample[3];
  950. int lbd = fs->bits_per_raw_sample <= 8;
  951. for (y = 0; y < h; y++) {
  952. int lastr=0, lastg=0, lastb=0;
  953. for (p = 0; p < 3; p++)
  954. sample[p] = fs->sample_buffer + p*w;
  955. for (x = 0; x < w; x++) {
  956. int b, g, r;
  957. int ab, ag, ar;
  958. if (lbd) {
  959. unsigned v = *((const uint32_t*)(src[0] + x*4 + stride[0]*y));
  960. b = v & 0xFF;
  961. g = (v >> 8) & 0xFF;
  962. r = (v >> 16) & 0xFF;
  963. } else {
  964. b = *((const uint16_t*)(src[0] + x*2 + stride[0]*y));
  965. g = *((const uint16_t*)(src[1] + x*2 + stride[1]*y));
  966. r = *((const uint16_t*)(src[2] + x*2 + stride[2]*y));
  967. }
  968. ar = r - lastr;
  969. ag = g - lastg;
  970. ab = b - lastb;
  971. if (x && y) {
  972. int bg = ag - sample[0][x];
  973. int bb = ab - sample[1][x];
  974. int br = ar - sample[2][x];
  975. br -= bg;
  976. bb -= bg;
  977. for (i = 0; i<NB_Y_COEFF; i++) {
  978. stat[i] += FFABS(bg + ((br*rct_y_coeff[i][0] + bb*rct_y_coeff[i][1])>>2));
  979. }
  980. }
  981. sample[0][x] = ag;
  982. sample[1][x] = ab;
  983. sample[2][x] = ar;
  984. lastr = r;
  985. lastg = g;
  986. lastb = b;
  987. }
  988. }
  989. best = 0;
  990. for (i=1; i<NB_Y_COEFF; i++) {
  991. if (stat[i] < stat[best])
  992. best = i;
  993. }
  994. fs->slice_rct_by_coef = rct_y_coeff[best][1];
  995. fs->slice_rct_ry_coef = rct_y_coeff[best][0];
  996. }
  997. static int encode_slice(AVCodecContext *c, void *arg)
  998. {
  999. FFV1Context *fs = *(void **)arg;
  1000. FFV1Context *f = fs->avctx->priv_data;
  1001. int width = fs->slice_width;
  1002. int height = fs->slice_height;
  1003. int x = fs->slice_x;
  1004. int y = fs->slice_y;
  1005. const AVFrame *const p = f->picture.f;
  1006. const int ps = av_pix_fmt_desc_get(c->pix_fmt)->comp[0].step_minus1 + 1;
  1007. int ret;
  1008. RangeCoder c_bak = fs->c;
  1009. const uint8_t *planes[3] = {p->data[0] + ps*x + y*p->linesize[0],
  1010. p->data[1] + ps*x + y*p->linesize[1],
  1011. p->data[2] + ps*x + y*p->linesize[2]};
  1012. fs->slice_coding_mode = 0;
  1013. if (f->version > 3) {
  1014. choose_rct_params(fs, planes, p->linesize, width, height);
  1015. } else {
  1016. fs->slice_rct_by_coef = 1;
  1017. fs->slice_rct_ry_coef = 1;
  1018. }
  1019. retry:
  1020. if (f->key_frame)
  1021. ffv1_clear_slice_state(f, fs);
  1022. if (f->version > 2) {
  1023. encode_slice_header(f, fs);
  1024. }
  1025. if (!fs->ac) {
  1026. if (f->version > 2)
  1027. put_rac(&fs->c, (uint8_t[]) { 129 }, 0);
  1028. fs->ac_byte_count = f->version > 2 || (!x && !y) ? ff_rac_terminate(&fs->c) : 0;
  1029. init_put_bits(&fs->pb,
  1030. fs->c.bytestream_start + fs->ac_byte_count,
  1031. fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count);
  1032. }
  1033. if (f->colorspace == 0) {
  1034. const int chroma_width = FF_CEIL_RSHIFT(width, f->chroma_h_shift);
  1035. const int chroma_height = FF_CEIL_RSHIFT(height, f->chroma_v_shift);
  1036. const int cx = x >> f->chroma_h_shift;
  1037. const int cy = y >> f->chroma_v_shift;
  1038. ret = encode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0);
  1039. if (f->chroma_planes) {
  1040. ret |= encode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
  1041. ret |= encode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1);
  1042. }
  1043. if (fs->transparency)
  1044. ret |= encode_plane(fs, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2);
  1045. } else {
  1046. ret = encode_rgb_frame(fs, planes, width, height, p->linesize);
  1047. }
  1048. emms_c();
  1049. if (ret < 0) {
  1050. av_assert0(fs->slice_coding_mode == 0);
  1051. if (fs->version < 4 || !fs->ac) {
  1052. av_log(c, AV_LOG_ERROR, "Buffer too small\n");
  1053. return ret;
  1054. }
  1055. av_log(c, AV_LOG_DEBUG, "Coding slice as PCM\n");
  1056. fs->slice_coding_mode = 1;
  1057. fs->c = c_bak;
  1058. goto retry;
  1059. }
  1060. return 0;
  1061. }
  1062. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  1063. const AVFrame *pict, int *got_packet)
  1064. {
  1065. FFV1Context *f = avctx->priv_data;
  1066. RangeCoder *const c = &f->slice_context[0]->c;
  1067. AVFrame *const p = f->picture.f;
  1068. int used_count = 0;
  1069. uint8_t keystate = 128;
  1070. uint8_t *buf_p;
  1071. int i, ret;
  1072. int64_t maxsize = FF_MIN_BUFFER_SIZE
  1073. + avctx->width*avctx->height*35LL*4;
  1074. if(!pict) {
  1075. if (avctx->flags & CODEC_FLAG_PASS1) {
  1076. int j, k, m;
  1077. char *p = avctx->stats_out;
  1078. char *end = p + STATS_OUT_SIZE;
  1079. memset(f->rc_stat, 0, sizeof(f->rc_stat));
  1080. for (i = 0; i < f->quant_table_count; i++)
  1081. memset(f->rc_stat2[i], 0, f->context_count[i] * sizeof(*f->rc_stat2[i]));
  1082. for (j = 0; j < f->slice_count; j++) {
  1083. FFV1Context *fs = f->slice_context[j];
  1084. for (i = 0; i < 256; i++) {
  1085. f->rc_stat[i][0] += fs->rc_stat[i][0];
  1086. f->rc_stat[i][1] += fs->rc_stat[i][1];
  1087. }
  1088. for (i = 0; i < f->quant_table_count; i++) {
  1089. for (k = 0; k < f->context_count[i]; k++)
  1090. for (m = 0; m < 32; m++) {
  1091. f->rc_stat2[i][k][m][0] += fs->rc_stat2[i][k][m][0];
  1092. f->rc_stat2[i][k][m][1] += fs->rc_stat2[i][k][m][1];
  1093. }
  1094. }
  1095. }
  1096. for (j = 0; j < 256; j++) {
  1097. snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
  1098. f->rc_stat[j][0], f->rc_stat[j][1]);
  1099. p += strlen(p);
  1100. }
  1101. snprintf(p, end - p, "\n");
  1102. for (i = 0; i < f->quant_table_count; i++) {
  1103. for (j = 0; j < f->context_count[i]; j++)
  1104. for (m = 0; m < 32; m++) {
  1105. snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
  1106. f->rc_stat2[i][j][m][0], f->rc_stat2[i][j][m][1]);
  1107. p += strlen(p);
  1108. }
  1109. }
  1110. snprintf(p, end - p, "%d\n", f->gob_count);
  1111. }
  1112. return 0;
  1113. }
  1114. if (f->version > 3)
  1115. maxsize = FF_MIN_BUFFER_SIZE + avctx->width*avctx->height*3LL*4;
  1116. if ((ret = ff_alloc_packet2(avctx, pkt, maxsize)) < 0)
  1117. return ret;
  1118. ff_init_range_encoder(c, pkt->data, pkt->size);
  1119. ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
  1120. av_frame_unref(p);
  1121. if ((ret = av_frame_ref(p, pict)) < 0)
  1122. return ret;
  1123. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  1124. if (avctx->gop_size == 0 || f->picture_number % avctx->gop_size == 0) {
  1125. put_rac(c, &keystate, 1);
  1126. f->key_frame = 1;
  1127. f->gob_count++;
  1128. write_header(f);
  1129. } else {
  1130. put_rac(c, &keystate, 0);
  1131. f->key_frame = 0;
  1132. }
  1133. if (f->ac > 1) {
  1134. int i;
  1135. for (i = 1; i < 256; i++) {
  1136. c->one_state[i] = f->state_transition[i];
  1137. c->zero_state[256 - i] = 256 - c->one_state[i];
  1138. }
  1139. }
  1140. for (i = 1; i < f->slice_count; i++) {
  1141. FFV1Context *fs = f->slice_context[i];
  1142. uint8_t *start = pkt->data + (pkt->size - used_count) * (int64_t)i / f->slice_count;
  1143. int len = pkt->size / f->slice_count;
  1144. ff_init_range_encoder(&fs->c, start, len);
  1145. }
  1146. avctx->execute(avctx, encode_slice, &f->slice_context[0], NULL,
  1147. f->slice_count, sizeof(void *));
  1148. buf_p = pkt->data;
  1149. for (i = 0; i < f->slice_count; i++) {
  1150. FFV1Context *fs = f->slice_context[i];
  1151. int bytes;
  1152. if (fs->ac) {
  1153. uint8_t state = 129;
  1154. put_rac(&fs->c, &state, 0);
  1155. bytes = ff_rac_terminate(&fs->c);
  1156. } else {
  1157. flush_put_bits(&fs->pb); // FIXME: nicer padding
  1158. bytes = fs->ac_byte_count + (put_bits_count(&fs->pb) + 7) / 8;
  1159. }
  1160. if (i > 0 || f->version > 2) {
  1161. av_assert0(bytes < pkt->size / f->slice_count);
  1162. memmove(buf_p, fs->c.bytestream_start, bytes);
  1163. av_assert0(bytes < (1 << 24));
  1164. AV_WB24(buf_p + bytes, bytes);
  1165. bytes += 3;
  1166. }
  1167. if (f->ec) {
  1168. unsigned v;
  1169. buf_p[bytes++] = 0;
  1170. v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, bytes);
  1171. AV_WL32(buf_p + bytes, v);
  1172. bytes += 4;
  1173. }
  1174. buf_p += bytes;
  1175. }
  1176. if (avctx->flags & CODEC_FLAG_PASS1)
  1177. avctx->stats_out[0] = '\0';
  1178. avctx->coded_frame->key_frame = f->key_frame;
  1179. f->picture_number++;
  1180. pkt->size = buf_p - pkt->data;
  1181. pkt->pts =
  1182. pkt->dts = pict->pts;
  1183. pkt->flags |= AV_PKT_FLAG_KEY * f->key_frame;
  1184. *got_packet = 1;
  1185. return 0;
  1186. }
  1187. static av_cold int encode_close(AVCodecContext *avctx)
  1188. {
  1189. av_frame_free(&avctx->coded_frame);
  1190. ffv1_close(avctx);
  1191. return 0;
  1192. }
  1193. #define OFFSET(x) offsetof(FFV1Context, x)
  1194. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  1195. static const AVOption options[] = {
  1196. { "slicecrc", "Protect slices with CRCs", OFFSET(ec), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE },
  1197. { NULL }
  1198. };
  1199. static const AVClass ffv1_class = {
  1200. .class_name = "ffv1 encoder",
  1201. .item_name = av_default_item_name,
  1202. .option = options,
  1203. .version = LIBAVUTIL_VERSION_INT,
  1204. };
  1205. static const AVCodecDefault ffv1_defaults[] = {
  1206. { "coder", "-1" },
  1207. { NULL },
  1208. };
  1209. AVCodec ff_ffv1_encoder = {
  1210. .name = "ffv1",
  1211. .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  1212. .type = AVMEDIA_TYPE_VIDEO,
  1213. .id = AV_CODEC_ID_FFV1,
  1214. .priv_data_size = sizeof(FFV1Context),
  1215. .init = encode_init,
  1216. .encode2 = encode_frame,
  1217. .close = encode_close,
  1218. .capabilities = CODEC_CAP_SLICE_THREADS | CODEC_CAP_DELAY,
  1219. .pix_fmts = (const enum AVPixelFormat[]) {
  1220. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV444P,
  1221. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV411P,
  1222. AV_PIX_FMT_YUV410P, AV_PIX_FMT_0RGB32, AV_PIX_FMT_RGB32, AV_PIX_FMT_YUV420P16,
  1223. AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV422P9,
  1224. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  1225. AV_PIX_FMT_YUVA444P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA420P16,
  1226. AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10,
  1227. AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA420P9,
  1228. AV_PIX_FMT_GRAY16, AV_PIX_FMT_GRAY8, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  1229. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14,
  1230. AV_PIX_FMT_NONE
  1231. },
  1232. .defaults = ffv1_defaults,
  1233. .priv_class = &ffv1_class,
  1234. };