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.

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