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.

2210 lines
73KB

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