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.

1129 lines
38KB

  1. /*
  2. * FFV1 encoder for libavcodec
  3. *
  4. * Copyright (c) 2003-2012 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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/pixdesc.h"
  29. #include "libavutil/crc.h"
  30. #include "libavutil/opt.h"
  31. #include "libavutil/imgutils.h"
  32. #include "avcodec.h"
  33. #include "golomb.h"
  34. #include "internal.h"
  35. #include "put_bits.h"
  36. #include "rangecoder.h"
  37. #include "mathops.h"
  38. #include "ffv1.h"
  39. static void find_best_state(uint8_t best_state[256][256],
  40. const uint8_t one_state[256])
  41. {
  42. int i, j, k, m;
  43. double l2tab[256];
  44. for (i = 1; i < 256; i++)
  45. l2tab[i] = log2(i / 256.0);
  46. for (i = 0; i < 256; i++) {
  47. double best_len[256];
  48. double p = i / 256.0;
  49. for (j = 0; j < 256; j++)
  50. best_len[j] = 1 << 30;
  51. for (j = FFMAX(i - 10, 1); j < FFMIN(i + 11, 256); j++) {
  52. double occ[256] = { 0 };
  53. double len = 0;
  54. occ[j] = 1.0;
  55. for (k = 0; k < 256; k++) {
  56. double newocc[256] = { 0 };
  57. for (m = 1; m < 256; m++)
  58. if (occ[m]) {
  59. len -= occ[m] * (p * l2tab[m] +
  60. (1 - p) * l2tab[256 - m]);
  61. }
  62. if (len < best_len[k]) {
  63. best_len[k] = len;
  64. best_state[i][k] = j;
  65. }
  66. for (m = 1; m < 256; m++)
  67. if (occ[m]) {
  68. newocc[one_state[m]] += occ[m] * p;
  69. newocc[256 - one_state[256 - m]] += occ[m] * (1 - p);
  70. }
  71. memcpy(occ, newocc, sizeof(occ));
  72. }
  73. }
  74. }
  75. }
  76. static av_always_inline av_flatten void put_symbol_inline(RangeCoder *c,
  77. uint8_t *state, int v,
  78. int is_signed,
  79. uint64_t rc_stat[256][2],
  80. uint64_t rc_stat2[32][2])
  81. {
  82. int i;
  83. #define put_rac(C, S, B) \
  84. do { \
  85. if (rc_stat) { \
  86. rc_stat[*(S)][B]++; \
  87. rc_stat2[(S) - state][B]++; \
  88. } \
  89. put_rac(C, S, B); \
  90. } while (0)
  91. if (v) {
  92. const int a = FFABS(v);
  93. const int e = av_log2(a);
  94. put_rac(c, state + 0, 0);
  95. if (e <= 9) {
  96. for (i = 0; i < e; i++)
  97. put_rac(c, state + 1 + i, 1); // 1..10
  98. put_rac(c, state + 1 + i, 0);
  99. for (i = e - 1; i >= 0; i--)
  100. put_rac(c, state + 22 + i, (a >> i) & 1); // 22..31
  101. if (is_signed)
  102. put_rac(c, state + 11 + e, v < 0); // 11..21
  103. } else {
  104. for (i = 0; i < e; i++)
  105. put_rac(c, state + 1 + FFMIN(i, 9), 1); // 1..10
  106. put_rac(c, state + 1 + 9, 0);
  107. for (i = e - 1; i >= 0; i--)
  108. put_rac(c, state + 22 + FFMIN(i, 9), (a >> i) & 1); // 22..31
  109. if (is_signed)
  110. put_rac(c, state + 11 + 10, v < 0); // 11..21
  111. }
  112. } else {
  113. put_rac(c, state + 0, 1);
  114. }
  115. #undef put_rac
  116. }
  117. static av_noinline void put_symbol(RangeCoder *c, uint8_t *state,
  118. int v, int is_signed)
  119. {
  120. put_symbol_inline(c, state, v, is_signed, NULL, NULL);
  121. }
  122. static inline void put_vlc_symbol(PutBitContext *pb, VlcState *const state,
  123. int v, int bits)
  124. {
  125. int i, k, code;
  126. v = fold(v - state->bias, bits);
  127. i = state->count;
  128. k = 0;
  129. while (i < state->error_sum) { // FIXME: optimize
  130. k++;
  131. i += i;
  132. }
  133. assert(k <= 13);
  134. code = v ^ ((2 * state->drift + state->count) >> 31);
  135. ff_dlog(NULL, "v:%d/%d bias:%d error:%d drift:%d count:%d k:%d\n", v, code,
  136. state->bias, state->error_sum, state->drift, state->count, k);
  137. set_sr_golomb(pb, code, k, 12, bits);
  138. update_vlc_state(state, v);
  139. }
  140. static av_always_inline int encode_line(FFV1Context *s, int w,
  141. int16_t *sample[3],
  142. int plane_index, int bits)
  143. {
  144. PlaneContext *const p = &s->plane[plane_index];
  145. RangeCoder *const c = &s->c;
  146. int x;
  147. int run_index = s->run_index;
  148. int run_count = 0;
  149. int run_mode = 0;
  150. if (s->ac != AC_GOLOMB_RICE) {
  151. if (c->bytestream_end - c->bytestream < w * 20) {
  152. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  153. return AVERROR_INVALIDDATA;
  154. }
  155. } else {
  156. if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < w * 4) {
  157. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  158. return AVERROR_INVALIDDATA;
  159. }
  160. }
  161. for (x = 0; x < w; x++) {
  162. int diff, context;
  163. context = get_context(p, sample[0] + x, sample[1] + x, sample[2] + x);
  164. diff = sample[0][x] - predict(sample[0] + x, sample[1] + x);
  165. if (context < 0) {
  166. context = -context;
  167. diff = -diff;
  168. }
  169. diff = fold(diff, bits);
  170. if (s->ac != AC_GOLOMB_RICE) {
  171. if (s->flags & AV_CODEC_FLAG_PASS1) {
  172. put_symbol_inline(c, p->state[context], diff, 1, s->rc_stat,
  173. s->rc_stat2[p->quant_table_index][context]);
  174. } else {
  175. put_symbol_inline(c, p->state[context], diff, 1, NULL, NULL);
  176. }
  177. } else {
  178. if (context == 0)
  179. run_mode = 1;
  180. if (run_mode) {
  181. if (diff) {
  182. while (run_count >= 1 << ff_log2_run[run_index]) {
  183. run_count -= 1 << ff_log2_run[run_index];
  184. run_index++;
  185. put_bits(&s->pb, 1, 1);
  186. }
  187. put_bits(&s->pb, 1 + ff_log2_run[run_index], run_count);
  188. if (run_index)
  189. run_index--;
  190. run_count = 0;
  191. run_mode = 0;
  192. if (diff > 0)
  193. diff--;
  194. } else {
  195. run_count++;
  196. }
  197. }
  198. ff_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
  199. run_count, run_index, run_mode, x,
  200. (int)put_bits_count(&s->pb));
  201. if (run_mode == 0)
  202. put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
  203. }
  204. }
  205. if (run_mode) {
  206. while (run_count >= 1 << ff_log2_run[run_index]) {
  207. run_count -= 1 << ff_log2_run[run_index];
  208. run_index++;
  209. put_bits(&s->pb, 1, 1);
  210. }
  211. if (run_count)
  212. put_bits(&s->pb, 1, 1);
  213. }
  214. s->run_index = run_index;
  215. return 0;
  216. }
  217. static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h,
  218. int stride, int plane_index)
  219. {
  220. int x, y, i;
  221. const int ring_size = s->context_model ? 3 : 2;
  222. int16_t *sample[3];
  223. s->run_index = 0;
  224. memset(s->sample_buffer, 0, ring_size * (w + 6) * sizeof(*s->sample_buffer));
  225. for (y = 0; y < h; y++) {
  226. for (i = 0; i < ring_size; i++)
  227. sample[i] = s->sample_buffer + (w + 6) * ((h + i - y) % ring_size) + 3;
  228. sample[0][-1] = sample[1][0];
  229. sample[1][w] = sample[1][w - 1];
  230. // { START_TIMER
  231. if (s->bits_per_raw_sample <= 8) {
  232. for (x = 0; x < w; x++)
  233. sample[0][x] = src[x + stride * y];
  234. encode_line(s, w, sample, plane_index, 8);
  235. } else {
  236. if (s->packed_at_lsb) {
  237. for (x = 0; x < w; x++)
  238. sample[0][x] = ((uint16_t *)(src + stride * y))[x];
  239. } else {
  240. for (x = 0; x < w; x++)
  241. sample[0][x] =
  242. ((uint16_t *)(src + stride * y))[x] >> (16 - s->bits_per_raw_sample);
  243. }
  244. encode_line(s, w, sample, plane_index, s->bits_per_raw_sample);
  245. }
  246. // STOP_TIMER("encode line") }
  247. }
  248. }
  249. static void encode_rgb_frame(FFV1Context *s, const uint8_t *src[3],
  250. int w, int h, const int stride[3])
  251. {
  252. int x, y, p, i;
  253. const int ring_size = s->context_model ? 3 : 2;
  254. int16_t *sample[MAX_PLANES][3];
  255. int lbd = s->avctx->bits_per_raw_sample <= 8;
  256. int bits = s->avctx->bits_per_raw_sample > 0
  257. ? s->avctx->bits_per_raw_sample
  258. : 8;
  259. int offset = 1 << bits;
  260. s->run_index = 0;
  261. memset(s->sample_buffer, 0, ring_size * MAX_PLANES *
  262. (w + 6) * sizeof(*s->sample_buffer));
  263. for (y = 0; y < h; y++) {
  264. for (i = 0; i < ring_size; i++)
  265. for (p = 0; p < MAX_PLANES; p++)
  266. sample[p][i] = s->sample_buffer + p * ring_size *
  267. (w + 6) +
  268. ((h + i - y) % ring_size) * (w + 6) + 3;
  269. for (x = 0; x < w; x++) {
  270. int b, g, r, av_uninit(a);
  271. if (lbd) {
  272. unsigned v = *((const uint32_t *)(src[0] + x * 4 + stride[0] * y));
  273. b = v & 0xFF;
  274. g = (v >> 8) & 0xFF;
  275. r = (v >> 16) & 0xFF;
  276. a = v >> 24;
  277. } else {
  278. b = *((const uint16_t *)(src[0] + x * 2 + stride[0] * y));
  279. g = *((const uint16_t *)(src[1] + x * 2 + stride[1] * y));
  280. r = *((const uint16_t *)(src[2] + x * 2 + stride[2] * y));
  281. }
  282. b -= g;
  283. r -= g;
  284. g += (b + r) >> 2;
  285. b += offset;
  286. r += offset;
  287. sample[0][0][x] = g;
  288. sample[1][0][x] = b;
  289. sample[2][0][x] = r;
  290. sample[3][0][x] = a;
  291. }
  292. for (p = 0; p < 3 + s->transparency; p++) {
  293. sample[p][0][-1] = sample[p][1][0];
  294. sample[p][1][w] = sample[p][1][w - 1];
  295. if (lbd)
  296. encode_line(s, w, sample[p], (p + 1) / 2, 9);
  297. else
  298. encode_line(s, w, sample[p], (p + 1) / 2, bits + 1);
  299. }
  300. }
  301. }
  302. static void write_quant_table(RangeCoder *c, int16_t *quant_table)
  303. {
  304. int last = 0;
  305. int i;
  306. uint8_t state[CONTEXT_SIZE];
  307. memset(state, 128, sizeof(state));
  308. for (i = 1; i < 128; i++)
  309. if (quant_table[i] != quant_table[i - 1]) {
  310. put_symbol(c, state, i - last - 1, 0);
  311. last = i;
  312. }
  313. put_symbol(c, state, i - last - 1, 0);
  314. }
  315. static void write_quant_tables(RangeCoder *c,
  316. int16_t quant_table[MAX_CONTEXT_INPUTS][256])
  317. {
  318. int i;
  319. for (i = 0; i < 5; i++)
  320. write_quant_table(c, quant_table[i]);
  321. }
  322. static void write_header(FFV1Context *f)
  323. {
  324. uint8_t state[CONTEXT_SIZE];
  325. int i;
  326. RangeCoder *const c = &f->slice_context[0]->c;
  327. memset(state, 128, sizeof(state));
  328. if (f->version < 2) {
  329. put_symbol(c, state, f->version, 0);
  330. put_symbol(c, state, f->ac, 0);
  331. if (f->ac == AC_RANGE_CUSTOM_TAB) {
  332. for (i = 1; i < 256; i++)
  333. put_symbol(c, state,
  334. f->state_transition[i] - c->one_state[i], 1);
  335. }
  336. put_symbol(c, state, f->colorspace, 0); // YUV cs type
  337. if (f->version > 0)
  338. put_symbol(c, state, f->bits_per_raw_sample, 0);
  339. put_rac(c, state, f->chroma_planes);
  340. put_symbol(c, state, f->chroma_h_shift, 0);
  341. put_symbol(c, state, f->chroma_v_shift, 0);
  342. put_rac(c, state, f->transparency);
  343. write_quant_tables(c, f->quant_table);
  344. }
  345. }
  346. static int write_extradata(FFV1Context *f)
  347. {
  348. RangeCoder *const c = &f->c;
  349. uint8_t state[CONTEXT_SIZE];
  350. int i, j, k;
  351. uint8_t state2[32][CONTEXT_SIZE];
  352. unsigned v;
  353. memset(state2, 128, sizeof(state2));
  354. memset(state, 128, sizeof(state));
  355. f->avctx->extradata_size = 10000 + 4 +
  356. (11 * 11 * 5 * 5 * 5 + 11 * 11 * 11) * 32;
  357. f->avctx->extradata = av_malloc(f->avctx->extradata_size);
  358. ff_init_range_encoder(c, f->avctx->extradata, f->avctx->extradata_size);
  359. ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
  360. put_symbol(c, state, f->version, 0);
  361. if (f->version > 1) {
  362. if (f->version == 3)
  363. f->minor_version = 2;
  364. put_symbol(c, state, f->minor_version, 0);
  365. }
  366. put_symbol(c, state, f->ac, 0);
  367. if (f->ac == AC_RANGE_CUSTOM_TAB)
  368. for (i = 1; i < 256; i++)
  369. put_symbol(c, state, f->state_transition[i] - c->one_state[i], 1);
  370. put_symbol(c, state, f->colorspace, 0); // YUV cs type
  371. put_symbol(c, state, f->bits_per_raw_sample, 0);
  372. put_rac(c, state, f->chroma_planes);
  373. put_symbol(c, state, f->chroma_h_shift, 0);
  374. put_symbol(c, state, f->chroma_v_shift, 0);
  375. put_rac(c, state, f->transparency);
  376. put_symbol(c, state, f->num_h_slices - 1, 0);
  377. put_symbol(c, state, f->num_v_slices - 1, 0);
  378. put_symbol(c, state, f->quant_table_count, 0);
  379. for (i = 0; i < f->quant_table_count; i++)
  380. write_quant_tables(c, f->quant_tables[i]);
  381. for (i = 0; i < f->quant_table_count; i++) {
  382. for (j = 0; j < f->context_count[i] * CONTEXT_SIZE; j++)
  383. if (f->initial_states[i] && f->initial_states[i][0][j] != 128)
  384. break;
  385. if (j < f->context_count[i] * CONTEXT_SIZE) {
  386. put_rac(c, state, 1);
  387. for (j = 0; j < f->context_count[i]; j++)
  388. for (k = 0; k < CONTEXT_SIZE; k++) {
  389. int pred = j ? f->initial_states[i][j - 1][k] : 128;
  390. put_symbol(c, state2[k],
  391. (int8_t)(f->initial_states[i][j][k] - pred), 1);
  392. }
  393. } else {
  394. put_rac(c, state, 0);
  395. }
  396. }
  397. if (f->version > 2) {
  398. put_symbol(c, state, f->ec, 0);
  399. }
  400. f->avctx->extradata_size = ff_rac_terminate(c);
  401. v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0,
  402. f->avctx->extradata, f->avctx->extradata_size);
  403. AV_WL32(f->avctx->extradata + f->avctx->extradata_size, v);
  404. f->avctx->extradata_size += 4;
  405. return 0;
  406. }
  407. static int sort_stt(FFV1Context *s, uint8_t stt[256])
  408. {
  409. int i, i2, changed, print = 0;
  410. do {
  411. changed = 0;
  412. for (i = 12; i < 244; i++) {
  413. for (i2 = i + 1; i2 < 245 && i2 < i + 4; i2++) {
  414. #define COST(old, new) \
  415. s->rc_stat[old][0] * -log2((256 - (new)) / 256.0) + \
  416. s->rc_stat[old][1] * -log2((new) / 256.0)
  417. #define COST2(old, new) \
  418. COST(old, new) + COST(256 - (old), 256 - (new))
  419. double size0 = COST2(i, i) + COST2(i2, i2);
  420. double sizeX = COST2(i, i2) + COST2(i2, i);
  421. if (sizeX < size0 && i != 128 && i2 != 128) {
  422. int j;
  423. FFSWAP(int, stt[i], stt[i2]);
  424. FFSWAP(int, s->rc_stat[i][0], s->rc_stat[i2][0]);
  425. FFSWAP(int, s->rc_stat[i][1], s->rc_stat[i2][1]);
  426. if (i != 256 - i2) {
  427. FFSWAP(int, stt[256 - i], stt[256 - i2]);
  428. FFSWAP(int, s->rc_stat[256 - i][0], s->rc_stat[256 - i2][0]);
  429. FFSWAP(int, s->rc_stat[256 - i][1], s->rc_stat[256 - i2][1]);
  430. }
  431. for (j = 1; j < 256; j++) {
  432. if (stt[j] == i)
  433. stt[j] = i2;
  434. else if (stt[j] == i2)
  435. stt[j] = i;
  436. if (i != 256 - i2) {
  437. if (stt[256 - j] == 256 - i)
  438. stt[256 - j] = 256 - i2;
  439. else if (stt[256 - j] == 256 - i2)
  440. stt[256 - j] = 256 - i;
  441. }
  442. }
  443. print = changed = 1;
  444. }
  445. }
  446. }
  447. } while (changed);
  448. return print;
  449. }
  450. static av_cold int init_slices_state(FFV1Context *f)
  451. {
  452. int i, ret;
  453. for (i = 0; i < f->slice_count; i++) {
  454. FFV1Context *fs = f->slice_context[i];
  455. if ((ret = ffv1_init_slice_state(f, fs)) < 0)
  456. return AVERROR(ENOMEM);
  457. }
  458. return 0;
  459. }
  460. static av_cold int ffv1_encode_init(AVCodecContext *avctx)
  461. {
  462. FFV1Context *s = avctx->priv_data;
  463. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  464. int i, j, k, m, ret;
  465. ffv1_common_init(avctx);
  466. s->version = 0;
  467. switch (avctx->level) {
  468. case 3:
  469. break;
  470. case 2:
  471. av_log(avctx, AV_LOG_ERROR,
  472. "Version 2 had been deemed non-standard and deprecated "
  473. "the support for it had been removed\n");
  474. return AVERROR(ENOSYS);
  475. case 1:
  476. case 0:
  477. if (avctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) {
  478. av_log(avctx, AV_LOG_ERROR,
  479. "Multiple pass encoding requires version 3.\n");
  480. return AVERROR(ENOSYS);
  481. }
  482. if (avctx->slices > 1) {
  483. av_log(avctx, AV_LOG_ERROR,
  484. "Multiple slices support requires version 3.\n");
  485. return AVERROR(ENOSYS);
  486. }
  487. break;
  488. case FF_LEVEL_UNKNOWN:
  489. if ((avctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) ||
  490. avctx->slices > 1)
  491. s->version = 3;
  492. else
  493. s->version = 0;
  494. break;
  495. default:
  496. av_log(avctx, AV_LOG_ERROR, "Version %d not supported\n",
  497. avctx->level);
  498. return AVERROR(ENOSYS);
  499. }
  500. if (s->ec < 0) {
  501. s->ec = (s->version >= 3);
  502. }
  503. #if FF_API_CODER_TYPE
  504. FF_DISABLE_DEPRECATION_WARNINGS
  505. if (avctx->coder_type != -1)
  506. s->ac = avctx->coder_type > 0 ? AC_RANGE_CUSTOM_TAB : AC_GOLOMB_RICE;
  507. FF_ENABLE_DEPRECATION_WARNINGS
  508. #endif
  509. s->plane_count = 3;
  510. switch (avctx->pix_fmt) {
  511. case AV_PIX_FMT_YUV444P9:
  512. case AV_PIX_FMT_YUV422P9:
  513. case AV_PIX_FMT_YUV420P9:
  514. if (!avctx->bits_per_raw_sample)
  515. s->bits_per_raw_sample = 9;
  516. case AV_PIX_FMT_YUV444P10:
  517. case AV_PIX_FMT_YUV420P10:
  518. case AV_PIX_FMT_YUV422P10:
  519. s->packed_at_lsb = 1;
  520. if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
  521. s->bits_per_raw_sample = 10;
  522. case AV_PIX_FMT_GRAY16:
  523. case AV_PIX_FMT_YUV444P16:
  524. case AV_PIX_FMT_YUV422P16:
  525. case AV_PIX_FMT_YUV420P16:
  526. if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample) {
  527. s->bits_per_raw_sample = 16;
  528. } else if (!s->bits_per_raw_sample) {
  529. s->bits_per_raw_sample = avctx->bits_per_raw_sample;
  530. }
  531. if (s->bits_per_raw_sample <= 8) {
  532. av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
  533. return AVERROR_INVALIDDATA;
  534. }
  535. if (s->ac == AC_GOLOMB_RICE) {
  536. av_log(avctx, AV_LOG_INFO,
  537. "bits_per_raw_sample > 8, forcing range coder\n");
  538. s->ac = AC_RANGE_CUSTOM_TAB;
  539. }
  540. s->version = FFMAX(s->version, 1);
  541. case AV_PIX_FMT_GRAY8:
  542. case AV_PIX_FMT_YUV444P:
  543. case AV_PIX_FMT_YUV440P:
  544. case AV_PIX_FMT_YUV422P:
  545. case AV_PIX_FMT_YUV420P:
  546. case AV_PIX_FMT_YUV411P:
  547. case AV_PIX_FMT_YUV410P:
  548. s->chroma_planes = desc->nb_components < 3 ? 0 : 1;
  549. s->colorspace = 0;
  550. break;
  551. case AV_PIX_FMT_YUVA444P:
  552. case AV_PIX_FMT_YUVA422P:
  553. case AV_PIX_FMT_YUVA420P:
  554. s->chroma_planes = 1;
  555. s->colorspace = 0;
  556. s->transparency = 1;
  557. break;
  558. case AV_PIX_FMT_RGB32:
  559. s->colorspace = 1;
  560. s->transparency = 1;
  561. break;
  562. case AV_PIX_FMT_GBRP9:
  563. if (!avctx->bits_per_raw_sample)
  564. s->bits_per_raw_sample = 9;
  565. case AV_PIX_FMT_GBRP10:
  566. if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
  567. s->bits_per_raw_sample = 10;
  568. case AV_PIX_FMT_GBRP16:
  569. if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
  570. s->bits_per_raw_sample = 16;
  571. else if (!s->bits_per_raw_sample)
  572. s->bits_per_raw_sample = avctx->bits_per_raw_sample;
  573. s->colorspace = 1;
  574. s->chroma_planes = 1;
  575. s->version = FFMAX(s->version, 1);
  576. break;
  577. default:
  578. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  579. return AVERROR_INVALIDDATA;
  580. }
  581. if (s->transparency) {
  582. av_log(
  583. avctx, AV_LOG_WARNING,
  584. "Storing alpha plane, this will require a recent FFV1 decoder to playback!\n");
  585. }
  586. #if FF_API_PRIVATE_OPT
  587. FF_DISABLE_DEPRECATION_WARNINGS
  588. if (avctx->context_model)
  589. s->context_model = avctx->context_model;
  590. if (avctx->context_model > 1U) {
  591. av_log(avctx, AV_LOG_ERROR,
  592. "Invalid context model %d, valid values are 0 and 1\n",
  593. avctx->context_model);
  594. return AVERROR(EINVAL);
  595. }
  596. FF_ENABLE_DEPRECATION_WARNINGS
  597. #endif
  598. if (s->ac == AC_RANGE_CUSTOM_TAB)
  599. for (i = 1; i < 256; i++)
  600. s->state_transition[i] = ffv1_ver2_state[i];
  601. for (i = 0; i < 256; i++) {
  602. s->quant_table_count = 2;
  603. if (s->bits_per_raw_sample <= 8) {
  604. s->quant_tables[0][0][i] = ffv1_quant11[i];
  605. s->quant_tables[0][1][i] = ffv1_quant11[i] * 11;
  606. s->quant_tables[0][2][i] = ffv1_quant11[i] * 11 * 11;
  607. s->quant_tables[1][0][i] = ffv1_quant11[i];
  608. s->quant_tables[1][1][i] = ffv1_quant11[i] * 11;
  609. s->quant_tables[1][2][i] = ffv1_quant5[i] * 11 * 11;
  610. s->quant_tables[1][3][i] = ffv1_quant5[i] * 5 * 11 * 11;
  611. s->quant_tables[1][4][i] = ffv1_quant5[i] * 5 * 5 * 11 * 11;
  612. } else {
  613. s->quant_tables[0][0][i] = ffv1_quant9_10bit[i];
  614. s->quant_tables[0][1][i] = ffv1_quant9_10bit[i] * 11;
  615. s->quant_tables[0][2][i] = ffv1_quant9_10bit[i] * 11 * 11;
  616. s->quant_tables[1][0][i] = ffv1_quant9_10bit[i];
  617. s->quant_tables[1][1][i] = ffv1_quant9_10bit[i] * 11;
  618. s->quant_tables[1][2][i] = ffv1_quant5_10bit[i] * 11 * 11;
  619. s->quant_tables[1][3][i] = ffv1_quant5_10bit[i] * 5 * 11 * 11;
  620. s->quant_tables[1][4][i] = ffv1_quant5_10bit[i] * 5 * 5 * 11 * 11;
  621. }
  622. }
  623. s->context_count[0] = (11 * 11 * 11 + 1) / 2;
  624. s->context_count[1] = (11 * 11 * 5 * 5 * 5 + 1) / 2;
  625. memcpy(s->quant_table, s->quant_tables[s->context_model],
  626. sizeof(s->quant_table));
  627. for (i = 0; i < s->plane_count; i++) {
  628. PlaneContext *const p = &s->plane[i];
  629. memcpy(p->quant_table, s->quant_table, sizeof(p->quant_table));
  630. p->quant_table_index = s->context_model;
  631. p->context_count = s->context_count[p->quant_table_index];
  632. }
  633. if ((ret = ffv1_allocate_initial_states(s)) < 0)
  634. return ret;
  635. #if FF_API_CODED_FRAME
  636. FF_DISABLE_DEPRECATION_WARNINGS
  637. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  638. FF_ENABLE_DEPRECATION_WARNINGS
  639. #endif
  640. if (!s->transparency)
  641. s->plane_count = 2;
  642. av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift,
  643. &s->chroma_v_shift);
  644. s->picture_number = 0;
  645. if (avctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) {
  646. for (i = 0; i < s->quant_table_count; i++) {
  647. s->rc_stat2[i] = av_mallocz(s->context_count[i] *
  648. sizeof(*s->rc_stat2[i]));
  649. if (!s->rc_stat2[i])
  650. return AVERROR(ENOMEM);
  651. }
  652. }
  653. if (avctx->stats_in) {
  654. char *p = avctx->stats_in;
  655. uint8_t best_state[256][256];
  656. int gob_count = 0;
  657. char *next;
  658. av_assert0(s->version > 2);
  659. for (;; ) {
  660. for (j = 0; j < 256; j++)
  661. for (i = 0; i < 2; i++) {
  662. s->rc_stat[j][i] = strtol(p, &next, 0);
  663. if (next == p) {
  664. av_log(avctx, AV_LOG_ERROR,
  665. "2Pass file invalid at %d %d [%s]\n", j, i, p);
  666. return AVERROR_INVALIDDATA;
  667. }
  668. p = next;
  669. }
  670. for (i = 0; i < s->quant_table_count; i++)
  671. for (j = 0; j < s->context_count[i]; j++) {
  672. for (k = 0; k < 32; k++)
  673. for (m = 0; m < 2; m++) {
  674. s->rc_stat2[i][j][k][m] = strtol(p, &next, 0);
  675. if (next == p) {
  676. av_log(avctx, AV_LOG_ERROR,
  677. "2Pass file invalid at %d %d %d %d [%s]\n",
  678. i, j, k, m, p);
  679. return AVERROR_INVALIDDATA;
  680. }
  681. p = next;
  682. }
  683. }
  684. gob_count = strtol(p, &next, 0);
  685. if (next == p || gob_count <= 0) {
  686. av_log(avctx, AV_LOG_ERROR, "2Pass file invalid\n");
  687. return AVERROR_INVALIDDATA;
  688. }
  689. p = next;
  690. while (*p == '\n' || *p == ' ')
  691. p++;
  692. if (p[0] == 0)
  693. break;
  694. }
  695. sort_stt(s, s->state_transition);
  696. find_best_state(best_state, s->state_transition);
  697. for (i = 0; i < s->quant_table_count; i++) {
  698. for (j = 0; j < s->context_count[i]; j++)
  699. for (k = 0; k < 32; k++) {
  700. double p = 128;
  701. if (s->rc_stat2[i][j][k][0] + s->rc_stat2[i][j][k][1]) {
  702. p = 256.0 * s->rc_stat2[i][j][k][1] /
  703. (s->rc_stat2[i][j][k][0] + s->rc_stat2[i][j][k][1]);
  704. }
  705. s->initial_states[i][j][k] =
  706. best_state[av_clip(round(p), 1, 255)][av_clip((s->rc_stat2[i][j][k][0] +
  707. s->rc_stat2[i][j][k][1]) /
  708. gob_count, 0, 255)];
  709. }
  710. }
  711. }
  712. if (s->version > 1) {
  713. for (s->num_v_slices = 2; s->num_v_slices < 9; s->num_v_slices++)
  714. for (s->num_h_slices = s->num_v_slices;
  715. s->num_h_slices < 2 * s->num_v_slices; s->num_h_slices++)
  716. if (avctx->slices == s->num_h_slices * s->num_v_slices &&
  717. avctx->slices <= 64 || !avctx->slices)
  718. goto slices_ok;
  719. av_log(avctx, AV_LOG_ERROR,
  720. "Unsupported number %d of slices requested, please specify a "
  721. "supported number with -slices (ex:4,6,9,12,16, ...)\n",
  722. avctx->slices);
  723. return AVERROR(ENOSYS);
  724. slices_ok:
  725. write_extradata(s);
  726. }
  727. if ((ret = ffv1_init_slice_contexts(s)) < 0)
  728. return ret;
  729. if ((ret = init_slices_state(s)) < 0)
  730. return ret;
  731. #define STATS_OUT_SIZE 1024 * 1024 * 6
  732. if (avctx->flags & AV_CODEC_FLAG_PASS1) {
  733. avctx->stats_out = av_mallocz(STATS_OUT_SIZE);
  734. for (i = 0; i < s->quant_table_count; i++)
  735. for (j = 0; j < s->slice_count; j++) {
  736. FFV1Context *sf = s->slice_context[j];
  737. av_assert0(!sf->rc_stat2[i]);
  738. sf->rc_stat2[i] = av_mallocz(s->context_count[i] *
  739. sizeof(*sf->rc_stat2[i]));
  740. if (!sf->rc_stat2[i])
  741. return AVERROR(ENOMEM);
  742. }
  743. }
  744. return 0;
  745. }
  746. static void encode_slice_header(FFV1Context *f, FFV1Context *fs)
  747. {
  748. RangeCoder *c = &fs->c;
  749. uint8_t state[CONTEXT_SIZE];
  750. int j;
  751. memset(state, 128, sizeof(state));
  752. put_symbol(c, state, (fs->slice_x + 1) * f->num_h_slices / f->width, 0);
  753. put_symbol(c, state, (fs->slice_y + 1) * f->num_v_slices / f->height, 0);
  754. put_symbol(c, state, (fs->slice_width + 1) * f->num_h_slices / f->width - 1,
  755. 0);
  756. put_symbol(c, state,
  757. (fs->slice_height + 1) * f->num_v_slices / f->height - 1,
  758. 0);
  759. for (j = 0; j < f->plane_count; j++) {
  760. put_symbol(c, state, f->plane[j].quant_table_index, 0);
  761. av_assert0(f->plane[j].quant_table_index == f->context_model);
  762. }
  763. if (!f->frame->interlaced_frame)
  764. put_symbol(c, state, 3, 0);
  765. else
  766. put_symbol(c, state, 1 + !f->frame->top_field_first, 0);
  767. put_symbol(c, state, f->frame->sample_aspect_ratio.num, 0);
  768. put_symbol(c, state, f->frame->sample_aspect_ratio.den, 0);
  769. }
  770. static int encode_slice(AVCodecContext *c, void *arg)
  771. {
  772. FFV1Context *fs = *(void **)arg;
  773. FFV1Context *f = fs->avctx->priv_data;
  774. int width = fs->slice_width;
  775. int height = fs->slice_height;
  776. int x = fs->slice_x;
  777. int y = fs->slice_y;
  778. const AVFrame *const p = f->frame;
  779. const int ps = (av_pix_fmt_desc_get(c->pix_fmt)->flags & AV_PIX_FMT_FLAG_PLANAR)
  780. ? (f->bits_per_raw_sample > 8) + 1
  781. : 4;
  782. if (f->key_frame)
  783. ffv1_clear_slice_state(f, fs);
  784. if (f->version > 2) {
  785. encode_slice_header(f, fs);
  786. }
  787. if (fs->ac == AC_GOLOMB_RICE) {
  788. if (f->version > 2)
  789. put_rac(&fs->c, (uint8_t[]) { 129 }, 0);
  790. fs->ac_byte_count = f->version > 2 || (!x && !y) ? ff_rac_terminate( &fs->c) : 0;
  791. init_put_bits(&fs->pb, fs->c.bytestream_start + fs->ac_byte_count,
  792. fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count);
  793. }
  794. if (f->colorspace == 0) {
  795. const int chroma_width = AV_CEIL_RSHIFT(width, f->chroma_h_shift);
  796. const int chroma_height = AV_CEIL_RSHIFT(height, f->chroma_v_shift);
  797. const int cx = x >> f->chroma_h_shift;
  798. const int cy = y >> f->chroma_v_shift;
  799. encode_plane(fs, p->data[0] + ps * x + y * p->linesize[0],
  800. width, height, p->linesize[0], 0);
  801. if (f->chroma_planes) {
  802. encode_plane(fs, p->data[1] + ps * cx + cy * p->linesize[1],
  803. chroma_width, chroma_height, p->linesize[1], 1);
  804. encode_plane(fs, p->data[2] + ps * cx + cy * p->linesize[2],
  805. chroma_width, chroma_height, p->linesize[2], 1);
  806. }
  807. if (fs->transparency)
  808. encode_plane(fs, p->data[3] + ps * x + y * p->linesize[3], width,
  809. height, p->linesize[3], 2);
  810. } else {
  811. const uint8_t *planes[3] = { p->data[0] + ps * x + y * p->linesize[0],
  812. p->data[1] + ps * x + y * p->linesize[1],
  813. p->data[2] + ps * x + y * p->linesize[2] };
  814. encode_rgb_frame(fs, planes, width, height, p->linesize);
  815. }
  816. emms_c();
  817. return 0;
  818. }
  819. static int ffv1_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  820. const AVFrame *pict, int *got_packet)
  821. {
  822. FFV1Context *f = avctx->priv_data;
  823. RangeCoder *const c = &f->slice_context[0]->c;
  824. int used_count = 0;
  825. uint8_t keystate = 128;
  826. uint8_t *buf_p;
  827. int i, ret;
  828. f->frame = pict;
  829. if ((ret = ff_alloc_packet(pkt, avctx->width * avctx->height *
  830. ((8 * 2 + 1 + 1) * 4) / 8 +
  831. AV_INPUT_BUFFER_MIN_SIZE)) < 0) {
  832. av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
  833. return ret;
  834. }
  835. ff_init_range_encoder(c, pkt->data, pkt->size);
  836. ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
  837. if (avctx->gop_size == 0 || f->picture_number % avctx->gop_size == 0) {
  838. put_rac(c, &keystate, 1);
  839. f->key_frame = 1;
  840. f->gob_count++;
  841. write_header(f);
  842. } else {
  843. put_rac(c, &keystate, 0);
  844. f->key_frame = 0;
  845. }
  846. if (f->ac == AC_RANGE_CUSTOM_TAB) {
  847. int i;
  848. for (i = 1; i < 256; i++) {
  849. c->one_state[i] = f->state_transition[i];
  850. c->zero_state[256 - i] = 256 - c->one_state[i];
  851. }
  852. }
  853. for (i = 1; i < f->slice_count; i++) {
  854. FFV1Context *fs = f->slice_context[i];
  855. uint8_t *start = pkt->data +
  856. (pkt->size - used_count) * (int64_t)i / f->slice_count;
  857. int len = pkt->size / f->slice_count;
  858. ff_init_range_encoder(&fs->c, start, len);
  859. }
  860. avctx->execute(avctx, encode_slice, &f->slice_context[0], NULL,
  861. f->slice_count, sizeof(void *));
  862. buf_p = pkt->data;
  863. for (i = 0; i < f->slice_count; i++) {
  864. FFV1Context *fs = f->slice_context[i];
  865. int bytes;
  866. if (fs->ac != AC_GOLOMB_RICE) {
  867. uint8_t state = 129;
  868. put_rac(&fs->c, &state, 0);
  869. bytes = ff_rac_terminate(&fs->c);
  870. } else {
  871. flush_put_bits(&fs->pb); // FIXME: nicer padding
  872. bytes = fs->ac_byte_count + (put_bits_count(&fs->pb) + 7) / 8;
  873. }
  874. if (i > 0 || f->version > 2) {
  875. av_assert0(bytes < pkt->size / f->slice_count);
  876. memmove(buf_p, fs->c.bytestream_start, bytes);
  877. av_assert0(bytes < (1 << 24));
  878. AV_WB24(buf_p + bytes, bytes);
  879. bytes += 3;
  880. }
  881. if (f->ec) {
  882. unsigned v;
  883. buf_p[bytes++] = 0;
  884. v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, bytes);
  885. AV_WL32(buf_p + bytes, v);
  886. bytes += 4;
  887. }
  888. buf_p += bytes;
  889. }
  890. if ((avctx->flags & AV_CODEC_FLAG_PASS1) && (f->picture_number & 31) == 0) {
  891. int j, k, m;
  892. char *p = avctx->stats_out;
  893. char *end = p + STATS_OUT_SIZE;
  894. memset(f->rc_stat, 0, sizeof(f->rc_stat));
  895. for (i = 0; i < f->quant_table_count; i++)
  896. memset(f->rc_stat2[i], 0, f->context_count[i] * sizeof(*f->rc_stat2[i]));
  897. for (j = 0; j < f->slice_count; j++) {
  898. FFV1Context *fs = f->slice_context[j];
  899. for (i = 0; i < 256; i++) {
  900. f->rc_stat[i][0] += fs->rc_stat[i][0];
  901. f->rc_stat[i][1] += fs->rc_stat[i][1];
  902. }
  903. for (i = 0; i < f->quant_table_count; i++) {
  904. for (k = 0; k < f->context_count[i]; k++)
  905. for (m = 0; m < 32; m++) {
  906. f->rc_stat2[i][k][m][0] += fs->rc_stat2[i][k][m][0];
  907. f->rc_stat2[i][k][m][1] += fs->rc_stat2[i][k][m][1];
  908. }
  909. }
  910. }
  911. for (j = 0; j < 256; j++) {
  912. snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
  913. f->rc_stat[j][0], f->rc_stat[j][1]);
  914. p += strlen(p);
  915. }
  916. snprintf(p, end - p, "\n");
  917. for (i = 0; i < f->quant_table_count; i++) {
  918. for (j = 0; j < f->context_count[i]; j++)
  919. for (m = 0; m < 32; m++) {
  920. snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
  921. f->rc_stat2[i][j][m][0], f->rc_stat2[i][j][m][1]);
  922. p += strlen(p);
  923. }
  924. }
  925. snprintf(p, end - p, "%d\n", f->gob_count);
  926. } else if (avctx->flags & AV_CODEC_FLAG_PASS1)
  927. avctx->stats_out[0] = '\0';
  928. #if FF_API_CODED_FRAME
  929. FF_DISABLE_DEPRECATION_WARNINGS
  930. avctx->coded_frame->key_frame = f->key_frame;
  931. FF_ENABLE_DEPRECATION_WARNINGS
  932. #endif
  933. f->picture_number++;
  934. pkt->size = buf_p - pkt->data;
  935. pkt->flags |= AV_PKT_FLAG_KEY * f->key_frame;
  936. *got_packet = 1;
  937. return 0;
  938. }
  939. static av_cold int ffv1_encode_close(AVCodecContext *avctx)
  940. {
  941. ffv1_close(avctx);
  942. return 0;
  943. }
  944. #define OFFSET(x) offsetof(FFV1Context, x)
  945. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  946. static const AVOption options[] = {
  947. { "slicecrc", "Protect slices with CRCs", OFFSET(ec), AV_OPT_TYPE_INT,
  948. { .i64 = -1 }, -1, 1, VE },
  949. { "coder", "Coder type", OFFSET(ac), AV_OPT_TYPE_INT,
  950. { .i64 = AC_GOLOMB_RICE }, 0, 2, VE, "coder" },
  951. { "rice", "Golomb rice", 0, AV_OPT_TYPE_CONST,
  952. { .i64 = AC_GOLOMB_RICE }, INT_MIN, INT_MAX, VE, "coder" },
  953. { "range_def", "Range with default table", 0, AV_OPT_TYPE_CONST,
  954. { .i64 = AC_RANGE_DEFAULT_TAB }, INT_MIN, INT_MAX, VE, "coder" },
  955. { "range_tab", "Range with custom table", 0, AV_OPT_TYPE_CONST,
  956. { .i64 = AC_RANGE_CUSTOM_TAB }, INT_MIN, INT_MAX, VE, "coder" },
  957. { "context", "Context model", OFFSET(context_model), AV_OPT_TYPE_INT,
  958. { .i64 = 0 }, 0, 1, VE },
  959. { NULL }
  960. };
  961. static const AVClass class = {
  962. .class_name = "ffv1 encoder",
  963. .item_name = av_default_item_name,
  964. .option = options,
  965. .version = LIBAVUTIL_VERSION_INT,
  966. };
  967. #if FF_API_CODER_TYPE
  968. static const AVCodecDefault ffv1_defaults[] = {
  969. { "coder", "-1" },
  970. { NULL },
  971. };
  972. #endif
  973. AVCodec ff_ffv1_encoder = {
  974. .name = "ffv1",
  975. .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  976. .type = AVMEDIA_TYPE_VIDEO,
  977. .id = AV_CODEC_ID_FFV1,
  978. .priv_data_size = sizeof(FFV1Context),
  979. .init = ffv1_encode_init,
  980. .encode2 = ffv1_encode_frame,
  981. .close = ffv1_encode_close,
  982. .capabilities = AV_CODEC_CAP_SLICE_THREADS,
  983. .pix_fmts = (const enum AVPixelFormat[]) {
  984. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
  985. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  986. AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV420P9,
  987. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  988. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  989. AV_PIX_FMT_RGB32,
  990. AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  991. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  992. AV_PIX_FMT_GRAY16, AV_PIX_FMT_GRAY8,
  993. AV_PIX_FMT_NONE
  994. },
  995. #if FF_API_CODER_TYPE
  996. .defaults = ffv1_defaults,
  997. #endif
  998. .priv_class = &class,
  999. };