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.

2302 lines
79KB

  1. /*
  2. * FFV1 codec for libavcodec
  3. *
  4. * Copyright (c) 2003-2012 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)
  25. */
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/crc.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/imgutils.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "libavutil/timer.h"
  32. #include "avcodec.h"
  33. #include "internal.h"
  34. #include "get_bits.h"
  35. #include "put_bits.h"
  36. #include "dsputil.h"
  37. #include "rangecoder.h"
  38. #include "golomb.h"
  39. #include "mathops.h"
  40. #ifdef __INTEL_COMPILER
  41. #undef av_flatten
  42. #define av_flatten
  43. #endif
  44. #define MAX_PLANES 4
  45. #define CONTEXT_SIZE 32
  46. #define MAX_QUANT_TABLES 8
  47. #define MAX_CONTEXT_INPUTS 5
  48. extern const uint8_t ff_log2_run[41];
  49. static const int8_t quant5_10bit[256] = {
  50. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
  51. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  52. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  53. 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  54. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  55. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  56. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  57. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  58. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  59. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  60. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  61. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  62. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1,
  63. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  64. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  65. -1, -1, -1, -1, -1, -1, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0,
  66. };
  67. static const int8_t quant5[256] = {
  68. 0, 1, 1, 1, 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, 2, 2, 2,
  75. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  76. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  77. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  78. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  79. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  80. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  81. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  82. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  83. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -1,
  84. };
  85. static const int8_t quant9_10bit[256] = {
  86. 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
  87. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3,
  88. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  89. 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  90. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  91. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  92. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  93. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  94. -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
  95. -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
  96. -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
  97. -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
  98. -4, -4, -4, -4, -4, -4, -4, -4, -4, -3, -3, -3, -3, -3, -3, -3,
  99. -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
  100. -3, -3, -3, -3, -3, -3, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  101. -2, -2, -2, -2, -1, -1, -1, -1, -1, -1, -1, -1, -0, -0, -0, -0,
  102. };
  103. static const int8_t quant11[256] = {
  104. 0, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
  105. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  106. 4, 4, 4, 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, 5, 5,
  109. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  110. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  111. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  112. -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
  113. -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
  114. -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
  115. -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
  116. -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
  117. -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -4, -4,
  118. -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
  119. -4, -4, -4, -4, -4, -3, -3, -3, -3, -3, -3, -3, -2, -2, -2, -1,
  120. };
  121. static const uint8_t ver2_state[256] = {
  122. 0, 10, 10, 10, 10, 16, 16, 16, 28, 16, 16, 29, 42, 49, 20, 49,
  123. 59, 25, 26, 26, 27, 31, 33, 33, 33, 34, 34, 37, 67, 38, 39, 39,
  124. 40, 40, 41, 79, 43, 44, 45, 45, 48, 48, 64, 50, 51, 52, 88, 52,
  125. 53, 74, 55, 57, 58, 58, 74, 60, 101, 61, 62, 84, 66, 66, 68, 69,
  126. 87, 82, 71, 97, 73, 73, 82, 75, 111, 77, 94, 78, 87, 81, 83, 97,
  127. 85, 83, 94, 86, 99, 89, 90, 99, 111, 92, 93, 134, 95, 98, 105, 98,
  128. 105, 110, 102, 108, 102, 118, 103, 106, 106, 113, 109, 112, 114, 112, 116, 125,
  129. 115, 116, 117, 117, 126, 119, 125, 121, 121, 123, 145, 124, 126, 131, 127, 129,
  130. 165, 130, 132, 138, 133, 135, 145, 136, 137, 139, 146, 141, 143, 142, 144, 148,
  131. 147, 155, 151, 149, 151, 150, 152, 157, 153, 154, 156, 168, 158, 162, 161, 160,
  132. 172, 163, 169, 164, 166, 184, 167, 170, 177, 174, 171, 173, 182, 176, 180, 178,
  133. 175, 189, 179, 181, 186, 183, 192, 185, 200, 187, 191, 188, 190, 197, 193, 196,
  134. 197, 194, 195, 196, 198, 202, 199, 201, 210, 203, 207, 204, 205, 206, 208, 214,
  135. 209, 211, 221, 212, 213, 215, 224, 216, 217, 218, 219, 220, 222, 228, 223, 225,
  136. 226, 224, 227, 229, 240, 230, 231, 232, 233, 234, 235, 236, 238, 239, 237, 242,
  137. 241, 243, 242, 244, 245, 246, 247, 248, 249, 250, 251, 252, 252, 253, 254, 255,
  138. };
  139. typedef struct VlcState {
  140. int16_t drift;
  141. uint16_t error_sum;
  142. int8_t bias;
  143. uint8_t count;
  144. } VlcState;
  145. typedef struct PlaneContext {
  146. int16_t quant_table[MAX_CONTEXT_INPUTS][256];
  147. int quant_table_index;
  148. int context_count;
  149. uint8_t (*state)[CONTEXT_SIZE];
  150. VlcState *vlc_state;
  151. uint8_t interlace_bit_state[2];
  152. } PlaneContext;
  153. #define MAX_SLICES 256
  154. typedef struct FFV1Context {
  155. AVClass *class;
  156. AVCodecContext *avctx;
  157. RangeCoder c;
  158. GetBitContext gb;
  159. PutBitContext pb;
  160. uint64_t rc_stat[256][2];
  161. uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2];
  162. int version;
  163. int minor_version;
  164. int width, height;
  165. int chroma_h_shift, chroma_v_shift;
  166. int chroma_planes;
  167. int transparency;
  168. int flags;
  169. int picture_number;
  170. AVFrame picture;
  171. AVFrame last_picture;
  172. int plane_count;
  173. int ac; ///< 1=range coder <-> 0=golomb rice
  174. int ac_byte_count; ///< number of bytes used for AC coding
  175. PlaneContext plane[MAX_PLANES];
  176. int16_t quant_table[MAX_CONTEXT_INPUTS][256];
  177. int16_t quant_tables[MAX_QUANT_TABLES][MAX_CONTEXT_INPUTS][256];
  178. int context_count[MAX_QUANT_TABLES];
  179. uint8_t state_transition[256];
  180. uint8_t (*initial_states[MAX_QUANT_TABLES])[32];
  181. int run_index;
  182. int colorspace;
  183. int16_t *sample_buffer;
  184. int gob_count;
  185. int packed_at_lsb;
  186. int ec;
  187. int slice_damaged;
  188. int key_frame_ok;
  189. int quant_table_count;
  190. DSPContext dsp;
  191. struct FFV1Context *slice_context[MAX_SLICES];
  192. int slice_count;
  193. int num_v_slices;
  194. int num_h_slices;
  195. int slice_width;
  196. int slice_height;
  197. int slice_x;
  198. int slice_y;
  199. int bits_per_raw_sample;
  200. } FFV1Context;
  201. static av_always_inline int fold(int diff, int bits)
  202. {
  203. if (bits == 8)
  204. diff = (int8_t)diff;
  205. else {
  206. diff += 1 << (bits - 1);
  207. diff &= (1 << bits) - 1;
  208. diff -= 1 << (bits - 1);
  209. }
  210. return diff;
  211. }
  212. static inline int predict(int16_t *src, int16_t *last)
  213. {
  214. const int LT = last[-1];
  215. const int T = last[0];
  216. const int L = src[-1];
  217. return mid_pred(L, L + T - LT, T);
  218. }
  219. static inline int get_context(PlaneContext *p, int16_t *src,
  220. int16_t *last, int16_t *last2)
  221. {
  222. const int LT = last[-1];
  223. const int T = last[0];
  224. const int RT = last[1];
  225. const int L = src[-1];
  226. if (p->quant_table[3][127]) {
  227. const int TT = last2[0];
  228. const int LL = src[-2];
  229. return p->quant_table[0][(L - LT) & 0xFF] +
  230. p->quant_table[1][(LT - T) & 0xFF] +
  231. p->quant_table[2][(T - RT) & 0xFF] +
  232. p->quant_table[3][(LL - L) & 0xFF] +
  233. p->quant_table[4][(TT - T) & 0xFF];
  234. } else
  235. return p->quant_table[0][(L - LT) & 0xFF] +
  236. p->quant_table[1][(LT - T) & 0xFF] +
  237. p->quant_table[2][(T - RT) & 0xFF];
  238. }
  239. static void find_best_state(uint8_t best_state[256][256],
  240. const uint8_t one_state[256])
  241. {
  242. int i, j, k, m;
  243. double l2tab[256];
  244. for (i = 1; i < 256; i++)
  245. l2tab[i] = log2(i / 256.0);
  246. for (i = 0; i < 256; i++) {
  247. double best_len[256];
  248. double p = i / 256.0;
  249. for (j = 0; j < 256; j++)
  250. best_len[j] = 1 << 30;
  251. for (j = FFMAX(i - 10, 1); j < FFMIN(i + 11, 256); j++) {
  252. double occ[256] = { 0 };
  253. double len = 0;
  254. occ[j] = 1.0;
  255. for (k = 0; k < 256; k++) {
  256. double newocc[256] = { 0 };
  257. for (m = 1; m < 256; m++)
  258. if (occ[m]) {
  259. len -=occ[m]*( p *l2tab[ m]
  260. + (1-p)*l2tab[256-m]);
  261. }
  262. if (len < best_len[k]) {
  263. best_len[k] = len;
  264. best_state[i][k] = j;
  265. }
  266. for (m = 0; m < 256; m++)
  267. if (occ[m]) {
  268. newocc[ one_state[ m]] += occ[m] * p;
  269. newocc[256 - one_state[256 - m]] += occ[m] * (1 - p);
  270. }
  271. memcpy(occ, newocc, sizeof(occ));
  272. }
  273. }
  274. }
  275. }
  276. static av_always_inline av_flatten void put_symbol_inline(RangeCoder *c,
  277. uint8_t *state, int v,
  278. int is_signed,
  279. uint64_t rc_stat[256][2],
  280. uint64_t rc_stat2[32][2])
  281. {
  282. int i;
  283. #define put_rac(C, S, B) \
  284. do { \
  285. if (rc_stat) { \
  286. rc_stat[*(S)][B]++; \
  287. rc_stat2[(S) - state][B]++; \
  288. } \
  289. put_rac(C, S, B); \
  290. } while (0)
  291. if (v) {
  292. const int a = FFABS(v);
  293. const int e = av_log2(a);
  294. put_rac(c, state + 0, 0);
  295. if (e <= 9) {
  296. for (i = 0; i < e; i++)
  297. put_rac(c, state + 1 + i, 1); // 1..10
  298. put_rac(c, state + 1 + i, 0);
  299. for (i = e - 1; i >= 0; i--)
  300. put_rac(c, state + 22 + i, (a >> i) & 1); // 22..31
  301. if (is_signed)
  302. put_rac(c, state + 11 + e, v < 0); // 11..21
  303. } else {
  304. for (i = 0; i < e; i++)
  305. put_rac(c, state + 1 + FFMIN(i, 9), 1); // 1..10
  306. put_rac(c, state + 1 + 9, 0);
  307. for (i = e - 1; i >= 0; i--)
  308. put_rac(c, state + 22 + FFMIN(i, 9), (a >> i) & 1); // 22..31
  309. if (is_signed)
  310. put_rac(c, state + 11 + 10, v < 0); // 11..21
  311. }
  312. } else {
  313. put_rac(c, state + 0, 1);
  314. }
  315. #undef put_rac
  316. }
  317. static av_noinline void put_symbol(RangeCoder *c, uint8_t *state,
  318. int v, int is_signed)
  319. {
  320. put_symbol_inline(c, state, v, is_signed, NULL, NULL);
  321. }
  322. static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state,
  323. int is_signed)
  324. {
  325. if (get_rac(c, state + 0))
  326. return 0;
  327. else {
  328. int i, e, a;
  329. e = 0;
  330. while (get_rac(c, state + 1 + FFMIN(e, 9))) // 1..10
  331. e++;
  332. a = 1;
  333. for (i = e - 1; i >= 0; i--)
  334. a += a + get_rac(c, state + 22 + FFMIN(i, 9)); // 22..31
  335. e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10))); // 11..21
  336. return (a ^ e) - e;
  337. }
  338. }
  339. static av_noinline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
  340. {
  341. return get_symbol_inline(c, state, is_signed);
  342. }
  343. static inline void update_vlc_state(VlcState *const state, const int v)
  344. {
  345. int drift = state->drift;
  346. int count = state->count;
  347. state->error_sum += FFABS(v);
  348. drift += v;
  349. if (count == 128) { // FIXME: variable
  350. count >>= 1;
  351. drift >>= 1;
  352. state->error_sum >>= 1;
  353. }
  354. count++;
  355. if (drift <= -count) {
  356. if (state->bias > -128)
  357. state->bias--;
  358. drift += count;
  359. if (drift <= -count)
  360. drift = -count + 1;
  361. } else if (drift > 0) {
  362. if (state->bias < 127)
  363. state->bias++;
  364. drift -= count;
  365. if (drift > 0)
  366. drift = 0;
  367. }
  368. state->drift = drift;
  369. state->count = count;
  370. }
  371. static inline void put_vlc_symbol(PutBitContext *pb, VlcState *const state,
  372. int v, int bits)
  373. {
  374. int i, k, code;
  375. v = fold(v - state->bias, bits);
  376. i = state->count;
  377. k = 0;
  378. while (i < state->error_sum) { // FIXME: optimize
  379. k++;
  380. i += i;
  381. }
  382. av_assert2(k<=13);
  383. #if 0 // JPEG LS
  384. if (k == 0 && 2 * state->drift <= -state->count)
  385. code = v ^ (-1);
  386. else
  387. code = v;
  388. #else
  389. code = v ^ ((2 * state->drift + state->count) >> 31);
  390. #endif
  391. av_dlog(NULL, "v:%d/%d bias:%d error:%d drift:%d count:%d k:%d\n", v, code,
  392. state->bias, state->error_sum, state->drift, state->count, k);
  393. set_sr_golomb(pb, code, k, 12, bits);
  394. update_vlc_state(state, v);
  395. }
  396. static inline int get_vlc_symbol(GetBitContext *gb, VlcState *const state,
  397. int bits)
  398. {
  399. int k, i, v, ret;
  400. i = state->count;
  401. k = 0;
  402. while (i < state->error_sum) { // FIXME: optimize
  403. k++;
  404. i += i;
  405. }
  406. assert(k <= 8);
  407. v = get_sr_golomb(gb, k, 12, bits);
  408. av_dlog(NULL, "v:%d bias:%d error:%d drift:%d count:%d k:%d",
  409. v, state->bias, state->error_sum, state->drift, state->count, k);
  410. #if 0 // JPEG LS
  411. if (k == 0 && 2 * state->drift <= -state->count)
  412. v ^= (-1);
  413. #else
  414. v ^= ((2 * state->drift + state->count) >> 31);
  415. #endif
  416. ret = fold(v + state->bias, bits);
  417. update_vlc_state(state, v);
  418. return ret;
  419. }
  420. #if CONFIG_FFV1_ENCODER
  421. static av_always_inline int encode_line(FFV1Context *s, int w,
  422. int16_t *sample[3],
  423. int plane_index, int bits)
  424. {
  425. PlaneContext *const p = &s->plane[plane_index];
  426. RangeCoder *const c = &s->c;
  427. int x;
  428. int run_index = s->run_index;
  429. int run_count = 0;
  430. int run_mode = 0;
  431. if (s->ac) {
  432. if (c->bytestream_end - c->bytestream < w * 20) {
  433. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  434. return -1;
  435. }
  436. } else {
  437. if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < w * 4) {
  438. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  439. return -1;
  440. }
  441. }
  442. for (x = 0; x < w; x++) {
  443. int diff, context;
  444. context = get_context(p, sample[0] + x, sample[1] + x, sample[2] + x);
  445. diff = sample[0][x] - predict(sample[0] + x, sample[1] + x);
  446. if (context < 0) {
  447. context = -context;
  448. diff = -diff;
  449. }
  450. diff = fold(diff, bits);
  451. if (s->ac) {
  452. if (s->flags & CODEC_FLAG_PASS1) {
  453. put_symbol_inline(c, p->state[context], diff, 1, s->rc_stat,
  454. s->rc_stat2[p->quant_table_index][context]);
  455. } else {
  456. put_symbol_inline(c, p->state[context], diff, 1, NULL, NULL);
  457. }
  458. } else {
  459. if (context == 0)
  460. run_mode = 1;
  461. if (run_mode) {
  462. if (diff) {
  463. while (run_count >= 1 << ff_log2_run[run_index]) {
  464. run_count -= 1 << ff_log2_run[run_index];
  465. run_index++;
  466. put_bits(&s->pb, 1, 1);
  467. }
  468. put_bits(&s->pb, 1 + ff_log2_run[run_index], run_count);
  469. if (run_index)
  470. run_index--;
  471. run_count = 0;
  472. run_mode = 0;
  473. if (diff > 0)
  474. diff--;
  475. } else {
  476. run_count++;
  477. }
  478. }
  479. av_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
  480. run_count, run_index, run_mode, x,
  481. (int)put_bits_count(&s->pb));
  482. if (run_mode == 0)
  483. put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
  484. }
  485. }
  486. if (run_mode) {
  487. while (run_count >= 1 << ff_log2_run[run_index]) {
  488. run_count -= 1 << ff_log2_run[run_index];
  489. run_index++;
  490. put_bits(&s->pb, 1, 1);
  491. }
  492. if (run_count)
  493. put_bits(&s->pb, 1, 1);
  494. }
  495. s->run_index = run_index;
  496. return 0;
  497. }
  498. static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h,
  499. int stride, int plane_index)
  500. {
  501. int x, y, i;
  502. const int ring_size = s->avctx->context_model ? 3 : 2;
  503. int16_t *sample[3];
  504. s->run_index = 0;
  505. memset(s->sample_buffer, 0, ring_size * (w + 6) * sizeof(*s->sample_buffer));
  506. for (y = 0; y < h; y++) {
  507. for (i = 0; i < ring_size; i++)
  508. sample[i] = s->sample_buffer + (w + 6) * ((h + i - y) % ring_size) + 3;
  509. sample[0][-1]= sample[1][0 ];
  510. sample[1][ w]= sample[1][w-1];
  511. // { START_TIMER
  512. if (s->bits_per_raw_sample <= 8) {
  513. for (x = 0; x < w; x++)
  514. sample[0][x] = src[x + stride * y];
  515. encode_line(s, w, sample, plane_index, 8);
  516. } else {
  517. if (s->packed_at_lsb) {
  518. for (x = 0; x < w; x++) {
  519. sample[0][x] = ((uint16_t*)(src + stride*y))[x];
  520. }
  521. } else {
  522. for (x = 0; x < w; x++) {
  523. sample[0][x] = ((uint16_t*)(src + stride*y))[x] >> (16 - s->bits_per_raw_sample);
  524. }
  525. }
  526. encode_line(s, w, sample, plane_index, s->bits_per_raw_sample);
  527. }
  528. // STOP_TIMER("encode line") }
  529. }
  530. }
  531. static void encode_rgb_frame(FFV1Context *s, uint8_t *src[3], int w, int h, int stride[3])
  532. {
  533. int x, y, p, i;
  534. const int ring_size = s->avctx->context_model ? 3 : 2;
  535. int16_t *sample[4][3];
  536. int lbd = s->avctx->bits_per_raw_sample <= 8;
  537. int bits = s->avctx->bits_per_raw_sample > 0 ? s->avctx->bits_per_raw_sample : 8;
  538. int offset = 1 << bits;
  539. s->run_index = 0;
  540. memset(s->sample_buffer, 0, ring_size * 4 * (w + 6) * sizeof(*s->sample_buffer));
  541. for (y = 0; y < h; y++) {
  542. for (i = 0; i < ring_size; i++)
  543. for (p = 0; p < 4; p++)
  544. sample[p][i]= s->sample_buffer + p*ring_size*(w+6) + ((h+i-y)%ring_size)*(w+6) + 3;
  545. for (x = 0; x < w; x++) {
  546. int b, g, r, av_uninit(a);
  547. if (lbd) {
  548. unsigned v = *((uint32_t*)(src[0] + x*4 + stride[0]*y));
  549. b = v & 0xFF;
  550. g = (v >> 8) & 0xFF;
  551. r = (v >> 16) & 0xFF;
  552. a = v >> 24;
  553. } else {
  554. b = *((uint16_t*)(src[0] + x*2 + stride[0]*y));
  555. g = *((uint16_t*)(src[1] + x*2 + stride[1]*y));
  556. r = *((uint16_t*)(src[2] + x*2 + stride[2]*y));
  557. }
  558. b -= g;
  559. r -= g;
  560. g += (b + r) >> 2;
  561. b += offset;
  562. r += offset;
  563. sample[0][0][x] = g;
  564. sample[1][0][x] = b;
  565. sample[2][0][x] = r;
  566. sample[3][0][x] = a;
  567. }
  568. for (p = 0; p < 3 + s->transparency; p++) {
  569. sample[p][0][-1] = sample[p][1][0 ];
  570. sample[p][1][ w] = sample[p][1][w-1];
  571. if (lbd)
  572. encode_line(s, w, sample[p], (p+1)/2, 9);
  573. else
  574. encode_line(s, w, sample[p], (p+1)/2, bits+1);
  575. }
  576. }
  577. }
  578. static void write_quant_table(RangeCoder *c, int16_t *quant_table)
  579. {
  580. int last = 0;
  581. int i;
  582. uint8_t state[CONTEXT_SIZE];
  583. memset(state, 128, sizeof(state));
  584. for (i = 1; i < 128; i++)
  585. if (quant_table[i] != quant_table[i - 1]) {
  586. put_symbol(c, state, i - last - 1, 0);
  587. last = i;
  588. }
  589. put_symbol(c, state, i - last - 1, 0);
  590. }
  591. static void write_quant_tables(RangeCoder *c,
  592. int16_t quant_table[MAX_CONTEXT_INPUTS][256])
  593. {
  594. int i;
  595. for (i = 0; i < 5; i++)
  596. write_quant_table(c, quant_table[i]);
  597. }
  598. static void write_header(FFV1Context *f)
  599. {
  600. uint8_t state[CONTEXT_SIZE];
  601. int i, j;
  602. RangeCoder *const c = &f->slice_context[0]->c;
  603. memset(state, 128, sizeof(state));
  604. if (f->version < 2) {
  605. put_symbol(c, state, f->version, 0);
  606. put_symbol(c, state, f->ac, 0);
  607. if (f->ac > 1) {
  608. for (i = 1; i < 256; i++)
  609. put_symbol(c, state,
  610. f->state_transition[i] - c->one_state[i], 1);
  611. }
  612. put_symbol(c, state, f->colorspace, 0); //YUV cs type
  613. if (f->version > 0)
  614. put_symbol(c, state, f->bits_per_raw_sample, 0);
  615. put_rac(c, state, f->chroma_planes);
  616. put_symbol(c, state, f->chroma_h_shift, 0);
  617. put_symbol(c, state, f->chroma_v_shift, 0);
  618. put_rac(c, state, f->transparency);
  619. write_quant_tables(c, f->quant_table);
  620. } else if (f->version < 3) {
  621. put_symbol(c, state, f->slice_count, 0);
  622. for (i = 0; i < f->slice_count; i++) {
  623. FFV1Context *fs = f->slice_context[i];
  624. put_symbol(c, state,
  625. (fs->slice_x + 1) * f->num_h_slices / f->width, 0);
  626. put_symbol(c, state,
  627. (fs->slice_y + 1) * f->num_v_slices / f->height, 0);
  628. put_symbol(c, state,
  629. (fs->slice_width + 1) * f->num_h_slices / f->width - 1,
  630. 0);
  631. put_symbol(c, state,
  632. (fs->slice_height + 1) * f->num_v_slices / f->height - 1,
  633. 0);
  634. for (j = 0; j < f->plane_count; j++) {
  635. put_symbol(c, state, f->plane[j].quant_table_index, 0);
  636. av_assert0(f->plane[j].quant_table_index == f->avctx->context_model);
  637. }
  638. }
  639. }
  640. }
  641. #endif /* CONFIG_FFV1_ENCODER */
  642. static av_cold int common_init(AVCodecContext *avctx)
  643. {
  644. FFV1Context *s = avctx->priv_data;
  645. if (!avctx->width || !avctx->height)
  646. return AVERROR_INVALIDDATA;
  647. s->avctx = avctx;
  648. s->flags = avctx->flags;
  649. avcodec_get_frame_defaults(&s->picture);
  650. ff_dsputil_init(&s->dsp, avctx);
  651. s->width = avctx->width;
  652. s->height = avctx->height;
  653. // defaults
  654. s->num_h_slices = 1;
  655. s->num_v_slices = 1;
  656. return 0;
  657. }
  658. static int init_slice_state(FFV1Context *f, FFV1Context *fs)
  659. {
  660. int j;
  661. fs->plane_count = f->plane_count;
  662. fs->transparency = f->transparency;
  663. for (j = 0; j < f->plane_count; j++) {
  664. PlaneContext *const p = &fs->plane[j];
  665. if (fs->ac) {
  666. if (!p->state)
  667. p->state = av_malloc(CONTEXT_SIZE * p->context_count *
  668. sizeof(uint8_t));
  669. if (!p->state)
  670. return AVERROR(ENOMEM);
  671. } else {
  672. if (!p->vlc_state)
  673. p->vlc_state = av_malloc(p->context_count * sizeof(VlcState));
  674. if (!p->vlc_state)
  675. return AVERROR(ENOMEM);
  676. }
  677. }
  678. if (fs->ac > 1) {
  679. // FIXME: only redo if state_transition changed
  680. for (j = 1; j < 256; j++) {
  681. fs->c. one_state[ j] = f->state_transition[j];
  682. fs->c.zero_state[256 - j] = 256 - fs->c.one_state[j];
  683. }
  684. }
  685. return 0;
  686. }
  687. static int init_slices_state(FFV1Context *f) {
  688. int i;
  689. for (i = 0; i < f->slice_count; i++) {
  690. FFV1Context *fs = f->slice_context[i];
  691. if (init_slice_state(f, fs) < 0)
  692. return -1;
  693. }
  694. return 0;
  695. }
  696. static av_cold int init_slice_contexts(FFV1Context *f)
  697. {
  698. int i;
  699. f->slice_count = f->num_h_slices * f->num_v_slices;
  700. for (i = 0; i < f->slice_count; i++) {
  701. FFV1Context *fs = av_mallocz(sizeof(*fs));
  702. int sx = i % f->num_h_slices;
  703. int sy = i / f->num_h_slices;
  704. int sxs = f->avctx->width * sx / f->num_h_slices;
  705. int sxe = f->avctx->width * (sx + 1) / f->num_h_slices;
  706. int sys = f->avctx->height * sy / f->num_v_slices;
  707. int sye = f->avctx->height * (sy + 1) / f->num_v_slices;
  708. f->slice_context[i] = fs;
  709. memcpy(fs, f, sizeof(*fs));
  710. memset(fs->rc_stat2, 0, sizeof(fs->rc_stat2));
  711. fs->slice_width = sxe - sxs;
  712. fs->slice_height = sye - sys;
  713. fs->slice_x = sxs;
  714. fs->slice_y = sys;
  715. fs->sample_buffer = av_malloc(3*4 * (fs->width+6) * sizeof(*fs->sample_buffer));
  716. if (!fs->sample_buffer)
  717. return AVERROR(ENOMEM);
  718. }
  719. return 0;
  720. }
  721. static int allocate_initial_states(FFV1Context *f)
  722. {
  723. int i;
  724. for (i = 0; i < f->quant_table_count; i++) {
  725. f->initial_states[i] = av_malloc(f->context_count[i] *
  726. sizeof(*f->initial_states[i]));
  727. if (!f->initial_states[i])
  728. return AVERROR(ENOMEM);
  729. memset(f->initial_states[i], 128,
  730. f->context_count[i] * sizeof(*f->initial_states[i]));
  731. }
  732. return 0;
  733. }
  734. #if CONFIG_FFV1_ENCODER
  735. static int write_extra_header(FFV1Context *f)
  736. {
  737. RangeCoder *const c = &f->c;
  738. uint8_t state[CONTEXT_SIZE];
  739. int i, j, k;
  740. uint8_t state2[32][CONTEXT_SIZE];
  741. unsigned v;
  742. memset(state2, 128, sizeof(state2));
  743. memset(state, 128, sizeof(state));
  744. f->avctx->extradata = av_malloc(f->avctx->extradata_size = 10000 +
  745. (11 * 11 * 5 * 5 * 5 + 11 * 11 * 11) * 32);
  746. ff_init_range_encoder(c, f->avctx->extradata, f->avctx->extradata_size);
  747. ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
  748. put_symbol(c, state, f->version, 0);
  749. if (f->version > 2) {
  750. if (f->version == 3)
  751. f->minor_version = 2;
  752. put_symbol(c, state, f->minor_version, 0);
  753. }
  754. put_symbol(c, state, f->ac, 0);
  755. if (f->ac > 1)
  756. for (i = 1; i < 256; i++)
  757. put_symbol(c, state, f->state_transition[i] - c->one_state[i], 1);
  758. put_symbol(c, state, f->colorspace, 0); // YUV cs type
  759. put_symbol(c, state, f->bits_per_raw_sample, 0);
  760. put_rac(c, state, f->chroma_planes);
  761. put_symbol(c, state, f->chroma_h_shift, 0);
  762. put_symbol(c, state, f->chroma_v_shift, 0);
  763. put_rac(c, state, f->transparency);
  764. put_symbol(c, state, f->num_h_slices - 1, 0);
  765. put_symbol(c, state, f->num_v_slices - 1, 0);
  766. put_symbol(c, state, f->quant_table_count, 0);
  767. for (i = 0; i < f->quant_table_count; i++)
  768. write_quant_tables(c, f->quant_tables[i]);
  769. for (i = 0; i < f->quant_table_count; i++) {
  770. for (j = 0; j < f->context_count[i] * CONTEXT_SIZE; j++)
  771. if (f->initial_states[i] && f->initial_states[i][0][j] != 128)
  772. break;
  773. if (j < f->context_count[i] * CONTEXT_SIZE) {
  774. put_rac(c, state, 1);
  775. for (j = 0; j < f->context_count[i]; j++)
  776. for (k = 0; k < CONTEXT_SIZE; k++) {
  777. int pred = j ? f->initial_states[i][j - 1][k] : 128;
  778. put_symbol(c, state2[k],
  779. (int8_t)(f->initial_states[i][j][k] - pred), 1);
  780. }
  781. } else {
  782. put_rac(c, state, 0);
  783. }
  784. }
  785. if (f->version > 2) {
  786. put_symbol(c, state, f->ec, 0);
  787. }
  788. f->avctx->extradata_size = ff_rac_terminate(c);
  789. v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, f->avctx->extradata, f->avctx->extradata_size);
  790. AV_WL32(f->avctx->extradata + f->avctx->extradata_size, v);
  791. f->avctx->extradata_size += 4;
  792. return 0;
  793. }
  794. static int sort_stt(FFV1Context *s, uint8_t stt[256])
  795. {
  796. int i, i2, changed, print = 0;
  797. do {
  798. changed = 0;
  799. for (i = 12; i < 244; i++) {
  800. for (i2 = i + 1; i2 < 245 && i2 < i + 4; i2++) {
  801. #define COST(old, new) \
  802. s->rc_stat[old][0] * -log2((256 - (new)) / 256.0) + \
  803. s->rc_stat[old][1] * -log2((new) / 256.0)
  804. #define COST2(old, new) \
  805. COST(old, new) + COST(256 - (old), 256 - (new))
  806. double size0 = COST2(i, i) + COST2(i2, i2);
  807. double sizeX = COST2(i, i2) + COST2(i2, i);
  808. if (sizeX < size0 && i != 128 && i2 != 128) {
  809. int j;
  810. FFSWAP(int, stt[i], stt[i2]);
  811. FFSWAP(int, s->rc_stat[i][0], s->rc_stat[i2][0]);
  812. FFSWAP(int, s->rc_stat[i][1], s->rc_stat[i2][1]);
  813. if (i != 256 - i2) {
  814. FFSWAP(int, stt[256 - i], stt[256 - i2]);
  815. FFSWAP(int, s->rc_stat[256 - i][0], s->rc_stat[256 - i2][0]);
  816. FFSWAP(int, s->rc_stat[256 - i][1], s->rc_stat[256 - i2][1]);
  817. }
  818. for (j = 1; j < 256; j++) {
  819. if (stt[j] == i)
  820. stt[j] = i2;
  821. else if (stt[j] == i2)
  822. stt[j] = i;
  823. if (i != 256 - i2) {
  824. if (stt[256 - j] == 256 - i)
  825. stt[256 - j] = 256 - i2;
  826. else if (stt[256 - j] == 256 - i2)
  827. stt[256 - j] = 256 - i;
  828. }
  829. }
  830. print = changed = 1;
  831. }
  832. }
  833. }
  834. } while (changed);
  835. return print;
  836. }
  837. static av_cold int encode_init(AVCodecContext *avctx)
  838. {
  839. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  840. FFV1Context *s = avctx->priv_data;
  841. int i, j, k, m;
  842. common_init(avctx);
  843. s->version = 0;
  844. if ((avctx->flags & (CODEC_FLAG_PASS1|CODEC_FLAG_PASS2)) || avctx->slices>1)
  845. s->version = FFMAX(s->version, 2);
  846. if (avctx->level == 3) {
  847. s->version = 3;
  848. }
  849. if (s->ec < 0) {
  850. s->ec = (s->version >= 3);
  851. }
  852. if (s->version >= 2 && avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  853. av_log(avctx, AV_LOG_ERROR, "Version 2 needed for requested features but version 2 is experimental and not enabled\n");
  854. return AVERROR_INVALIDDATA;
  855. }
  856. s->ac = avctx->coder_type > 0 ? 2 : 0;
  857. s->plane_count = 3;
  858. switch(avctx->pix_fmt) {
  859. case AV_PIX_FMT_YUV444P9:
  860. case AV_PIX_FMT_YUV422P9:
  861. case AV_PIX_FMT_YUV420P9:
  862. if (!avctx->bits_per_raw_sample)
  863. s->bits_per_raw_sample = 9;
  864. case AV_PIX_FMT_YUV444P10:
  865. case AV_PIX_FMT_YUV420P10:
  866. case AV_PIX_FMT_YUV422P10:
  867. s->packed_at_lsb = 1;
  868. if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
  869. s->bits_per_raw_sample = 10;
  870. case AV_PIX_FMT_GRAY16:
  871. case AV_PIX_FMT_YUV444P16:
  872. case AV_PIX_FMT_YUV422P16:
  873. case AV_PIX_FMT_YUV420P16:
  874. if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample) {
  875. s->bits_per_raw_sample = 16;
  876. } else if (!s->bits_per_raw_sample) {
  877. s->bits_per_raw_sample = avctx->bits_per_raw_sample;
  878. }
  879. if (s->bits_per_raw_sample <= 8) {
  880. av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
  881. return AVERROR_INVALIDDATA;
  882. }
  883. if (!s->ac && avctx->coder_type == -1) {
  884. av_log(avctx, AV_LOG_INFO, "bits_per_raw_sample > 8, forcing coder 1\n");
  885. s->ac = 2;
  886. }
  887. if (!s->ac) {
  888. av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample of more than 8 needs -coder 1 currently\n");
  889. return AVERROR_INVALIDDATA;
  890. }
  891. s->version = FFMAX(s->version, 1);
  892. case AV_PIX_FMT_GRAY8:
  893. case AV_PIX_FMT_YUV444P:
  894. case AV_PIX_FMT_YUV440P:
  895. case AV_PIX_FMT_YUV422P:
  896. case AV_PIX_FMT_YUV420P:
  897. case AV_PIX_FMT_YUV411P:
  898. case AV_PIX_FMT_YUV410P:
  899. s->chroma_planes = desc->nb_components < 3 ? 0 : 1;
  900. s->colorspace = 0;
  901. break;
  902. case AV_PIX_FMT_YUVA444P:
  903. case AV_PIX_FMT_YUVA422P:
  904. case AV_PIX_FMT_YUVA420P:
  905. s->chroma_planes = 1;
  906. s->colorspace = 0;
  907. s->transparency = 1;
  908. break;
  909. case AV_PIX_FMT_RGB32:
  910. s->colorspace = 1;
  911. s->transparency = 1;
  912. break;
  913. case AV_PIX_FMT_0RGB32:
  914. s->colorspace = 1;
  915. break;
  916. case AV_PIX_FMT_GBRP9:
  917. if (!avctx->bits_per_raw_sample)
  918. s->bits_per_raw_sample = 9;
  919. case AV_PIX_FMT_GBRP10:
  920. if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
  921. s->bits_per_raw_sample = 10;
  922. case AV_PIX_FMT_GBRP12:
  923. if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
  924. s->bits_per_raw_sample = 12;
  925. case AV_PIX_FMT_GBRP14:
  926. if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
  927. s->bits_per_raw_sample = 14;
  928. else if (!s->bits_per_raw_sample)
  929. s->bits_per_raw_sample = avctx->bits_per_raw_sample;
  930. s->colorspace = 1;
  931. s->chroma_planes = 1;
  932. s->version = FFMAX(s->version, 1);
  933. break;
  934. default:
  935. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  936. return AVERROR_INVALIDDATA;
  937. }
  938. if (s->transparency) {
  939. av_log(avctx, AV_LOG_WARNING, "Storing alpha plane, this will require a recent FFV1 decoder to playback!\n");
  940. }
  941. if (avctx->context_model > 1U) {
  942. av_log(avctx, AV_LOG_ERROR, "Invalid context model %d, valid values are 0 and 1\n", avctx->context_model);
  943. return AVERROR(EINVAL);
  944. }
  945. if (s->ac > 1)
  946. for (i = 1; i < 256; i++)
  947. s->state_transition[i] = ver2_state[i];
  948. for (i = 0; i < 256; i++) {
  949. s->quant_table_count = 2;
  950. if (s->bits_per_raw_sample <= 8) {
  951. s->quant_tables[0][0][i]= quant11[i];
  952. s->quant_tables[0][1][i]= 11*quant11[i];
  953. s->quant_tables[0][2][i]= 11*11*quant11[i];
  954. s->quant_tables[1][0][i]= quant11[i];
  955. s->quant_tables[1][1][i]= 11*quant11[i];
  956. s->quant_tables[1][2][i]= 11*11*quant5 [i];
  957. s->quant_tables[1][3][i]= 5*11*11*quant5 [i];
  958. s->quant_tables[1][4][i]= 5*5*11*11*quant5 [i];
  959. } else {
  960. s->quant_tables[0][0][i]= quant9_10bit[i];
  961. s->quant_tables[0][1][i]= 11*quant9_10bit[i];
  962. s->quant_tables[0][2][i]= 11*11*quant9_10bit[i];
  963. s->quant_tables[1][0][i]= quant9_10bit[i];
  964. s->quant_tables[1][1][i]= 11*quant9_10bit[i];
  965. s->quant_tables[1][2][i]= 11*11*quant5_10bit[i];
  966. s->quant_tables[1][3][i]= 5*11*11*quant5_10bit[i];
  967. s->quant_tables[1][4][i]= 5*5*11*11*quant5_10bit[i];
  968. }
  969. }
  970. s->context_count[0] = (11 * 11 * 11 + 1) / 2;
  971. s->context_count[1] = (11 * 11 * 5 * 5 * 5 + 1) / 2;
  972. memcpy(s->quant_table, s->quant_tables[avctx->context_model],
  973. sizeof(s->quant_table));
  974. for (i = 0; i < s->plane_count; i++) {
  975. PlaneContext *const p = &s->plane[i];
  976. memcpy(p->quant_table, s->quant_table, sizeof(p->quant_table));
  977. p->quant_table_index = avctx->context_model;
  978. p->context_count = s->context_count[p->quant_table_index];
  979. }
  980. if (allocate_initial_states(s) < 0)
  981. return AVERROR(ENOMEM);
  982. avctx->coded_frame = &s->picture;
  983. if (!s->transparency)
  984. s->plane_count = 2;
  985. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  986. s->picture_number = 0;
  987. if (avctx->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2)) {
  988. for (i = 0; i < s->quant_table_count; i++) {
  989. s->rc_stat2[i] = av_mallocz(s->context_count[i] *
  990. sizeof(*s->rc_stat2[i]));
  991. if (!s->rc_stat2[i])
  992. return AVERROR(ENOMEM);
  993. }
  994. }
  995. if (avctx->stats_in) {
  996. char *p = avctx->stats_in;
  997. uint8_t best_state[256][256];
  998. int gob_count = 0;
  999. char *next;
  1000. av_assert0(s->version >= 2);
  1001. for (;;) {
  1002. for (j = 0; j < 256; j++)
  1003. for (i = 0; i < 2; i++) {
  1004. s->rc_stat[j][i] = strtol(p, &next, 0);
  1005. if (next == p) {
  1006. av_log(avctx, AV_LOG_ERROR,
  1007. "2Pass file invalid at %d %d [%s]\n", j, i, p);
  1008. return -1;
  1009. }
  1010. p = next;
  1011. }
  1012. for (i = 0; i < s->quant_table_count; i++)
  1013. for (j = 0; j < s->context_count[i]; j++) {
  1014. for (k = 0; k < 32; k++)
  1015. for (m = 0; m < 2; m++) {
  1016. s->rc_stat2[i][j][k][m] = strtol(p, &next, 0);
  1017. if (next == p) {
  1018. av_log(avctx, AV_LOG_ERROR,
  1019. "2Pass file invalid at %d %d %d %d [%s]\n",
  1020. i, j, k, m, p);
  1021. return AVERROR_INVALIDDATA;
  1022. }
  1023. p = next;
  1024. }
  1025. }
  1026. gob_count = strtol(p, &next, 0);
  1027. if (next == p || gob_count <= 0) {
  1028. av_log(avctx, AV_LOG_ERROR, "2Pass file invalid\n");
  1029. return AVERROR_INVALIDDATA;
  1030. }
  1031. p = next;
  1032. while (*p == '\n' || *p == ' ')
  1033. p++;
  1034. if (p[0] == 0)
  1035. break;
  1036. }
  1037. sort_stt(s, s->state_transition);
  1038. find_best_state(best_state, s->state_transition);
  1039. for (i = 0; i < s->quant_table_count; i++) {
  1040. for (j = 0; j < s->context_count[i]; j++)
  1041. for (k = 0; k < 32; k++) {
  1042. double p = 128;
  1043. if (s->rc_stat2[i][j][k][0] + s->rc_stat2[i][j][k][1]) {
  1044. p = 256.0 * s->rc_stat2[i][j][k][1] /
  1045. (s->rc_stat2[i][j][k][0] + s->rc_stat2[i][j][k][1]);
  1046. }
  1047. s->initial_states[i][j][k] =
  1048. best_state[av_clip(round(p), 1, 255)][av_clip((s->rc_stat2[i][j][k][0] +
  1049. s->rc_stat2[i][j][k][1]) /
  1050. gob_count, 0, 255)];
  1051. }
  1052. }
  1053. }
  1054. if (s->version > 1) {
  1055. for (s->num_v_slices = 2; s->num_v_slices < 9; s->num_v_slices++) {
  1056. for (s->num_h_slices = s->num_v_slices; s->num_h_slices < 2*s->num_v_slices; s->num_h_slices++) {
  1057. if (avctx->slices == s->num_h_slices * s->num_v_slices && avctx->slices <= 64 || !avctx->slices)
  1058. goto slices_ok;
  1059. }
  1060. }
  1061. av_log(avctx, AV_LOG_ERROR, "Unsupported number %d of slices requested, please specify a supported number with -slices (ex:4,6,9,12,16, ...)\n", avctx->slices);
  1062. return -1;
  1063. slices_ok:
  1064. write_extra_header(s);
  1065. }
  1066. if (init_slice_contexts(s) < 0)
  1067. return -1;
  1068. if (init_slices_state(s) < 0)
  1069. return -1;
  1070. #define STATS_OUT_SIZE 1024 * 1024 * 6
  1071. if (avctx->flags & CODEC_FLAG_PASS1) {
  1072. avctx->stats_out = av_mallocz(STATS_OUT_SIZE);
  1073. for (i = 0; i < s->quant_table_count; i++)
  1074. for (j = 0; j < s->slice_count; j++) {
  1075. FFV1Context *sf = s->slice_context[j];
  1076. av_assert0(!sf->rc_stat2[i]);
  1077. sf->rc_stat2[i] = av_mallocz(s->context_count[i] *
  1078. sizeof(*sf->rc_stat2[i]));
  1079. if (!sf->rc_stat2[i])
  1080. return AVERROR(ENOMEM);
  1081. }
  1082. }
  1083. return 0;
  1084. }
  1085. #endif /* CONFIG_FFV1_ENCODER */
  1086. static void clear_slice_state(FFV1Context *f, FFV1Context *fs)
  1087. {
  1088. int i, j;
  1089. for (i = 0; i < f->plane_count; i++) {
  1090. PlaneContext *p = &fs->plane[i];
  1091. p->interlace_bit_state[0] = 128;
  1092. p->interlace_bit_state[1] = 128;
  1093. if (fs->ac) {
  1094. if (f->initial_states[p->quant_table_index]) {
  1095. memcpy(p->state, f->initial_states[p->quant_table_index],
  1096. CONTEXT_SIZE * p->context_count);
  1097. } else
  1098. memset(p->state, 128, CONTEXT_SIZE * p->context_count);
  1099. } else {
  1100. for (j = 0; j < p->context_count; j++) {
  1101. p->vlc_state[j].drift = 0;
  1102. p->vlc_state[j].error_sum = 4; // FFMAX((RANGE + 32)/64, 2);
  1103. p->vlc_state[j].bias = 0;
  1104. p->vlc_state[j].count = 1;
  1105. }
  1106. }
  1107. }
  1108. }
  1109. #if CONFIG_FFV1_ENCODER
  1110. static void encode_slice_header(FFV1Context *f, FFV1Context *fs)
  1111. {
  1112. RangeCoder *c = &fs->c;
  1113. uint8_t state[CONTEXT_SIZE];
  1114. int j;
  1115. memset(state, 128, sizeof(state));
  1116. put_symbol(c, state, (fs->slice_x +1)*f->num_h_slices / f->width , 0);
  1117. put_symbol(c, state, (fs->slice_y +1)*f->num_v_slices / f->height , 0);
  1118. put_symbol(c, state, (fs->slice_width +1)*f->num_h_slices / f->width -1, 0);
  1119. put_symbol(c, state, (fs->slice_height+1)*f->num_v_slices / f->height-1, 0);
  1120. for (j=0; j<f->plane_count; j++) {
  1121. put_symbol(c, state, f->plane[j].quant_table_index, 0);
  1122. av_assert0(f->plane[j].quant_table_index == f->avctx->context_model);
  1123. }
  1124. if (!f->picture.interlaced_frame) put_symbol(c, state, 3, 0);
  1125. else put_symbol(c, state, 1 + !f->picture.top_field_first, 0);
  1126. put_symbol(c, state, f->picture.sample_aspect_ratio.num, 0);
  1127. put_symbol(c, state, f->picture.sample_aspect_ratio.den, 0);
  1128. }
  1129. static int encode_slice(AVCodecContext *c, void *arg)
  1130. {
  1131. FFV1Context *fs = *(void **)arg;
  1132. FFV1Context *f = fs->avctx->priv_data;
  1133. int width = fs->slice_width;
  1134. int height = fs->slice_height;
  1135. int x = fs->slice_x;
  1136. int y = fs->slice_y;
  1137. AVFrame *const p = &f->picture;
  1138. const int ps = (av_pix_fmt_desc_get(c->pix_fmt)->flags & PIX_FMT_PLANAR)
  1139. ?
  1140. (f->bits_per_raw_sample>8)+1
  1141. :
  1142. 4;
  1143. if (p->key_frame)
  1144. clear_slice_state(f, fs);
  1145. if (f->version > 2) {
  1146. encode_slice_header(f, fs);
  1147. }
  1148. if (!fs->ac) {
  1149. if (f->version > 2)
  1150. put_rac(&fs->c, (uint8_t[]) {129}, 0);
  1151. fs->ac_byte_count = f->version > 2 || (!x&&!y) ? ff_rac_terminate(&fs->c) : 0;
  1152. init_put_bits(&fs->pb, fs->c.bytestream_start + fs->ac_byte_count, fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count);
  1153. }
  1154. if (f->colorspace == 0) {
  1155. const int chroma_width = -((-width) >> f->chroma_h_shift);
  1156. const int chroma_height = -((-height) >> f->chroma_v_shift);
  1157. const int cx = x >> f->chroma_h_shift;
  1158. const int cy = y >> f->chroma_v_shift;
  1159. encode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0);
  1160. if (f->chroma_planes) {
  1161. encode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
  1162. encode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1);
  1163. }
  1164. if (fs->transparency)
  1165. encode_plane(fs, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2);
  1166. } else {
  1167. uint8_t *planes[3] = {p->data[0] + ps*x + y*p->linesize[0],
  1168. p->data[1] + ps*x + y*p->linesize[1],
  1169. p->data[2] + ps*x + y*p->linesize[2]};
  1170. encode_rgb_frame(fs, planes, width, height, p->linesize);
  1171. }
  1172. emms_c();
  1173. return 0;
  1174. }
  1175. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  1176. const AVFrame *pict, int *got_packet)
  1177. {
  1178. FFV1Context *f = avctx->priv_data;
  1179. RangeCoder *const c = &f->slice_context[0]->c;
  1180. AVFrame *const p = &f->picture;
  1181. int used_count = 0;
  1182. uint8_t keystate = 128;
  1183. uint8_t *buf_p;
  1184. int i, ret;
  1185. if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*((8*2+1+1)*4)/8
  1186. + FF_MIN_BUFFER_SIZE)) < 0)
  1187. return ret;
  1188. ff_init_range_encoder(c, pkt->data, pkt->size);
  1189. ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
  1190. *p = *pict;
  1191. p->pict_type = AV_PICTURE_TYPE_I;
  1192. if (avctx->gop_size == 0 || f->picture_number % avctx->gop_size == 0) {
  1193. put_rac(c, &keystate, 1);
  1194. p->key_frame = 1;
  1195. f->gob_count++;
  1196. write_header(f);
  1197. } else {
  1198. put_rac(c, &keystate, 0);
  1199. p->key_frame = 0;
  1200. }
  1201. if (f->ac > 1) {
  1202. int i;
  1203. for (i = 1; i < 256; i++) {
  1204. c->one_state[i] = f->state_transition[i];
  1205. c->zero_state[256 - i] = 256 - c->one_state[i];
  1206. }
  1207. }
  1208. for (i = 1; i < f->slice_count; i++) {
  1209. FFV1Context *fs = f->slice_context[i];
  1210. uint8_t *start = pkt->data + (pkt->size - used_count) * (int64_t)i / f->slice_count;
  1211. int len = pkt->size / f->slice_count;
  1212. ff_init_range_encoder(&fs->c, start, len);
  1213. }
  1214. avctx->execute(avctx, encode_slice, &f->slice_context[0], NULL,
  1215. f->slice_count, sizeof(void *));
  1216. buf_p = pkt->data;
  1217. for (i = 0; i < f->slice_count; i++) {
  1218. FFV1Context *fs = f->slice_context[i];
  1219. int bytes;
  1220. if (fs->ac) {
  1221. uint8_t state=129;
  1222. put_rac(&fs->c, &state, 0);
  1223. bytes = ff_rac_terminate(&fs->c);
  1224. } else {
  1225. flush_put_bits(&fs->pb); // FIXME: nicer padding
  1226. bytes = fs->ac_byte_count + (put_bits_count(&fs->pb) + 7)/8;
  1227. }
  1228. if (i > 0 || f->version > 2) {
  1229. av_assert0(bytes < pkt->size / f->slice_count);
  1230. memmove(buf_p, fs->c.bytestream_start, bytes);
  1231. av_assert0(bytes < (1 << 24));
  1232. AV_WB24(buf_p + bytes, bytes);
  1233. bytes += 3;
  1234. }
  1235. if (f->ec) {
  1236. unsigned v;
  1237. buf_p[bytes++] = 0;
  1238. v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, bytes);
  1239. AV_WL32(buf_p + bytes, v); bytes += 4;
  1240. }
  1241. buf_p += bytes;
  1242. }
  1243. if ((avctx->flags & CODEC_FLAG_PASS1) && (f->picture_number & 31) == 0) {
  1244. int j, k, m;
  1245. char *p = avctx->stats_out;
  1246. char *end = p + STATS_OUT_SIZE;
  1247. memset(f->rc_stat, 0, sizeof(f->rc_stat));
  1248. for (i = 0; i < f->quant_table_count; i++)
  1249. memset(f->rc_stat2[i], 0, f->context_count[i] * sizeof(*f->rc_stat2[i]));
  1250. for (j = 0; j < f->slice_count; j++) {
  1251. FFV1Context *fs = f->slice_context[j];
  1252. for (i = 0; i < 256; i++) {
  1253. f->rc_stat[i][0] += fs->rc_stat[i][0];
  1254. f->rc_stat[i][1] += fs->rc_stat[i][1];
  1255. }
  1256. for (i = 0; i < f->quant_table_count; i++) {
  1257. for (k = 0; k < f->context_count[i]; k++)
  1258. for (m = 0; m < 32; m++) {
  1259. f->rc_stat2[i][k][m][0] += fs->rc_stat2[i][k][m][0];
  1260. f->rc_stat2[i][k][m][1] += fs->rc_stat2[i][k][m][1];
  1261. }
  1262. }
  1263. }
  1264. for (j = 0; j < 256; j++) {
  1265. snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
  1266. f->rc_stat[j][0], f->rc_stat[j][1]);
  1267. p += strlen(p);
  1268. }
  1269. snprintf(p, end - p, "\n");
  1270. for (i = 0; i < f->quant_table_count; i++) {
  1271. for (j = 0; j < f->context_count[i]; j++)
  1272. for (m = 0; m < 32; m++) {
  1273. snprintf(p, end - p, "%" PRIu64 " %" PRIu64 " ",
  1274. f->rc_stat2[i][j][m][0], f->rc_stat2[i][j][m][1]);
  1275. p += strlen(p);
  1276. }
  1277. }
  1278. snprintf(p, end - p, "%d\n", f->gob_count);
  1279. } else if (avctx->flags & CODEC_FLAG_PASS1)
  1280. avctx->stats_out[0] = '\0';
  1281. f->picture_number++;
  1282. pkt->size = buf_p - pkt->data;
  1283. pkt->flags |= AV_PKT_FLAG_KEY * p->key_frame;
  1284. *got_packet = 1;
  1285. return 0;
  1286. }
  1287. #endif /* CONFIG_FFV1_ENCODER */
  1288. static av_cold int common_end(AVCodecContext *avctx)
  1289. {
  1290. FFV1Context *s = avctx->priv_data;
  1291. int i, j;
  1292. if (avctx->codec->decode && s->picture.data[0])
  1293. avctx->release_buffer(avctx, &s->picture);
  1294. if (avctx->codec->decode && s->last_picture.data[0])
  1295. avctx->release_buffer(avctx, &s->last_picture);
  1296. for (j = 0; j < s->slice_count; j++) {
  1297. FFV1Context *fs = s->slice_context[j];
  1298. for (i = 0; i < s->plane_count; i++) {
  1299. PlaneContext *p = &fs->plane[i];
  1300. av_freep(&p->state);
  1301. av_freep(&p->vlc_state);
  1302. }
  1303. av_freep(&fs->sample_buffer);
  1304. }
  1305. av_freep(&avctx->stats_out);
  1306. for (j = 0; j < s->quant_table_count; j++) {
  1307. av_freep(&s->initial_states[j]);
  1308. for (i = 0; i < s->slice_count; i++) {
  1309. FFV1Context *sf = s->slice_context[i];
  1310. av_freep(&sf->rc_stat2[j]);
  1311. }
  1312. av_freep(&s->rc_stat2[j]);
  1313. }
  1314. for (i = 0; i < s->slice_count; i++)
  1315. av_freep(&s->slice_context[i]);
  1316. return 0;
  1317. }
  1318. static av_always_inline void decode_line(FFV1Context *s, int w,
  1319. int16_t *sample[2],
  1320. int plane_index, int bits)
  1321. {
  1322. PlaneContext *const p = &s->plane[plane_index];
  1323. RangeCoder *const c = &s->c;
  1324. int x;
  1325. int run_count = 0;
  1326. int run_mode = 0;
  1327. int run_index = s->run_index;
  1328. for (x = 0; x < w; x++) {
  1329. int diff, context, sign;
  1330. context = get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
  1331. if (context < 0) {
  1332. context = -context;
  1333. sign = 1;
  1334. } else
  1335. sign = 0;
  1336. av_assert2(context < p->context_count);
  1337. if (s->ac) {
  1338. diff = get_symbol_inline(c, p->state[context], 1);
  1339. } else {
  1340. if (context == 0 && run_mode == 0)
  1341. run_mode = 1;
  1342. if (run_mode) {
  1343. if (run_count == 0 && run_mode == 1) {
  1344. if (get_bits1(&s->gb)) {
  1345. run_count = 1 << ff_log2_run[run_index];
  1346. if (x + run_count <= w)
  1347. run_index++;
  1348. } else {
  1349. if (ff_log2_run[run_index])
  1350. run_count = get_bits(&s->gb, ff_log2_run[run_index]);
  1351. else
  1352. run_count = 0;
  1353. if (run_index)
  1354. run_index--;
  1355. run_mode = 2;
  1356. }
  1357. }
  1358. run_count--;
  1359. if (run_count < 0) {
  1360. run_mode = 0;
  1361. run_count = 0;
  1362. diff = get_vlc_symbol(&s->gb, &p->vlc_state[context],
  1363. bits);
  1364. if (diff >= 0)
  1365. diff++;
  1366. } else
  1367. diff = 0;
  1368. } else
  1369. diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  1370. av_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
  1371. run_count, run_index, run_mode, x, get_bits_count(&s->gb));
  1372. }
  1373. if (sign)
  1374. diff = -diff;
  1375. sample[1][x] = (predict(sample[1] + x, sample[0] + x) + diff) &
  1376. ((1 << bits) - 1);
  1377. }
  1378. s->run_index = run_index;
  1379. }
  1380. static void decode_plane(FFV1Context *s, uint8_t *src,
  1381. int w, int h, int stride, int plane_index)
  1382. {
  1383. int x, y;
  1384. int16_t *sample[2];
  1385. sample[0] = s->sample_buffer + 3;
  1386. sample[1] = s->sample_buffer + w + 6 + 3;
  1387. s->run_index = 0;
  1388. memset(s->sample_buffer, 0, 2 * (w + 6) * sizeof(*s->sample_buffer));
  1389. for (y = 0; y < h; y++) {
  1390. int16_t *temp = sample[0]; // FIXME: try a normal buffer
  1391. sample[0] = sample[1];
  1392. sample[1] = temp;
  1393. sample[1][-1] = sample[0][0];
  1394. sample[0][w] = sample[0][w - 1];
  1395. // { START_TIMER
  1396. if (s->avctx->bits_per_raw_sample <= 8) {
  1397. decode_line(s, w, sample, plane_index, 8);
  1398. for (x = 0; x < w; x++)
  1399. src[x + stride * y] = sample[1][x];
  1400. } else {
  1401. decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
  1402. if (s->packed_at_lsb) {
  1403. for (x = 0; x < w; x++) {
  1404. ((uint16_t*)(src + stride*y))[x] = sample[1][x];
  1405. }
  1406. } else {
  1407. for (x = 0; x < w; x++) {
  1408. ((uint16_t*)(src + stride*y))[x] = sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
  1409. }
  1410. }
  1411. }
  1412. // STOP_TIMER("decode-line") }
  1413. }
  1414. }
  1415. static void decode_rgb_frame(FFV1Context *s, uint8_t *src[3], int w, int h, int stride[3])
  1416. {
  1417. int x, y, p;
  1418. int16_t *sample[4][2];
  1419. int lbd = s->avctx->bits_per_raw_sample <= 8;
  1420. int bits = s->avctx->bits_per_raw_sample > 0 ? s->avctx->bits_per_raw_sample : 8;
  1421. int offset = 1 << bits;
  1422. for (x = 0; x < 4; x++) {
  1423. sample[x][0] = s->sample_buffer + x * 2 * (w + 6) + 3;
  1424. sample[x][1] = s->sample_buffer + (x * 2 + 1) * (w + 6) + 3;
  1425. }
  1426. s->run_index = 0;
  1427. memset(s->sample_buffer, 0, 8*(w+6)*sizeof(*s->sample_buffer));
  1428. for (y = 0; y < h; y++) {
  1429. for (p = 0; p < 3 + s->transparency; p++) {
  1430. int16_t *temp = sample[p][0]; // FIXME: try a normal buffer
  1431. sample[p][0] = sample[p][1];
  1432. sample[p][1] = temp;
  1433. sample[p][1][-1]= sample[p][0][0 ];
  1434. sample[p][0][ w]= sample[p][0][w-1];
  1435. if (lbd)
  1436. decode_line(s, w, sample[p], (p+1)/2, 9);
  1437. else
  1438. decode_line(s, w, sample[p], (p+1)/2, bits+1);
  1439. }
  1440. for (x = 0; x < w; x++) {
  1441. int g = sample[0][1][x];
  1442. int b = sample[1][1][x];
  1443. int r = sample[2][1][x];
  1444. int a = sample[3][1][x];
  1445. b -= offset;
  1446. r -= offset;
  1447. g -= (b + r) >> 2;
  1448. b += g;
  1449. r += g;
  1450. if (lbd)
  1451. *((uint32_t*)(src[0] + x*4 + stride[0]*y)) = b + (g<<8) + (r<<16) + (a<<24);
  1452. else {
  1453. *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = b;
  1454. *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = g;
  1455. *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
  1456. }
  1457. }
  1458. }
  1459. }
  1460. static int decode_slice_header(FFV1Context *f, FFV1Context *fs)
  1461. {
  1462. RangeCoder *c = &fs->c;
  1463. uint8_t state[CONTEXT_SIZE];
  1464. unsigned ps, i, context_count;
  1465. memset(state, 128, sizeof(state));
  1466. av_assert0(f->version > 2);
  1467. fs->slice_x = get_symbol(c, state, 0) * f->width ;
  1468. fs->slice_y = get_symbol(c, state, 0) * f->height;
  1469. fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
  1470. fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
  1471. fs->slice_x /= f->num_h_slices;
  1472. fs->slice_y /= f->num_v_slices;
  1473. fs->slice_width = fs->slice_width /f->num_h_slices - fs->slice_x;
  1474. fs->slice_height = fs->slice_height/f->num_v_slices - fs->slice_y;
  1475. if ((unsigned)fs->slice_width > f->width || (unsigned)fs->slice_height > f->height)
  1476. return -1;
  1477. if ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
  1478. || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
  1479. return -1;
  1480. for (i = 0; i < f->plane_count; i++) {
  1481. PlaneContext * const p = &fs->plane[i];
  1482. int idx = get_symbol(c, state, 0);
  1483. if (idx > (unsigned)f->quant_table_count) {
  1484. av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
  1485. return -1;
  1486. }
  1487. p->quant_table_index = idx;
  1488. memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
  1489. context_count = f->context_count[idx];
  1490. if (p->context_count < context_count) {
  1491. av_freep(&p->state);
  1492. av_freep(&p->vlc_state);
  1493. }
  1494. p->context_count = context_count;
  1495. }
  1496. ps = get_symbol(c, state, 0);
  1497. if (ps == 1) {
  1498. f->picture.interlaced_frame = 1;
  1499. f->picture.top_field_first = 1;
  1500. } else if (ps == 2) {
  1501. f->picture.interlaced_frame = 1;
  1502. f->picture.top_field_first = 0;
  1503. } else if (ps == 3) {
  1504. f->picture.interlaced_frame = 0;
  1505. }
  1506. f->picture.sample_aspect_ratio.num = get_symbol(c, state, 0);
  1507. f->picture.sample_aspect_ratio.den = get_symbol(c, state, 0);
  1508. return 0;
  1509. }
  1510. static int decode_slice(AVCodecContext *c, void *arg)
  1511. {
  1512. FFV1Context *fs = *(void **)arg;
  1513. FFV1Context *f = fs->avctx->priv_data;
  1514. int width, height, x, y;
  1515. const int ps = (av_pix_fmt_desc_get(c->pix_fmt)->flags & PIX_FMT_PLANAR)
  1516. ?
  1517. (c->bits_per_raw_sample>8)+1
  1518. :
  1519. 4;
  1520. AVFrame * const p = &f->picture;
  1521. if (f->version > 2) {
  1522. if (init_slice_state(f, fs) < 0)
  1523. return AVERROR(ENOMEM);
  1524. if (decode_slice_header(f, fs) < 0) {
  1525. fs->slice_damaged = 1;
  1526. return AVERROR_INVALIDDATA;
  1527. }
  1528. }
  1529. if (init_slice_state(f, fs) < 0)
  1530. return AVERROR(ENOMEM);
  1531. if (f->picture.key_frame)
  1532. clear_slice_state(f, fs);
  1533. width = fs->slice_width;
  1534. height= fs->slice_height;
  1535. x= fs->slice_x;
  1536. y= fs->slice_y;
  1537. if (!fs->ac) {
  1538. if (f->version == 3 && f->minor_version > 1 || f->version > 3)
  1539. get_rac(&fs->c, (uint8_t[]) {129});
  1540. fs->ac_byte_count = f->version > 2 || (!x&&!y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0;
  1541. init_get_bits(&fs->gb,
  1542. fs->c.bytestream_start + fs->ac_byte_count,
  1543. (fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count) * 8);
  1544. }
  1545. av_assert1(width && height);
  1546. if (f->colorspace == 0) {
  1547. const int chroma_width = -((-width) >> f->chroma_h_shift);
  1548. const int chroma_height = -((-height) >> f->chroma_v_shift);
  1549. const int cx = x >> f->chroma_h_shift;
  1550. const int cy = y >> f->chroma_v_shift;
  1551. decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0);
  1552. if (f->chroma_planes) {
  1553. decode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
  1554. decode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1);
  1555. }
  1556. if (fs->transparency)
  1557. decode_plane(fs, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2);
  1558. } else {
  1559. uint8_t *planes[3] = {p->data[0] + ps*x + y*p->linesize[0],
  1560. p->data[1] + ps*x + y*p->linesize[1],
  1561. p->data[2] + ps*x + y*p->linesize[2]};
  1562. decode_rgb_frame(fs, planes, width, height, p->linesize);
  1563. }
  1564. if (fs->ac && f->version > 2) {
  1565. int v;
  1566. get_rac(&fs->c, (uint8_t[]) {129});
  1567. v = fs->c.bytestream_end - fs->c.bytestream - 2 - 5*f->ec;
  1568. if (v) {
  1569. av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n", v);
  1570. fs->slice_damaged = 1;
  1571. }
  1572. }
  1573. emms_c();
  1574. return 0;
  1575. }
  1576. static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale)
  1577. {
  1578. int v;
  1579. int i = 0;
  1580. uint8_t state[CONTEXT_SIZE];
  1581. memset(state, 128, sizeof(state));
  1582. for (v = 0; i < 128; v++) {
  1583. unsigned len = get_symbol(c, state, 0) + 1;
  1584. if (len > 128 - i)
  1585. return -1;
  1586. while (len--) {
  1587. quant_table[i] = scale * v;
  1588. i++;
  1589. }
  1590. }
  1591. for (i = 1; i < 128; i++)
  1592. quant_table[256 - i] = -quant_table[i];
  1593. quant_table[128] = -quant_table[127];
  1594. return 2 * v - 1;
  1595. }
  1596. static int read_quant_tables(RangeCoder *c,
  1597. int16_t quant_table[MAX_CONTEXT_INPUTS][256])
  1598. {
  1599. int i;
  1600. int context_count = 1;
  1601. for (i = 0; i < 5; i++) {
  1602. context_count *= read_quant_table(c, quant_table[i], context_count);
  1603. if (context_count > 32768U) {
  1604. return -1;
  1605. }
  1606. }
  1607. return (context_count + 1) / 2;
  1608. }
  1609. static int read_extra_header(FFV1Context *f)
  1610. {
  1611. RangeCoder *const c = &f->c;
  1612. uint8_t state[CONTEXT_SIZE];
  1613. int i, j, k;
  1614. uint8_t state2[32][CONTEXT_SIZE];
  1615. memset(state2, 128, sizeof(state2));
  1616. memset(state, 128, sizeof(state));
  1617. ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
  1618. ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
  1619. f->version = get_symbol(c, state, 0);
  1620. if (f->version > 2) {
  1621. c->bytestream_end -= 4;
  1622. f->minor_version = get_symbol(c, state, 0);
  1623. }
  1624. f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
  1625. if (f->ac > 1)
  1626. for (i = 1; i < 256; i++)
  1627. f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
  1628. f->colorspace = get_symbol(c, state, 0); // YUV cs type
  1629. f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
  1630. f->chroma_planes = get_rac(c, state);
  1631. f->chroma_h_shift = get_symbol(c, state, 0);
  1632. f->chroma_v_shift = get_symbol(c, state, 0);
  1633. f->transparency = get_rac(c, state);
  1634. f->plane_count = 2 + f->transparency;
  1635. f->num_h_slices = 1 + get_symbol(c, state, 0);
  1636. f->num_v_slices = 1 + get_symbol(c, state, 0);
  1637. if (f->num_h_slices > (unsigned)f->width ||
  1638. f->num_v_slices > (unsigned)f->height) {
  1639. av_log(f->avctx, AV_LOG_ERROR, "too many slices\n");
  1640. return -1;
  1641. }
  1642. f->quant_table_count = get_symbol(c, state, 0);
  1643. if (f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
  1644. return -1;
  1645. for (i = 0; i < f->quant_table_count; i++) {
  1646. f->context_count[i] = read_quant_tables(c, f->quant_tables[i]);
  1647. if (f->context_count[i] < 0) {
  1648. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  1649. return -1;
  1650. }
  1651. }
  1652. if (allocate_initial_states(f) < 0)
  1653. return AVERROR(ENOMEM);
  1654. for (i = 0; i < f->quant_table_count; i++)
  1655. if (get_rac(c, state))
  1656. for (j = 0; j < f->context_count[i]; j++)
  1657. for (k = 0; k < CONTEXT_SIZE; k++) {
  1658. int pred = j ? f->initial_states[i][j - 1][k] : 128;
  1659. f->initial_states[i][j][k] =
  1660. (pred + get_symbol(c, state2[k], 1)) & 0xFF;
  1661. }
  1662. if (f->version > 2) {
  1663. f->ec = get_symbol(c, state, 0);
  1664. }
  1665. if (f->version > 2) {
  1666. unsigned v;
  1667. v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, f->avctx->extradata, f->avctx->extradata_size);
  1668. if (v) {
  1669. av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
  1670. return AVERROR_INVALIDDATA;
  1671. }
  1672. }
  1673. return 0;
  1674. }
  1675. static int read_header(FFV1Context *f)
  1676. {
  1677. uint8_t state[CONTEXT_SIZE];
  1678. int i, j, context_count = -1; //-1 to avoid warning
  1679. RangeCoder *const c = &f->slice_context[0]->c;
  1680. memset(state, 128, sizeof(state));
  1681. if (f->version < 2) {
  1682. unsigned v= get_symbol(c, state, 0);
  1683. if (v >= 2) {
  1684. av_log(f->avctx, AV_LOG_ERROR, "invalid version %d in ver01 header\n", v);
  1685. return AVERROR_INVALIDDATA;
  1686. }
  1687. f->version = v;
  1688. f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
  1689. if (f->ac > 1)
  1690. for (i = 1; i < 256; i++)
  1691. f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
  1692. f->colorspace = get_symbol(c, state, 0); // YUV cs type
  1693. if (f->version > 0)
  1694. f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
  1695. f->chroma_planes = get_rac(c, state);
  1696. f->chroma_h_shift = get_symbol(c, state, 0);
  1697. f->chroma_v_shift = get_symbol(c, state, 0);
  1698. f->transparency = get_rac(c, state);
  1699. f->plane_count = 2 + f->transparency;
  1700. }
  1701. if (f->colorspace == 0) {
  1702. if (!f->transparency && !f->chroma_planes) {
  1703. if (f->avctx->bits_per_raw_sample <= 8)
  1704. f->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  1705. else
  1706. f->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
  1707. } else if (f->avctx->bits_per_raw_sample<=8 && !f->transparency) {
  1708. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  1709. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P; break;
  1710. case 0x01: f->avctx->pix_fmt = AV_PIX_FMT_YUV440P; break;
  1711. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P; break;
  1712. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P; break;
  1713. case 0x20: f->avctx->pix_fmt = AV_PIX_FMT_YUV411P; break;
  1714. case 0x22: f->avctx->pix_fmt = AV_PIX_FMT_YUV410P; break;
  1715. default:
  1716. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  1717. return -1;
  1718. }
  1719. } else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency) {
  1720. switch(16*f->chroma_h_shift + f->chroma_v_shift) {
  1721. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P; break;
  1722. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P; break;
  1723. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P; break;
  1724. default:
  1725. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  1726. return -1;
  1727. }
  1728. } else if (f->avctx->bits_per_raw_sample == 9) {
  1729. f->packed_at_lsb = 1;
  1730. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  1731. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P9; break;
  1732. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P9; break;
  1733. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P9; break;
  1734. default:
  1735. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  1736. return -1;
  1737. }
  1738. } else if (f->avctx->bits_per_raw_sample == 10) {
  1739. f->packed_at_lsb = 1;
  1740. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  1741. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P10; break;
  1742. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P10; break;
  1743. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P10; break;
  1744. default:
  1745. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  1746. return -1;
  1747. }
  1748. } else {
  1749. switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
  1750. case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P16; break;
  1751. case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P16; break;
  1752. case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P16; break;
  1753. default:
  1754. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  1755. return -1;
  1756. }
  1757. }
  1758. } else if (f->colorspace == 1) {
  1759. if (f->chroma_h_shift || f->chroma_v_shift) {
  1760. av_log(f->avctx, AV_LOG_ERROR,
  1761. "chroma subsampling not supported in this colorspace\n");
  1762. return -1;
  1763. }
  1764. if ( f->avctx->bits_per_raw_sample == 9)
  1765. f->avctx->pix_fmt = AV_PIX_FMT_GBRP9;
  1766. else if (f->avctx->bits_per_raw_sample == 10)
  1767. f->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
  1768. else if (f->avctx->bits_per_raw_sample == 12)
  1769. f->avctx->pix_fmt = AV_PIX_FMT_GBRP12;
  1770. else if (f->avctx->bits_per_raw_sample == 14)
  1771. f->avctx->pix_fmt = AV_PIX_FMT_GBRP14;
  1772. else
  1773. if (f->transparency) f->avctx->pix_fmt = AV_PIX_FMT_RGB32;
  1774. else f->avctx->pix_fmt = AV_PIX_FMT_0RGB32;
  1775. } else {
  1776. av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
  1777. return -1;
  1778. }
  1779. av_dlog(f->avctx, "%d %d %d\n",
  1780. f->chroma_h_shift, f->chroma_v_shift, f->avctx->pix_fmt);
  1781. if (f->version < 2) {
  1782. context_count = read_quant_tables(c, f->quant_table);
  1783. if (context_count < 0) {
  1784. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  1785. return -1;
  1786. }
  1787. } else if (f->version < 3) {
  1788. f->slice_count = get_symbol(c, state, 0);
  1789. } else {
  1790. const uint8_t *p = c->bytestream_end;
  1791. for (f->slice_count = 0; f->slice_count < MAX_SLICES && 3 < p - c->bytestream_start; f->slice_count++) {
  1792. int trailer = 3 + 5*!!f->ec;
  1793. int size = AV_RB24(p-trailer);
  1794. if (size + trailer > p - c->bytestream_start)
  1795. break;
  1796. p -= size + trailer;
  1797. }
  1798. }
  1799. if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0) {
  1800. av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid\n", f->slice_count);
  1801. return -1;
  1802. }
  1803. for (j = 0; j < f->slice_count; j++) {
  1804. FFV1Context *fs = f->slice_context[j];
  1805. fs->ac = f->ac;
  1806. fs->packed_at_lsb = f->packed_at_lsb;
  1807. fs->slice_damaged = 0;
  1808. if (f->version == 2) {
  1809. fs->slice_x = get_symbol(c, state, 0) * f->width ;
  1810. fs->slice_y = get_symbol(c, state, 0) * f->height;
  1811. fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
  1812. fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
  1813. fs->slice_x /= f->num_h_slices;
  1814. fs->slice_y /= f->num_v_slices;
  1815. fs->slice_width = fs->slice_width / f->num_h_slices - fs->slice_x;
  1816. fs->slice_height = fs->slice_height / f->num_v_slices - fs->slice_y;
  1817. if ((unsigned)fs->slice_width > f->width ||
  1818. (unsigned)fs->slice_height > f->height)
  1819. return -1;
  1820. if ((unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width ||
  1821. (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
  1822. return -1;
  1823. }
  1824. for (i = 0; i < f->plane_count; i++) {
  1825. PlaneContext *const p = &fs->plane[i];
  1826. if (f->version == 2) {
  1827. int idx = get_symbol(c, state, 0);
  1828. if (idx > (unsigned)f->quant_table_count) {
  1829. av_log(f->avctx, AV_LOG_ERROR,
  1830. "quant_table_index out of range\n");
  1831. return -1;
  1832. }
  1833. p->quant_table_index = idx;
  1834. memcpy(p->quant_table, f->quant_tables[idx],
  1835. sizeof(p->quant_table));
  1836. context_count = f->context_count[idx];
  1837. } else {
  1838. memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
  1839. }
  1840. if (f->version <= 2) {
  1841. av_assert0(context_count >= 0);
  1842. if (p->context_count < context_count) {
  1843. av_freep(&p->state);
  1844. av_freep(&p->vlc_state);
  1845. }
  1846. p->context_count = context_count;
  1847. }
  1848. }
  1849. }
  1850. return 0;
  1851. }
  1852. static av_cold int decode_init(AVCodecContext *avctx)
  1853. {
  1854. FFV1Context *f = avctx->priv_data;
  1855. common_init(avctx);
  1856. if (avctx->extradata && read_extra_header(f) < 0)
  1857. return -1;
  1858. if (init_slice_contexts(f) < 0)
  1859. return -1;
  1860. return 0;
  1861. }
  1862. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
  1863. {
  1864. const uint8_t *buf = avpkt->data;
  1865. int buf_size = avpkt->size;
  1866. FFV1Context *f = avctx->priv_data;
  1867. RangeCoder *const c = &f->slice_context[0]->c;
  1868. AVFrame *const p = &f->picture;
  1869. int i;
  1870. uint8_t keystate = 128;
  1871. const uint8_t *buf_p;
  1872. AVFrame *picture = data;
  1873. /* release previously stored data */
  1874. if (p->data[0])
  1875. avctx->release_buffer(avctx, p);
  1876. ff_init_range_decoder(c, buf, buf_size);
  1877. ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
  1878. p->pict_type = AV_PICTURE_TYPE_I; // FIXME: I vs. P
  1879. if (get_rac(c, &keystate)) {
  1880. p->key_frame = 1;
  1881. f->key_frame_ok = 0;
  1882. if (read_header(f) < 0)
  1883. return -1;
  1884. f->key_frame_ok = 1;
  1885. } else {
  1886. if (!f->key_frame_ok) {
  1887. av_log(avctx, AV_LOG_ERROR, "Cant decode non keyframe without valid keyframe\n");
  1888. return AVERROR_INVALIDDATA;
  1889. }
  1890. p->key_frame= 0;
  1891. }
  1892. p->reference = 3; //for error concealment
  1893. if (avctx->get_buffer(avctx, p) < 0) {
  1894. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1895. return -1;
  1896. }
  1897. if (avctx->debug&FF_DEBUG_PICT_INFO)
  1898. av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
  1899. f->version, p->key_frame, f->ac, f->ec, f->slice_count, f->avctx->bits_per_raw_sample);
  1900. buf_p = buf + buf_size;
  1901. for (i = f->slice_count - 1; i >= 0; i--) {
  1902. FFV1Context *fs = f->slice_context[i];
  1903. int trailer = 3 + 5*!!f->ec;
  1904. int v;
  1905. if (i || f->version>2) v = AV_RB24(buf_p-trailer)+trailer;
  1906. else v = buf_p - c->bytestream_start;
  1907. if (buf_p - c->bytestream_start < v) {
  1908. av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
  1909. return -1;
  1910. }
  1911. buf_p -= v;
  1912. if (f->ec) {
  1913. unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
  1914. if (crc) {
  1915. int64_t ts = avpkt->pts != AV_NOPTS_VALUE ? avpkt->pts : avpkt->dts;
  1916. av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!", crc);
  1917. if (ts != AV_NOPTS_VALUE && avctx->pkt_timebase.num) {
  1918. av_log(f->avctx, AV_LOG_ERROR, "at %f seconds\n", ts*av_q2d(avctx->pkt_timebase));
  1919. } else if (ts != AV_NOPTS_VALUE) {
  1920. av_log(f->avctx, AV_LOG_ERROR, "at %"PRId64"\n", ts);
  1921. } else {
  1922. av_log(f->avctx, AV_LOG_ERROR, "\n");
  1923. }
  1924. fs->slice_damaged = 1;
  1925. }
  1926. }
  1927. if (i) {
  1928. ff_init_range_decoder(&fs->c, buf_p, v);
  1929. } else
  1930. fs->c.bytestream_end = (uint8_t *)(buf_p + v);
  1931. }
  1932. avctx->execute(avctx, decode_slice, &f->slice_context[0], NULL, f->slice_count, sizeof(void*));
  1933. for (i=f->slice_count-1; i>=0; i--) {
  1934. FFV1Context *fs= f->slice_context[i];
  1935. int j;
  1936. if (fs->slice_damaged && f->last_picture.data[0]) {
  1937. uint8_t *dst[4], *src[4];
  1938. for (j=0; j<4; j++) {
  1939. int sh = (j==1 || j==2) ? f->chroma_h_shift : 0;
  1940. int sv = (j==1 || j==2) ? f->chroma_v_shift : 0;
  1941. dst[j] = f->picture .data[j] + f->picture .linesize[j]*
  1942. (fs->slice_y>>sv) + (fs->slice_x>>sh);
  1943. src[j] = f->last_picture.data[j] + f->last_picture.linesize[j]*
  1944. (fs->slice_y>>sv) + (fs->slice_x>>sh);
  1945. }
  1946. av_image_copy(dst, f->picture.linesize, (const uint8_t **)src, f->last_picture.linesize,
  1947. avctx->pix_fmt, fs->slice_width, fs->slice_height);
  1948. }
  1949. }
  1950. f->picture_number++;
  1951. *picture = *p;
  1952. *data_size = sizeof(AVFrame);
  1953. FFSWAP(AVFrame, f->picture, f->last_picture);
  1954. return buf_size;
  1955. }
  1956. AVCodec ff_ffv1_decoder = {
  1957. .name = "ffv1",
  1958. .type = AVMEDIA_TYPE_VIDEO,
  1959. .id = AV_CODEC_ID_FFV1,
  1960. .priv_data_size = sizeof(FFV1Context),
  1961. .init = decode_init,
  1962. .close = common_end,
  1963. .decode = decode_frame,
  1964. .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/ |
  1965. CODEC_CAP_SLICE_THREADS,
  1966. .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  1967. };
  1968. #if CONFIG_FFV1_ENCODER
  1969. #define OFFSET(x) offsetof(FFV1Context, x)
  1970. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  1971. static const AVOption options[] = {
  1972. { "slicecrc", "Protect slices with CRCs", OFFSET(ec), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, VE},
  1973. {NULL}
  1974. };
  1975. static const AVClass class = {
  1976. .class_name = "ffv1 encoder",
  1977. .item_name = av_default_item_name,
  1978. .option = options,
  1979. .version = LIBAVUTIL_VERSION_INT,
  1980. };
  1981. static const AVCodecDefault ffv1_defaults[] = {
  1982. { "coder", "-1" },
  1983. { NULL },
  1984. };
  1985. AVCodec ff_ffv1_encoder = {
  1986. .name = "ffv1",
  1987. .type = AVMEDIA_TYPE_VIDEO,
  1988. .id = AV_CODEC_ID_FFV1,
  1989. .priv_data_size = sizeof(FFV1Context),
  1990. .init = encode_init,
  1991. .encode2 = encode_frame,
  1992. .close = common_end,
  1993. .capabilities = CODEC_CAP_SLICE_THREADS,
  1994. .defaults = ffv1_defaults,
  1995. .pix_fmts = (const enum AVPixelFormat[]) {
  1996. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV444P,
  1997. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV411P,
  1998. AV_PIX_FMT_YUV410P, AV_PIX_FMT_0RGB32, AV_PIX_FMT_RGB32, AV_PIX_FMT_YUV420P16,
  1999. AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV422P9,
  2000. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  2001. AV_PIX_FMT_GRAY16, AV_PIX_FMT_GRAY8, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  2002. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14,
  2003. AV_PIX_FMT_NONE
  2004. },
  2005. .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  2006. .priv_class = &class,
  2007. };
  2008. #endif