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.

2047 lines
66KB

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