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.

1138 lines
39KB

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