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.

2212 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=0; 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. assert(k<=8);
  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. // assert(g>=0 && b>=0 && r>=0);
  538. // assert(g<256 && b<512 && r<512);
  539. sample[0][0][x]= g;
  540. sample[1][0][x]= b;
  541. sample[2][0][x]= r;
  542. sample[3][0][x]= a;
  543. }
  544. for(p=0; p<3 + s->transparency; p++){
  545. sample[p][0][-1]= sample[p][1][0 ];
  546. sample[p][1][ w]= sample[p][1][w-1];
  547. if (lbd)
  548. encode_line(s, w, sample[p], (p+1)/2, 9);
  549. else
  550. encode_line(s, w, sample[p], (p+1)/2, bits+1);
  551. }
  552. }
  553. }
  554. static void write_quant_table(RangeCoder *c, int16_t *quant_table){
  555. int last=0;
  556. int i;
  557. uint8_t state[CONTEXT_SIZE];
  558. memset(state, 128, sizeof(state));
  559. for(i=1; i<128 ; i++){
  560. if(quant_table[i] != quant_table[i-1]){
  561. put_symbol(c, state, i-last-1, 0);
  562. last= i;
  563. }
  564. }
  565. put_symbol(c, state, i-last-1, 0);
  566. }
  567. static void write_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256]){
  568. int i;
  569. for(i=0; i<5; i++)
  570. write_quant_table(c, quant_table[i]);
  571. }
  572. static void write_header(FFV1Context *f){
  573. uint8_t state[CONTEXT_SIZE];
  574. int i, j;
  575. RangeCoder * const c= &f->slice_context[0]->c;
  576. memset(state, 128, sizeof(state));
  577. if(f->version < 2){
  578. put_symbol(c, state, f->version, 0);
  579. put_symbol(c, state, f->ac, 0);
  580. if(f->ac>1){
  581. for(i=1; i<256; i++){
  582. put_symbol(c, state, f->state_transition[i] - c->one_state[i], 1);
  583. }
  584. }
  585. put_symbol(c, state, f->colorspace, 0); //YUV cs type
  586. if(f->version>0)
  587. put_symbol(c, state, f->bits_per_raw_sample, 0);
  588. put_rac(c, state, f->chroma_planes);
  589. put_symbol(c, state, f->chroma_h_shift, 0);
  590. put_symbol(c, state, f->chroma_v_shift, 0);
  591. put_rac(c, state, f->transparency);
  592. write_quant_tables(c, f->quant_table);
  593. }else if(f->version < 3){
  594. put_symbol(c, state, f->slice_count, 0);
  595. for(i=0; i<f->slice_count; i++){
  596. FFV1Context *fs= f->slice_context[i];
  597. put_symbol(c, state, (fs->slice_x +1)*f->num_h_slices / f->width , 0);
  598. put_symbol(c, state, (fs->slice_y +1)*f->num_v_slices / f->height , 0);
  599. put_symbol(c, state, (fs->slice_width +1)*f->num_h_slices / f->width -1, 0);
  600. put_symbol(c, state, (fs->slice_height+1)*f->num_v_slices / f->height-1, 0);
  601. for(j=0; j<f->plane_count; j++){
  602. put_symbol(c, state, f->plane[j].quant_table_index, 0);
  603. av_assert0(f->plane[j].quant_table_index == f->avctx->context_model);
  604. }
  605. }
  606. }
  607. }
  608. #endif /* CONFIG_FFV1_ENCODER */
  609. static av_cold int common_init(AVCodecContext *avctx){
  610. FFV1Context *s = avctx->priv_data;
  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. assert(s->width && s->height);
  618. //defaults
  619. s->num_h_slices=1;
  620. s->num_v_slices=1;
  621. return 0;
  622. }
  623. static int init_slice_state(FFV1Context *f, FFV1Context *fs){
  624. int j;
  625. fs->plane_count= f->plane_count;
  626. fs->transparency= f->transparency;
  627. for(j=0; j<f->plane_count; j++){
  628. PlaneContext * const p= &fs->plane[j];
  629. if(fs->ac){
  630. if(!p-> state) p-> state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  631. if(!p-> state)
  632. return AVERROR(ENOMEM);
  633. }else{
  634. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  635. if(!p->vlc_state)
  636. return AVERROR(ENOMEM);
  637. }
  638. }
  639. if (fs->ac>1){
  640. //FIXME only redo if state_transition changed
  641. for(j=1; j<256; j++){
  642. fs->c.one_state [ j]= f->state_transition[j];
  643. fs->c.zero_state[256-j]= 256-fs->c.one_state [j];
  644. }
  645. }
  646. return 0;
  647. }
  648. static int init_slices_state(FFV1Context *f){
  649. int i;
  650. for(i=0; i<f->slice_count; i++){
  651. FFV1Context *fs= f->slice_context[i];
  652. if(init_slice_state(f, fs) < 0)
  653. return -1;
  654. }
  655. return 0;
  656. }
  657. static av_cold int init_slice_contexts(FFV1Context *f){
  658. int i;
  659. f->slice_count= f->num_h_slices * f->num_v_slices;
  660. for(i=0; i<f->slice_count; i++){
  661. FFV1Context *fs= av_mallocz(sizeof(*fs));
  662. int sx= i % f->num_h_slices;
  663. int sy= i / f->num_h_slices;
  664. int sxs= f->avctx->width * sx / f->num_h_slices;
  665. int sxe= f->avctx->width *(sx+1) / f->num_h_slices;
  666. int sys= f->avctx->height* sy / f->num_v_slices;
  667. int sye= f->avctx->height*(sy+1) / f->num_v_slices;
  668. f->slice_context[i]= fs;
  669. memcpy(fs, f, sizeof(*fs));
  670. memset(fs->rc_stat2, 0, sizeof(fs->rc_stat2));
  671. fs->slice_width = sxe - sxs;
  672. fs->slice_height= sye - sys;
  673. fs->slice_x = sxs;
  674. fs->slice_y = sys;
  675. fs->sample_buffer = av_malloc(3*4 * (fs->width+6) * sizeof(*fs->sample_buffer));
  676. if (!fs->sample_buffer)
  677. return AVERROR(ENOMEM);
  678. }
  679. return 0;
  680. }
  681. static int allocate_initial_states(FFV1Context *f){
  682. int i;
  683. for(i=0; i<f->quant_table_count; i++){
  684. f->initial_states[i]= av_malloc(f->context_count[i]*sizeof(*f->initial_states[i]));
  685. if(!f->initial_states[i])
  686. return AVERROR(ENOMEM);
  687. memset(f->initial_states[i], 128, f->context_count[i]*sizeof(*f->initial_states[i]));
  688. }
  689. return 0;
  690. }
  691. #if CONFIG_FFV1_ENCODER
  692. static int write_extra_header(FFV1Context *f){
  693. RangeCoder * const c= &f->c;
  694. uint8_t state[CONTEXT_SIZE];
  695. int i, j, k;
  696. uint8_t state2[32][CONTEXT_SIZE];
  697. unsigned v;
  698. memset(state2, 128, sizeof(state2));
  699. memset(state, 128, sizeof(state));
  700. f->avctx->extradata= av_malloc(f->avctx->extradata_size= 10000 + (11*11*5*5*5+11*11*11)*32);
  701. ff_init_range_encoder(c, f->avctx->extradata, f->avctx->extradata_size);
  702. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  703. put_symbol(c, state, f->version, 0);
  704. if(f->version > 2) {
  705. if(f->version == 3)
  706. f->minor_version = 2;
  707. put_symbol(c, state, f->minor_version, 0);
  708. }
  709. put_symbol(c, state, f->ac, 0);
  710. if(f->ac>1){
  711. for(i=1; i<256; i++){
  712. put_symbol(c, state, f->state_transition[i] - c->one_state[i], 1);
  713. }
  714. }
  715. put_symbol(c, state, f->colorspace, 0); //YUV cs type
  716. put_symbol(c, state, f->bits_per_raw_sample, 0);
  717. put_rac(c, state, f->chroma_planes);
  718. put_symbol(c, state, f->chroma_h_shift, 0);
  719. put_symbol(c, state, f->chroma_v_shift, 0);
  720. put_rac(c, state, f->transparency);
  721. put_symbol(c, state, f->num_h_slices-1, 0);
  722. put_symbol(c, state, f->num_v_slices-1, 0);
  723. put_symbol(c, state, f->quant_table_count, 0);
  724. for(i=0; i<f->quant_table_count; i++)
  725. write_quant_tables(c, f->quant_tables[i]);
  726. for(i=0; i<f->quant_table_count; i++){
  727. for(j=0; j<f->context_count[i]*CONTEXT_SIZE; j++)
  728. if(f->initial_states[i] && f->initial_states[i][0][j] != 128)
  729. break;
  730. if(j<f->context_count[i]*CONTEXT_SIZE){
  731. put_rac(c, state, 1);
  732. for(j=0; j<f->context_count[i]; j++){
  733. for(k=0; k<CONTEXT_SIZE; k++){
  734. int pred= j ? f->initial_states[i][j-1][k] : 128;
  735. put_symbol(c, state2[k], (int8_t)(f->initial_states[i][j][k]-pred), 1);
  736. }
  737. }
  738. }else{
  739. put_rac(c, state, 0);
  740. }
  741. }
  742. if(f->version > 2){
  743. put_symbol(c, state, f->ec, 0);
  744. }
  745. f->avctx->extradata_size= ff_rac_terminate(c);
  746. v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, f->avctx->extradata, f->avctx->extradata_size);
  747. AV_WL32(f->avctx->extradata + f->avctx->extradata_size, v);
  748. f->avctx->extradata_size += 4;
  749. return 0;
  750. }
  751. static int sort_stt(FFV1Context *s, uint8_t stt[256]){
  752. int i,i2,changed,print=0;
  753. do{
  754. changed=0;
  755. for(i=12; i<244; i++){
  756. for(i2=i+1; i2<245 && i2<i+4; i2++){
  757. #define COST(old, new) \
  758. s->rc_stat[old][0]*-log2((256-(new))/256.0)\
  759. +s->rc_stat[old][1]*-log2( (new) /256.0)
  760. #define COST2(old, new) \
  761. COST(old, new)\
  762. +COST(256-(old), 256-(new))
  763. double size0= COST2(i, i ) + COST2(i2, i2);
  764. double sizeX= COST2(i, i2) + COST2(i2, i );
  765. if(sizeX < size0 && i!=128 && i2!=128){
  766. int j;
  767. FFSWAP(int, stt[ i], stt[ i2]);
  768. FFSWAP(int, s->rc_stat[i ][0],s->rc_stat[ i2][0]);
  769. FFSWAP(int, s->rc_stat[i ][1],s->rc_stat[ i2][1]);
  770. if(i != 256-i2){
  771. FFSWAP(int, stt[256-i], stt[256-i2]);
  772. FFSWAP(int, s->rc_stat[256-i][0],s->rc_stat[256-i2][0]);
  773. FFSWAP(int, s->rc_stat[256-i][1],s->rc_stat[256-i2][1]);
  774. }
  775. for(j=1; j<256; j++){
  776. if (stt[j] == i ) stt[j] = i2;
  777. else if(stt[j] == i2) stt[j] = i ;
  778. if(i != 256-i2){
  779. if (stt[256-j] == 256-i ) stt[256-j] = 256-i2;
  780. else if(stt[256-j] == 256-i2) stt[256-j] = 256-i ;
  781. }
  782. }
  783. print=changed=1;
  784. }
  785. }
  786. }
  787. }while(changed);
  788. return print;
  789. }
  790. static av_cold int encode_init(AVCodecContext *avctx)
  791. {
  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 PIX_FMT_YUV444P9:
  812. case PIX_FMT_YUV422P9:
  813. case PIX_FMT_YUV420P9:
  814. if (!avctx->bits_per_raw_sample)
  815. s->bits_per_raw_sample = 9;
  816. case PIX_FMT_YUV444P10:
  817. case PIX_FMT_YUV420P10:
  818. case 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 PIX_FMT_GRAY16:
  823. case PIX_FMT_YUV444P16:
  824. case PIX_FMT_YUV422P16:
  825. case 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 PIX_FMT_GRAY8:
  845. case PIX_FMT_YUV444P:
  846. case PIX_FMT_YUV440P:
  847. case PIX_FMT_YUV422P:
  848. case PIX_FMT_YUV420P:
  849. case PIX_FMT_YUV411P:
  850. case PIX_FMT_YUV410P:
  851. s->chroma_planes= av_pix_fmt_descriptors[avctx->pix_fmt].nb_components < 3 ? 0 : 1;
  852. s->colorspace= 0;
  853. break;
  854. case PIX_FMT_YUVA444P:
  855. case PIX_FMT_YUVA422P:
  856. case PIX_FMT_YUVA420P:
  857. s->chroma_planes= 1;
  858. s->colorspace= 0;
  859. s->transparency= 1;
  860. break;
  861. case PIX_FMT_RGB32:
  862. s->colorspace= 1;
  863. s->transparency= 1;
  864. break;
  865. case PIX_FMT_0RGB32:
  866. s->colorspace= 1;
  867. break;
  868. case PIX_FMT_GBRP9:
  869. if (!avctx->bits_per_raw_sample)
  870. s->bits_per_raw_sample = 9;
  871. case PIX_FMT_GBRP10:
  872. if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
  873. s->bits_per_raw_sample = 10;
  874. case PIX_FMT_GBRP12:
  875. if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
  876. s->bits_per_raw_sample = 12;
  877. case 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, (int[]){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. // assert(g>=0 && b>=0 && r>=0);
  1371. // assert(g<256 && b<512 && r<512);
  1372. b -= offset;
  1373. r -= offset;
  1374. g -= (b + r)>>2;
  1375. b += g;
  1376. r += g;
  1377. if(lbd)
  1378. *((uint32_t*)(src[0] + x*4 + stride[0]*y))= b + (g<<8) + (r<<16) + (a<<24);
  1379. else{
  1380. *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = b;
  1381. *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = g;
  1382. *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
  1383. }
  1384. }
  1385. }
  1386. }
  1387. static int decode_slice_header(FFV1Context *f, FFV1Context *fs){
  1388. RangeCoder *c = &fs->c;
  1389. uint8_t state[CONTEXT_SIZE];
  1390. unsigned ps, i, context_count;
  1391. memset(state, 128, sizeof(state));
  1392. av_assert0(f->version > 2);
  1393. fs->slice_x = get_symbol(c, state, 0) *f->width ;
  1394. fs->slice_y = get_symbol(c, state, 0) *f->height;
  1395. fs->slice_width =(get_symbol(c, state, 0)+1)*f->width + fs->slice_x;
  1396. fs->slice_height=(get_symbol(c, state, 0)+1)*f->height + fs->slice_y;
  1397. fs->slice_x /= f->num_h_slices;
  1398. fs->slice_y /= f->num_v_slices;
  1399. fs->slice_width = fs->slice_width /f->num_h_slices - fs->slice_x;
  1400. fs->slice_height = fs->slice_height/f->num_v_slices - fs->slice_y;
  1401. if((unsigned)fs->slice_width > f->width || (unsigned)fs->slice_height > f->height)
  1402. return -1;
  1403. if( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
  1404. || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
  1405. return -1;
  1406. for(i=0; i<f->plane_count; i++){
  1407. PlaneContext * const p= &fs->plane[i];
  1408. int idx=get_symbol(c, state, 0);
  1409. if(idx > (unsigned)f->quant_table_count){
  1410. av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
  1411. return -1;
  1412. }
  1413. p->quant_table_index= idx;
  1414. memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
  1415. context_count= f->context_count[idx];
  1416. if(p->context_count < context_count){
  1417. av_freep(&p->state);
  1418. av_freep(&p->vlc_state);
  1419. }
  1420. p->context_count= context_count;
  1421. }
  1422. ps = get_symbol(c, state, 0);
  1423. if(ps==1){
  1424. f->picture.interlaced_frame = 1;
  1425. f->picture.top_field_first = 1;
  1426. } else if(ps==2){
  1427. f->picture.interlaced_frame = 1;
  1428. f->picture.top_field_first = 0;
  1429. } else if(ps==3){
  1430. f->picture.interlaced_frame = 0;
  1431. }
  1432. f->picture.sample_aspect_ratio.num = get_symbol(c, state, 0);
  1433. f->picture.sample_aspect_ratio.den = get_symbol(c, state, 0);
  1434. return 0;
  1435. }
  1436. static int decode_slice(AVCodecContext *c, void *arg){
  1437. FFV1Context *fs= *(void**)arg;
  1438. FFV1Context *f= fs->avctx->priv_data;
  1439. int width, height, x, y;
  1440. const int ps= (c->bits_per_raw_sample>8)+1;
  1441. AVFrame * const p= &f->picture;
  1442. if(f->version > 2){
  1443. if(init_slice_state(f, fs) < 0)
  1444. return AVERROR(ENOMEM);
  1445. if(decode_slice_header(f, fs) < 0) {
  1446. fs->slice_damaged = 1;
  1447. return AVERROR_INVALIDDATA;
  1448. }
  1449. }
  1450. if(init_slice_state(f, fs) < 0)
  1451. return AVERROR(ENOMEM);
  1452. if(f->picture.key_frame)
  1453. clear_slice_state(f, fs);
  1454. width = fs->slice_width;
  1455. height= fs->slice_height;
  1456. x= fs->slice_x;
  1457. y= fs->slice_y;
  1458. if(!fs->ac){
  1459. if (f->version == 3 && f->minor_version > 1 || f->version > 3)
  1460. get_rac(&fs->c, (int[]){129});
  1461. fs->ac_byte_count = f->version > 2 || (!x&&!y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0;
  1462. init_get_bits(&fs->gb,
  1463. fs->c.bytestream_start + fs->ac_byte_count,
  1464. (fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count) * 8);
  1465. }
  1466. av_assert1(width && height);
  1467. if(f->colorspace==0){
  1468. const int chroma_width = -((-width )>>f->chroma_h_shift);
  1469. const int chroma_height= -((-height)>>f->chroma_v_shift);
  1470. const int cx= x>>f->chroma_h_shift;
  1471. const int cy= y>>f->chroma_v_shift;
  1472. decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0);
  1473. if (f->chroma_planes){
  1474. decode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
  1475. decode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1);
  1476. }
  1477. if (fs->transparency)
  1478. decode_plane(fs, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2);
  1479. }else{
  1480. uint8_t *planes[3] = {p->data[0] + ps*x + y*p->linesize[0],
  1481. p->data[1] + ps*x + y*p->linesize[1],
  1482. p->data[2] + ps*x + y*p->linesize[2]};
  1483. decode_rgb_frame(fs, planes, width, height, p->linesize);
  1484. }
  1485. if(fs->ac && f->version > 2) {
  1486. int v;
  1487. get_rac(&fs->c, (int[]){129});
  1488. v = fs->c.bytestream_end - fs->c.bytestream - 2 - 5*f->ec;
  1489. if(v) {
  1490. av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n", v);
  1491. fs->slice_damaged = 1;
  1492. }
  1493. }
  1494. emms_c();
  1495. return 0;
  1496. }
  1497. static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){
  1498. int v;
  1499. int i=0;
  1500. uint8_t state[CONTEXT_SIZE];
  1501. memset(state, 128, sizeof(state));
  1502. for(v=0; i<128 ; v++){
  1503. unsigned len= get_symbol(c, state, 0) + 1;
  1504. if(len > 128 - i) return -1;
  1505. while(len--){
  1506. quant_table[i] = scale*v;
  1507. i++;
  1508. }
  1509. }
  1510. for(i=1; i<128; i++){
  1511. quant_table[256-i]= -quant_table[i];
  1512. }
  1513. quant_table[128]= -quant_table[127];
  1514. return 2*v - 1;
  1515. }
  1516. static int read_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256]){
  1517. int i;
  1518. int context_count=1;
  1519. for(i=0; i<5; i++){
  1520. context_count*= read_quant_table(c, quant_table[i], context_count);
  1521. if(context_count > 32768U){
  1522. return -1;
  1523. }
  1524. }
  1525. return (context_count+1)/2;
  1526. }
  1527. static int read_extra_header(FFV1Context *f){
  1528. RangeCoder * const c= &f->c;
  1529. uint8_t state[CONTEXT_SIZE];
  1530. int i, j, k;
  1531. uint8_t state2[32][CONTEXT_SIZE];
  1532. memset(state2, 128, sizeof(state2));
  1533. memset(state, 128, sizeof(state));
  1534. ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
  1535. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  1536. f->version= get_symbol(c, state, 0);
  1537. if(f->version > 2) {
  1538. c->bytestream_end -= 4;
  1539. f->minor_version= get_symbol(c, state, 0);
  1540. }
  1541. f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
  1542. if(f->ac>1){
  1543. for(i=1; i<256; i++){
  1544. f->state_transition[i]= get_symbol(c, state, 1) + c->one_state[i];
  1545. }
  1546. }
  1547. f->colorspace= get_symbol(c, state, 0); //YUV cs type
  1548. f->avctx->bits_per_raw_sample= get_symbol(c, state, 0);
  1549. f->chroma_planes= get_rac(c, state);
  1550. f->chroma_h_shift= get_symbol(c, state, 0);
  1551. f->chroma_v_shift= get_symbol(c, state, 0);
  1552. f->transparency= get_rac(c, state);
  1553. f->plane_count= 2 + f->transparency;
  1554. f->num_h_slices= 1 + get_symbol(c, state, 0);
  1555. f->num_v_slices= 1 + get_symbol(c, state, 0);
  1556. if(f->num_h_slices > (unsigned)f->width || f->num_v_slices > (unsigned)f->height){
  1557. av_log(f->avctx, AV_LOG_ERROR, "too many slices\n");
  1558. return -1;
  1559. }
  1560. f->quant_table_count= get_symbol(c, state, 0);
  1561. if(f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
  1562. return -1;
  1563. for(i=0; i<f->quant_table_count; i++){
  1564. if((f->context_count[i]= read_quant_tables(c, f->quant_tables[i])) < 0){
  1565. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  1566. return -1;
  1567. }
  1568. }
  1569. if(allocate_initial_states(f) < 0)
  1570. return AVERROR(ENOMEM);
  1571. for(i=0; i<f->quant_table_count; i++){
  1572. if(get_rac(c, state)){
  1573. for(j=0; j<f->context_count[i]; j++){
  1574. for(k=0; k<CONTEXT_SIZE; k++){
  1575. int pred= j ? f->initial_states[i][j-1][k] : 128;
  1576. f->initial_states[i][j][k]= (pred+get_symbol(c, state2[k], 1))&0xFF;
  1577. }
  1578. }
  1579. }
  1580. }
  1581. if(f->version > 2){
  1582. f->ec = get_symbol(c, state, 0);
  1583. }
  1584. if(f->version > 2){
  1585. unsigned v;
  1586. v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, f->avctx->extradata, f->avctx->extradata_size);
  1587. if(v){
  1588. av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
  1589. return AVERROR_INVALIDDATA;
  1590. }
  1591. }
  1592. return 0;
  1593. }
  1594. static int read_header(FFV1Context *f){
  1595. uint8_t state[CONTEXT_SIZE];
  1596. int i, j, context_count = -1; //-1 to avoid warning
  1597. RangeCoder * const c= &f->slice_context[0]->c;
  1598. memset(state, 128, sizeof(state));
  1599. if(f->version < 2){
  1600. unsigned v= get_symbol(c, state, 0);
  1601. if(v >= 2){
  1602. av_log(f->avctx, AV_LOG_ERROR, "invalid version %d in ver01 header\n", v);
  1603. return AVERROR_INVALIDDATA;
  1604. }
  1605. f->version = v;
  1606. f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
  1607. if(f->ac>1){
  1608. for(i=1; i<256; i++){
  1609. f->state_transition[i]= get_symbol(c, state, 1) + c->one_state[i];
  1610. }
  1611. }
  1612. f->colorspace= get_symbol(c, state, 0); //YUV cs type
  1613. if(f->version>0)
  1614. f->avctx->bits_per_raw_sample= get_symbol(c, state, 0);
  1615. f->chroma_planes= get_rac(c, state);
  1616. f->chroma_h_shift= get_symbol(c, state, 0);
  1617. f->chroma_v_shift= get_symbol(c, state, 0);
  1618. f->transparency= get_rac(c, state);
  1619. f->plane_count= 2 + f->transparency;
  1620. }
  1621. if(f->colorspace==0){
  1622. if(!f->transparency && !f->chroma_planes){
  1623. if (f->avctx->bits_per_raw_sample<=8)
  1624. f->avctx->pix_fmt= PIX_FMT_GRAY8;
  1625. else
  1626. f->avctx->pix_fmt= PIX_FMT_GRAY16;
  1627. }else if(f->avctx->bits_per_raw_sample<=8 && !f->transparency){
  1628. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  1629. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
  1630. case 0x01: f->avctx->pix_fmt= PIX_FMT_YUV440P; break;
  1631. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
  1632. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
  1633. case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
  1634. case 0x22: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
  1635. default:
  1636. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  1637. return -1;
  1638. }
  1639. }else if(f->avctx->bits_per_raw_sample<=8 && f->transparency){
  1640. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  1641. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUVA444P; break;
  1642. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUVA422P; break;
  1643. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUVA420P; break;
  1644. default:
  1645. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  1646. return -1;
  1647. }
  1648. }else if(f->avctx->bits_per_raw_sample==9) {
  1649. f->packed_at_lsb=1;
  1650. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  1651. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P9; break;
  1652. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P9; break;
  1653. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P9; break;
  1654. default:
  1655. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  1656. return -1;
  1657. }
  1658. }else if(f->avctx->bits_per_raw_sample==10) {
  1659. f->packed_at_lsb=1;
  1660. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  1661. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P10; break;
  1662. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P10; break;
  1663. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P10; break;
  1664. default:
  1665. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  1666. return -1;
  1667. }
  1668. }else {
  1669. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  1670. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P16; break;
  1671. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P16; break;
  1672. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P16; break;
  1673. default:
  1674. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  1675. return -1;
  1676. }
  1677. }
  1678. }else if(f->colorspace==1){
  1679. if(f->chroma_h_shift || f->chroma_v_shift){
  1680. av_log(f->avctx, AV_LOG_ERROR, "chroma subsampling not supported in this colorspace\n");
  1681. return -1;
  1682. }
  1683. if(f->avctx->bits_per_raw_sample==9)
  1684. f->avctx->pix_fmt= PIX_FMT_GBRP9;
  1685. else if(f->avctx->bits_per_raw_sample==10)
  1686. f->avctx->pix_fmt= PIX_FMT_GBRP10;
  1687. else if(f->avctx->bits_per_raw_sample==12)
  1688. f->avctx->pix_fmt= PIX_FMT_GBRP12;
  1689. else if(f->avctx->bits_per_raw_sample==14)
  1690. f->avctx->pix_fmt= PIX_FMT_GBRP14;
  1691. else
  1692. if(f->transparency) f->avctx->pix_fmt= PIX_FMT_RGB32;
  1693. else f->avctx->pix_fmt= PIX_FMT_0RGB32;
  1694. }else{
  1695. av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
  1696. return -1;
  1697. }
  1698. av_dlog(f->avctx, "%d %d %d\n",
  1699. f->chroma_h_shift, f->chroma_v_shift, f->avctx->pix_fmt);
  1700. if(f->version < 2){
  1701. context_count= read_quant_tables(c, f->quant_table);
  1702. if(context_count < 0){
  1703. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  1704. return -1;
  1705. }
  1706. }else if(f->version < 3){
  1707. f->slice_count= get_symbol(c, state, 0);
  1708. }else{
  1709. const uint8_t *p= c->bytestream_end;
  1710. for(f->slice_count = 0; f->slice_count < MAX_SLICES && 3 < p - c->bytestream_start; f->slice_count++){
  1711. int trailer = 3 + 5*!!f->ec;
  1712. int size = AV_RB24(p-trailer);
  1713. if(size + trailer > p - c->bytestream_start)
  1714. break;
  1715. p -= size + trailer;
  1716. }
  1717. }
  1718. if(f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0){
  1719. av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid\n", f->slice_count);
  1720. return -1;
  1721. }
  1722. for(j=0; j<f->slice_count; j++){
  1723. FFV1Context *fs= f->slice_context[j];
  1724. fs->ac= f->ac;
  1725. fs->packed_at_lsb= f->packed_at_lsb;
  1726. fs->slice_damaged = 0;
  1727. if(f->version == 2){
  1728. fs->slice_x = get_symbol(c, state, 0) *f->width ;
  1729. fs->slice_y = get_symbol(c, state, 0) *f->height;
  1730. fs->slice_width =(get_symbol(c, state, 0)+1)*f->width + fs->slice_x;
  1731. fs->slice_height=(get_symbol(c, state, 0)+1)*f->height + fs->slice_y;
  1732. fs->slice_x /= f->num_h_slices;
  1733. fs->slice_y /= f->num_v_slices;
  1734. fs->slice_width = fs->slice_width /f->num_h_slices - fs->slice_x;
  1735. fs->slice_height = fs->slice_height/f->num_v_slices - fs->slice_y;
  1736. if((unsigned)fs->slice_width > f->width || (unsigned)fs->slice_height > f->height)
  1737. return -1;
  1738. if( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
  1739. || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
  1740. return -1;
  1741. }
  1742. for(i=0; i<f->plane_count; i++){
  1743. PlaneContext * const p= &fs->plane[i];
  1744. if(f->version == 2){
  1745. int idx=get_symbol(c, state, 0);
  1746. if(idx > (unsigned)f->quant_table_count){
  1747. av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
  1748. return -1;
  1749. }
  1750. p->quant_table_index= idx;
  1751. memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
  1752. context_count= f->context_count[idx];
  1753. }else{
  1754. memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
  1755. }
  1756. if(f->version <= 2){
  1757. av_assert0(context_count>=0);
  1758. if(p->context_count < context_count){
  1759. av_freep(&p->state);
  1760. av_freep(&p->vlc_state);
  1761. }
  1762. p->context_count= context_count;
  1763. }
  1764. }
  1765. }
  1766. return 0;
  1767. }
  1768. static av_cold int decode_init(AVCodecContext *avctx)
  1769. {
  1770. FFV1Context *f = avctx->priv_data;
  1771. common_init(avctx);
  1772. if(avctx->extradata && read_extra_header(f) < 0)
  1773. return -1;
  1774. if(init_slice_contexts(f) < 0)
  1775. return -1;
  1776. return 0;
  1777. }
  1778. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt){
  1779. const uint8_t *buf = avpkt->data;
  1780. int buf_size = avpkt->size;
  1781. FFV1Context *f = avctx->priv_data;
  1782. RangeCoder * const c= &f->slice_context[0]->c;
  1783. AVFrame * const p= &f->picture;
  1784. int i;
  1785. uint8_t keystate= 128;
  1786. const uint8_t *buf_p;
  1787. AVFrame *picture = data;
  1788. /* release previously stored data */
  1789. if (p->data[0])
  1790. avctx->release_buffer(avctx, p);
  1791. ff_init_range_decoder(c, buf, buf_size);
  1792. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  1793. p->pict_type= AV_PICTURE_TYPE_I; //FIXME I vs. P
  1794. if(get_rac(c, &keystate)){
  1795. p->key_frame= 1;
  1796. f->key_frame_ok = 0;
  1797. if(read_header(f) < 0)
  1798. return -1;
  1799. f->key_frame_ok = 1;
  1800. }else{
  1801. if (!f->key_frame_ok) {
  1802. av_log(avctx, AV_LOG_ERROR, "Cant decode non keyframe without valid keyframe\n");
  1803. return AVERROR_INVALIDDATA;
  1804. }
  1805. p->key_frame= 0;
  1806. }
  1807. p->reference= 3; //for error concealment
  1808. if(avctx->get_buffer(avctx, p) < 0){
  1809. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1810. return -1;
  1811. }
  1812. if(avctx->debug&FF_DEBUG_PICT_INFO)
  1813. av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
  1814. f->version, p->key_frame, f->ac, f->ec, f->slice_count, f->avctx->bits_per_raw_sample);
  1815. buf_p= buf + buf_size;
  1816. for(i=f->slice_count-1; i>=0; i--){
  1817. FFV1Context *fs= f->slice_context[i];
  1818. int trailer = 3 + 5*!!f->ec;
  1819. int v;
  1820. if(i || f->version>2) v = AV_RB24(buf_p-trailer)+trailer;
  1821. else v = buf_p - c->bytestream_start;
  1822. if(buf_p - c->bytestream_start < v){
  1823. av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
  1824. return -1;
  1825. }
  1826. buf_p -= v;
  1827. if(f->ec){
  1828. unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
  1829. if(crc){
  1830. int64_t ts = avpkt->pts != AV_NOPTS_VALUE ? avpkt->pts : avpkt->dts;
  1831. av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!", crc);
  1832. if(ts != AV_NOPTS_VALUE && avctx->pkt_timebase.num) {
  1833. av_log(f->avctx, AV_LOG_ERROR, "at %f seconds\n",ts*av_q2d(avctx->pkt_timebase));
  1834. } else if(ts != AV_NOPTS_VALUE) {
  1835. av_log(f->avctx, AV_LOG_ERROR, "at %"PRId64"\n", ts);
  1836. } else {
  1837. av_log(f->avctx, AV_LOG_ERROR, "\n");
  1838. }
  1839. fs->slice_damaged = 1;
  1840. }
  1841. }
  1842. if(i){
  1843. ff_init_range_decoder(&fs->c, buf_p, v);
  1844. }else
  1845. fs->c.bytestream_end = (uint8_t *)(buf_p + v);
  1846. }
  1847. avctx->execute(avctx, decode_slice, &f->slice_context[0], NULL, f->slice_count, sizeof(void*));
  1848. for(i=f->slice_count-1; i>=0; i--){
  1849. FFV1Context *fs= f->slice_context[i];
  1850. int j;
  1851. if(fs->slice_damaged && f->last_picture.data[0]){
  1852. uint8_t *dst[4], *src[4];
  1853. for(j=0; j<4; j++){
  1854. int sh = (j==1 || j==2) ? f->chroma_h_shift : 0;
  1855. int sv = (j==1 || j==2) ? f->chroma_v_shift : 0;
  1856. dst[j] = f->picture .data[j] + f->picture .linesize[j]*
  1857. (fs->slice_y>>sv) + (fs->slice_x>>sh);
  1858. src[j] = f->last_picture.data[j] + f->last_picture.linesize[j]*
  1859. (fs->slice_y>>sv) + (fs->slice_x>>sh);
  1860. }
  1861. av_image_copy(dst, f->picture.linesize, (const uint8_t **)src, f->last_picture.linesize,
  1862. avctx->pix_fmt, fs->slice_width, fs->slice_height);
  1863. }
  1864. }
  1865. f->picture_number++;
  1866. *picture= *p;
  1867. *data_size = sizeof(AVFrame);
  1868. FFSWAP(AVFrame, f->picture, f->last_picture);
  1869. return buf_size;
  1870. }
  1871. AVCodec ff_ffv1_decoder = {
  1872. .name = "ffv1",
  1873. .type = AVMEDIA_TYPE_VIDEO,
  1874. .id = AV_CODEC_ID_FFV1,
  1875. .priv_data_size = sizeof(FFV1Context),
  1876. .init = decode_init,
  1877. .close = common_end,
  1878. .decode = decode_frame,
  1879. .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/ |
  1880. CODEC_CAP_SLICE_THREADS,
  1881. .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  1882. };
  1883. #if CONFIG_FFV1_ENCODER
  1884. #define OFFSET(x) offsetof(FFV1Context, x)
  1885. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  1886. static const AVOption options[] = {
  1887. { "slicecrc", "Protect slices with CRCs", OFFSET(ec), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, VE},
  1888. {NULL}
  1889. };
  1890. static const AVClass class = {
  1891. .class_name = "ffv1 encoder",
  1892. .item_name = av_default_item_name,
  1893. .option = options,
  1894. .version = LIBAVUTIL_VERSION_INT,
  1895. };
  1896. static const AVCodecDefault ffv1_defaults[] = {
  1897. { "coder", "-1" },
  1898. { NULL },
  1899. };
  1900. AVCodec ff_ffv1_encoder = {
  1901. .name = "ffv1",
  1902. .type = AVMEDIA_TYPE_VIDEO,
  1903. .id = AV_CODEC_ID_FFV1,
  1904. .priv_data_size = sizeof(FFV1Context),
  1905. .init = encode_init,
  1906. .encode2 = encode_frame,
  1907. .close = common_end,
  1908. .capabilities = CODEC_CAP_SLICE_THREADS,
  1909. .defaults = ffv1_defaults,
  1910. .pix_fmts = (const enum PixelFormat[]){
  1911. PIX_FMT_YUV420P, PIX_FMT_YUVA420P, PIX_FMT_YUVA422P, PIX_FMT_YUV444P,
  1912. PIX_FMT_YUVA444P, PIX_FMT_YUV440P, PIX_FMT_YUV422P, PIX_FMT_YUV411P,
  1913. PIX_FMT_YUV410P, PIX_FMT_0RGB32, PIX_FMT_RGB32, PIX_FMT_YUV420P16,
  1914. PIX_FMT_YUV422P16, PIX_FMT_YUV444P16, PIX_FMT_YUV444P9, PIX_FMT_YUV422P9,
  1915. PIX_FMT_YUV420P9, PIX_FMT_YUV420P10, PIX_FMT_YUV422P10, PIX_FMT_YUV444P10,
  1916. PIX_FMT_GRAY16, PIX_FMT_GRAY8, PIX_FMT_GBRP9, PIX_FMT_GBRP10,
  1917. PIX_FMT_GBRP12, PIX_FMT_GBRP14,
  1918. PIX_FMT_NONE
  1919. },
  1920. .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  1921. .priv_class = &class,
  1922. };
  1923. #endif