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.

1309 lines
48KB

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