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.

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