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.

2053 lines
67KB

  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 if(f->version < 3){
  573. put_symbol(c, state, f->slice_count, 0);
  574. for(i=0; i<f->slice_count; i++){
  575. FFV1Context *fs= f->slice_context[i];
  576. put_symbol(c, state, (fs->slice_x +1)*f->num_h_slices / f->width , 0);
  577. put_symbol(c, state, (fs->slice_y +1)*f->num_v_slices / f->height , 0);
  578. put_symbol(c, state, (fs->slice_width +1)*f->num_h_slices / f->width -1, 0);
  579. put_symbol(c, state, (fs->slice_height+1)*f->num_v_slices / f->height-1, 0);
  580. for(j=0; j<f->plane_count; j++){
  581. put_symbol(c, state, f->plane[j].quant_table_index, 0);
  582. av_assert0(f->plane[j].quant_table_index == f->avctx->context_model);
  583. }
  584. }
  585. }
  586. }
  587. #endif /* CONFIG_FFV1_ENCODER */
  588. static av_cold int common_init(AVCodecContext *avctx){
  589. FFV1Context *s = avctx->priv_data;
  590. s->avctx= avctx;
  591. s->flags= avctx->flags;
  592. avcodec_get_frame_defaults(&s->picture);
  593. ff_dsputil_init(&s->dsp, avctx);
  594. s->width = avctx->width;
  595. s->height= avctx->height;
  596. assert(s->width && s->height);
  597. //defaults
  598. s->num_h_slices=1;
  599. s->num_v_slices=1;
  600. return 0;
  601. }
  602. static int init_slice_state(FFV1Context *f, FFV1Context *fs){
  603. int j;
  604. fs->plane_count= f->plane_count;
  605. fs->transparency= f->transparency;
  606. for(j=0; j<f->plane_count; j++){
  607. PlaneContext * const p= &fs->plane[j];
  608. if(fs->ac){
  609. if(!p-> state) p-> state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  610. if(!p-> state)
  611. return AVERROR(ENOMEM);
  612. }else{
  613. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  614. if(!p->vlc_state)
  615. return AVERROR(ENOMEM);
  616. }
  617. }
  618. if (fs->ac>1){
  619. //FIXME only redo if state_transition changed
  620. for(j=1; j<256; j++){
  621. fs->c.one_state [ j]= fs->state_transition[j];
  622. fs->c.zero_state[256-j]= 256-fs->c.one_state [j];
  623. }
  624. }
  625. return 0;
  626. }
  627. static int init_slices_state(FFV1Context *f){
  628. int i;
  629. for(i=0; i<f->slice_count; i++){
  630. FFV1Context *fs= f->slice_context[i];
  631. if(init_slice_state(f, fs) < 0)
  632. return -1;
  633. }
  634. return 0;
  635. }
  636. static av_cold int init_slice_contexts(FFV1Context *f){
  637. int i;
  638. f->slice_count= f->num_h_slices * f->num_v_slices;
  639. for(i=0; i<f->slice_count; i++){
  640. FFV1Context *fs= av_mallocz(sizeof(*fs));
  641. int sx= i % f->num_h_slices;
  642. int sy= i / f->num_h_slices;
  643. int sxs= f->avctx->width * sx / f->num_h_slices;
  644. int sxe= f->avctx->width *(sx+1) / f->num_h_slices;
  645. int sys= f->avctx->height* sy / f->num_v_slices;
  646. int sye= f->avctx->height*(sy+1) / f->num_v_slices;
  647. f->slice_context[i]= fs;
  648. memcpy(fs, f, sizeof(*fs));
  649. memset(fs->rc_stat2, 0, sizeof(fs->rc_stat2));
  650. fs->slice_width = sxe - sxs;
  651. fs->slice_height= sye - sys;
  652. fs->slice_x = sxs;
  653. fs->slice_y = sys;
  654. fs->sample_buffer = av_malloc(3*4 * (fs->width+6) * sizeof(*fs->sample_buffer));
  655. if (!fs->sample_buffer)
  656. return AVERROR(ENOMEM);
  657. }
  658. return 0;
  659. }
  660. static int allocate_initial_states(FFV1Context *f){
  661. int i;
  662. for(i=0; i<f->quant_table_count; i++){
  663. f->initial_states[i]= av_malloc(f->context_count[i]*sizeof(*f->initial_states[i]));
  664. if(!f->initial_states[i])
  665. return AVERROR(ENOMEM);
  666. memset(f->initial_states[i], 128, f->context_count[i]*sizeof(*f->initial_states[i]));
  667. }
  668. return 0;
  669. }
  670. #if CONFIG_FFV1_ENCODER
  671. static int write_extra_header(FFV1Context *f){
  672. RangeCoder * const c= &f->c;
  673. uint8_t state[CONTEXT_SIZE];
  674. int i, j, k;
  675. uint8_t state2[32][CONTEXT_SIZE];
  676. unsigned v;
  677. memset(state2, 128, sizeof(state2));
  678. memset(state, 128, sizeof(state));
  679. f->avctx->extradata= av_malloc(f->avctx->extradata_size= 10000 + (11*11*5*5*5+11*11*11)*32);
  680. ff_init_range_encoder(c, f->avctx->extradata, f->avctx->extradata_size);
  681. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  682. put_symbol(c, state, f->version, 0);
  683. if(f->version > 2)
  684. put_symbol(c, state, f->minor_version, 0);
  685. put_symbol(c, state, f->ac, 0);
  686. if(f->ac>1){
  687. for(i=1; i<256; i++){
  688. put_symbol(c, state, f->state_transition[i] - c->one_state[i], 1);
  689. }
  690. }
  691. put_symbol(c, state, f->colorspace, 0); //YUV cs type
  692. put_symbol(c, state, f->bits_per_raw_sample, 0);
  693. put_rac(c, state, f->chroma_planes);
  694. put_symbol(c, state, f->chroma_h_shift, 0);
  695. put_symbol(c, state, f->chroma_v_shift, 0);
  696. put_rac(c, state, f->transparency);
  697. put_symbol(c, state, f->num_h_slices-1, 0);
  698. put_symbol(c, state, f->num_v_slices-1, 0);
  699. put_symbol(c, state, f->quant_table_count, 0);
  700. for(i=0; i<f->quant_table_count; i++)
  701. write_quant_tables(c, f->quant_tables[i]);
  702. for(i=0; i<f->quant_table_count; i++){
  703. for(j=0; j<f->context_count[i]*CONTEXT_SIZE; j++)
  704. if(f->initial_states[i] && f->initial_states[i][0][j] != 128)
  705. break;
  706. if(j<f->context_count[i]*CONTEXT_SIZE){
  707. put_rac(c, state, 1);
  708. for(j=0; j<f->context_count[i]; j++){
  709. for(k=0; k<CONTEXT_SIZE; k++){
  710. int pred= j ? f->initial_states[i][j-1][k] : 128;
  711. put_symbol(c, state2[k], (int8_t)(f->initial_states[i][j][k]-pred), 1);
  712. }
  713. }
  714. }else{
  715. put_rac(c, state, 0);
  716. }
  717. }
  718. if(f->version > 2){
  719. put_symbol(c, state, f->ec, 0);
  720. }
  721. f->avctx->extradata_size= ff_rac_terminate(c);
  722. v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, f->avctx->extradata, f->avctx->extradata_size);
  723. AV_WL32(f->avctx->extradata + f->avctx->extradata_size, v);
  724. f->avctx->extradata_size += 4;
  725. return 0;
  726. }
  727. static int sort_stt(FFV1Context *s, uint8_t stt[256]){
  728. int i,i2,changed,print=0;
  729. do{
  730. changed=0;
  731. for(i=12; i<244; i++){
  732. for(i2=i+1; i2<245 && i2<i+4; i2++){
  733. #define COST(old, new) \
  734. s->rc_stat[old][0]*-log2((256-(new))/256.0)\
  735. +s->rc_stat[old][1]*-log2( (new) /256.0)
  736. #define COST2(old, new) \
  737. COST(old, new)\
  738. +COST(256-(old), 256-(new))
  739. double size0= COST2(i, i ) + COST2(i2, i2);
  740. double sizeX= COST2(i, i2) + COST2(i2, i );
  741. if(sizeX < size0 && i!=128 && i2!=128){
  742. int j;
  743. FFSWAP(int, stt[ i], stt[ i2]);
  744. FFSWAP(int, s->rc_stat[i ][0],s->rc_stat[ i2][0]);
  745. FFSWAP(int, s->rc_stat[i ][1],s->rc_stat[ i2][1]);
  746. if(i != 256-i2){
  747. FFSWAP(int, stt[256-i], stt[256-i2]);
  748. FFSWAP(int, s->rc_stat[256-i][0],s->rc_stat[256-i2][0]);
  749. FFSWAP(int, s->rc_stat[256-i][1],s->rc_stat[256-i2][1]);
  750. }
  751. for(j=1; j<256; j++){
  752. if (stt[j] == i ) stt[j] = i2;
  753. else if(stt[j] == i2) stt[j] = i ;
  754. if(i != 256-i2){
  755. if (stt[256-j] == 256-i ) stt[256-j] = 256-i2;
  756. else if(stt[256-j] == 256-i2) stt[256-j] = 256-i ;
  757. }
  758. }
  759. print=changed=1;
  760. }
  761. }
  762. }
  763. }while(changed);
  764. return print;
  765. }
  766. static av_cold int encode_init(AVCodecContext *avctx)
  767. {
  768. FFV1Context *s = avctx->priv_data;
  769. int i, j, k, m;
  770. common_init(avctx);
  771. s->version=0;
  772. if((avctx->flags & (CODEC_FLAG_PASS1|CODEC_FLAG_PASS2)) || avctx->slices>1)
  773. s->version = FFMAX(s->version, 2);
  774. if(avctx->level == 3){
  775. s->version = 3;
  776. s->ec = 1;
  777. }
  778. if(s->version >= 2 && avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  779. av_log(avctx, AV_LOG_ERROR, "Version 2 needed for requested features but version 2 is experimental and not enabled\n");
  780. return -1;
  781. }
  782. s->ac= avctx->coder_type ? 2:0;
  783. if(s->ac>1)
  784. for(i=1; i<256; i++)
  785. s->state_transition[i]=ver2_state[i];
  786. s->plane_count=3;
  787. switch(avctx->pix_fmt){
  788. case PIX_FMT_YUV444P9:
  789. case PIX_FMT_YUV422P9:
  790. case PIX_FMT_YUV420P9:
  791. if (!avctx->bits_per_raw_sample)
  792. s->bits_per_raw_sample = 9;
  793. case PIX_FMT_YUV444P10:
  794. case PIX_FMT_YUV420P10:
  795. case PIX_FMT_YUV422P10:
  796. s->packed_at_lsb = 1;
  797. if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
  798. s->bits_per_raw_sample = 10;
  799. case PIX_FMT_GRAY16:
  800. case PIX_FMT_YUV444P16:
  801. case PIX_FMT_YUV422P16:
  802. case PIX_FMT_YUV420P16:
  803. if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample) {
  804. s->bits_per_raw_sample = 16;
  805. } else if (!s->bits_per_raw_sample){
  806. s->bits_per_raw_sample = avctx->bits_per_raw_sample;
  807. }
  808. if(s->bits_per_raw_sample <=8){
  809. av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
  810. return -1;
  811. }
  812. if(!s->ac){
  813. av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample of more than 8 needs -coder 1 currently\n");
  814. return -1;
  815. }
  816. s->version= FFMAX(s->version, 1);
  817. case PIX_FMT_GRAY8:
  818. case PIX_FMT_YUV444P:
  819. case PIX_FMT_YUV440P:
  820. case PIX_FMT_YUV422P:
  821. case PIX_FMT_YUV420P:
  822. case PIX_FMT_YUV411P:
  823. case PIX_FMT_YUV410P:
  824. s->chroma_planes= av_pix_fmt_descriptors[avctx->pix_fmt].nb_components < 3 ? 0 : 1;
  825. s->colorspace= 0;
  826. break;
  827. case PIX_FMT_YUVA444P:
  828. case PIX_FMT_YUVA420P:
  829. s->chroma_planes= 1;
  830. s->colorspace= 0;
  831. s->transparency= 1;
  832. break;
  833. case PIX_FMT_RGB32:
  834. s->colorspace= 1;
  835. s->transparency= 1;
  836. break;
  837. case PIX_FMT_0RGB32:
  838. s->colorspace= 1;
  839. break;
  840. default:
  841. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  842. return -1;
  843. }
  844. if (s->transparency) {
  845. av_log(avctx, AV_LOG_WARNING, "Storing alpha plane, this will require a recent FFV1 decoder to playback!\n");
  846. }
  847. if (avctx->context_model > 1U) {
  848. av_log(avctx, AV_LOG_ERROR, "Invalid context model %d, valid values are 0 and 1\n", avctx->context_model);
  849. return AVERROR(EINVAL);
  850. }
  851. for(i=0; i<256; i++){
  852. s->quant_table_count=2;
  853. if(s->bits_per_raw_sample <=8){
  854. s->quant_tables[0][0][i]= quant11[i];
  855. s->quant_tables[0][1][i]= 11*quant11[i];
  856. s->quant_tables[0][2][i]= 11*11*quant11[i];
  857. s->quant_tables[1][0][i]= quant11[i];
  858. s->quant_tables[1][1][i]= 11*quant11[i];
  859. s->quant_tables[1][2][i]= 11*11*quant5 [i];
  860. s->quant_tables[1][3][i]= 5*11*11*quant5 [i];
  861. s->quant_tables[1][4][i]= 5*5*11*11*quant5 [i];
  862. }else{
  863. s->quant_tables[0][0][i]= quant9_10bit[i];
  864. s->quant_tables[0][1][i]= 11*quant9_10bit[i];
  865. s->quant_tables[0][2][i]= 11*11*quant9_10bit[i];
  866. s->quant_tables[1][0][i]= quant9_10bit[i];
  867. s->quant_tables[1][1][i]= 11*quant9_10bit[i];
  868. s->quant_tables[1][2][i]= 11*11*quant5_10bit[i];
  869. s->quant_tables[1][3][i]= 5*11*11*quant5_10bit[i];
  870. s->quant_tables[1][4][i]= 5*5*11*11*quant5_10bit[i];
  871. }
  872. }
  873. s->context_count[0]= (11*11*11+1)/2;
  874. s->context_count[1]= (11*11*5*5*5+1)/2;
  875. memcpy(s->quant_table, s->quant_tables[avctx->context_model], sizeof(s->quant_table));
  876. for(i=0; i<s->plane_count; i++){
  877. PlaneContext * const p= &s->plane[i];
  878. memcpy(p->quant_table, s->quant_table, sizeof(p->quant_table));
  879. p->quant_table_index= avctx->context_model;
  880. p->context_count= s->context_count[p->quant_table_index];
  881. }
  882. if(allocate_initial_states(s) < 0)
  883. return AVERROR(ENOMEM);
  884. avctx->coded_frame= &s->picture;
  885. if(!s->transparency)
  886. s->plane_count= 2;
  887. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  888. s->picture_number=0;
  889. if(avctx->flags & (CODEC_FLAG_PASS1|CODEC_FLAG_PASS2)){
  890. for(i=0; i<s->quant_table_count; i++){
  891. s->rc_stat2[i]= av_mallocz(s->context_count[i]*sizeof(*s->rc_stat2[i]));
  892. if(!s->rc_stat2[i])
  893. return AVERROR(ENOMEM);
  894. }
  895. }
  896. if(avctx->stats_in){
  897. char *p= avctx->stats_in;
  898. uint8_t best_state[256][256];
  899. int gob_count=0;
  900. char *next;
  901. av_assert0(s->version>=2);
  902. for(;;){
  903. for(j=0; j<256; j++){
  904. for(i=0; i<2; i++){
  905. s->rc_stat[j][i]= strtol(p, &next, 0);
  906. if(next==p){
  907. av_log(avctx, AV_LOG_ERROR, "2Pass file invalid at %d %d [%s]\n", j,i,p);
  908. return -1;
  909. }
  910. p=next;
  911. }
  912. }
  913. for(i=0; i<s->quant_table_count; i++){
  914. for(j=0; j<s->context_count[i]; j++){
  915. for(k=0; k<32; k++){
  916. for(m=0; m<2; m++){
  917. s->rc_stat2[i][j][k][m]= strtol(p, &next, 0);
  918. if(next==p){
  919. av_log(avctx, AV_LOG_ERROR, "2Pass file invalid at %d %d %d %d [%s]\n", i,j,k,m,p);
  920. return -1;
  921. }
  922. p=next;
  923. }
  924. }
  925. }
  926. }
  927. gob_count= strtol(p, &next, 0);
  928. if(next==p || gob_count <0){
  929. av_log(avctx, AV_LOG_ERROR, "2Pass file invalid\n");
  930. return -1;
  931. }
  932. p=next;
  933. while(*p=='\n' || *p==' ') p++;
  934. if(p[0]==0) break;
  935. }
  936. sort_stt(s, s->state_transition);
  937. find_best_state(best_state, s->state_transition);
  938. for(i=0; i<s->quant_table_count; i++){
  939. for(j=0; j<s->context_count[i]; j++){
  940. for(k=0; k<32; k++){
  941. double p= 128;
  942. if(s->rc_stat2[i][j][k][0]+s->rc_stat2[i][j][k][1]){
  943. p=256.0*s->rc_stat2[i][j][k][1] / (s->rc_stat2[i][j][k][0]+s->rc_stat2[i][j][k][1]);
  944. }
  945. 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)];
  946. }
  947. }
  948. }
  949. }
  950. if(s->version>1){
  951. for(s->num_v_slices=2; s->num_v_slices<9; s->num_v_slices++){
  952. for(s->num_h_slices=s->num_v_slices; s->num_h_slices<2*s->num_v_slices; s->num_h_slices++){
  953. if(avctx->slices == s->num_h_slices * s->num_v_slices && avctx->slices <= 64)
  954. goto slices_ok;
  955. }
  956. }
  957. av_log(avctx, AV_LOG_ERROR, "Unsupported number %d of slices requested, please specify a supported number with -slices\n", avctx->slices);
  958. return -1;
  959. slices_ok:
  960. write_extra_header(s);
  961. }
  962. if(init_slice_contexts(s) < 0)
  963. return -1;
  964. if(init_slices_state(s) < 0)
  965. return -1;
  966. #define STATS_OUT_SIZE 1024*1024*6
  967. if(avctx->flags & CODEC_FLAG_PASS1){
  968. avctx->stats_out= av_mallocz(STATS_OUT_SIZE);
  969. for(i=0; i<s->quant_table_count; i++){
  970. for(j=0; j<s->slice_count; j++){
  971. FFV1Context *sf= s->slice_context[j];
  972. av_assert0(!sf->rc_stat2[i]);
  973. sf->rc_stat2[i]= av_mallocz(s->context_count[i]*sizeof(*sf->rc_stat2[i]));
  974. if(!sf->rc_stat2[i])
  975. return AVERROR(ENOMEM);
  976. }
  977. }
  978. }
  979. return 0;
  980. }
  981. #endif /* CONFIG_FFV1_ENCODER */
  982. static void clear_slice_state(FFV1Context *f, FFV1Context *fs){
  983. int i, j;
  984. for(i=0; i<f->plane_count; i++){
  985. PlaneContext *p= &fs->plane[i];
  986. p->interlace_bit_state[0]= 128;
  987. p->interlace_bit_state[1]= 128;
  988. if(fs->ac){
  989. if(f->initial_states[p->quant_table_index]){
  990. memcpy(p->state, f->initial_states[p->quant_table_index], CONTEXT_SIZE*p->context_count);
  991. }else
  992. memset(p->state, 128, CONTEXT_SIZE*p->context_count);
  993. }else{
  994. for(j=0; j<p->context_count; j++){
  995. p->vlc_state[j].drift= 0;
  996. p->vlc_state[j].error_sum= 4; //FFMAX((RANGE + 32)/64, 2);
  997. p->vlc_state[j].bias= 0;
  998. p->vlc_state[j].count= 1;
  999. }
  1000. }
  1001. }
  1002. }
  1003. static void clear_state(FFV1Context *f){
  1004. int si;
  1005. for(si=0; si<f->slice_count; si++){
  1006. FFV1Context *fs= f->slice_context[si];
  1007. clear_slice_state(f, fs);
  1008. }
  1009. }
  1010. #if CONFIG_FFV1_ENCODER
  1011. static void encode_slice_header(FFV1Context *f, FFV1Context *fs){
  1012. RangeCoder *c = &fs->c;
  1013. uint8_t state[CONTEXT_SIZE];
  1014. int j;
  1015. memset(state, 128, sizeof(state));
  1016. put_symbol(c, state, (fs->slice_x +1)*f->num_h_slices / f->width , 0);
  1017. put_symbol(c, state, (fs->slice_y +1)*f->num_v_slices / f->height , 0);
  1018. put_symbol(c, state, (fs->slice_width +1)*f->num_h_slices / f->width -1, 0);
  1019. put_symbol(c, state, (fs->slice_height+1)*f->num_v_slices / f->height-1, 0);
  1020. for(j=0; j<f->plane_count; j++){
  1021. put_symbol(c, state, f->plane[j].quant_table_index, 0);
  1022. av_assert0(f->plane[j].quant_table_index == f->avctx->context_model);
  1023. }
  1024. if(!f->picture.interlaced_frame) put_symbol(c, state, 3, 0);
  1025. else put_symbol(c, state, 1 + !f->picture.top_field_first, 0);
  1026. put_symbol(c, state, f->picture.sample_aspect_ratio.num, 0);
  1027. put_symbol(c, state, f->picture.sample_aspect_ratio.den, 0);
  1028. }
  1029. static int encode_slice(AVCodecContext *c, void *arg){
  1030. FFV1Context *fs= *(void**)arg;
  1031. FFV1Context *f= fs->avctx->priv_data;
  1032. int width = fs->slice_width;
  1033. int height= fs->slice_height;
  1034. int x= fs->slice_x;
  1035. int y= fs->slice_y;
  1036. AVFrame * const p= &f->picture;
  1037. const int ps= (f->bits_per_raw_sample>8)+1;
  1038. if(p->key_frame)
  1039. clear_slice_state(f, fs);
  1040. if(f->version > 2){
  1041. encode_slice_header(f, fs);
  1042. }
  1043. if(!fs->ac){
  1044. fs->ac_byte_count = f->version > 2 || (!x&&!y) ? ff_rac_terminate(&fs->c) : 0;
  1045. 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);
  1046. }
  1047. if(f->colorspace==0){
  1048. const int chroma_width = -((-width )>>f->chroma_h_shift);
  1049. const int chroma_height= -((-height)>>f->chroma_v_shift);
  1050. const int cx= x>>f->chroma_h_shift;
  1051. const int cy= y>>f->chroma_v_shift;
  1052. encode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0);
  1053. if (f->chroma_planes){
  1054. encode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
  1055. encode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1);
  1056. }
  1057. if (fs->transparency)
  1058. encode_plane(fs, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2);
  1059. }else{
  1060. encode_rgb_frame(fs, (uint32_t*)(p->data[0]) + ps*x + y*(p->linesize[0]/4), width, height, p->linesize[0]/4);
  1061. }
  1062. emms_c();
  1063. return 0;
  1064. }
  1065. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  1066. const AVFrame *pict, int *got_packet)
  1067. {
  1068. FFV1Context *f = avctx->priv_data;
  1069. RangeCoder * const c= &f->slice_context[0]->c;
  1070. AVFrame * const p= &f->picture;
  1071. int used_count= 0;
  1072. uint8_t keystate=128;
  1073. uint8_t *buf_p;
  1074. int i, ret;
  1075. if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*((8*2+1+1)*4)/8
  1076. + FF_MIN_BUFFER_SIZE)) < 0)
  1077. return ret;
  1078. ff_init_range_encoder(c, pkt->data, pkt->size);
  1079. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  1080. *p = *pict;
  1081. p->pict_type= AV_PICTURE_TYPE_I;
  1082. if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){
  1083. put_rac(c, &keystate, 1);
  1084. p->key_frame= 1;
  1085. f->gob_count++;
  1086. write_header(f);
  1087. }else{
  1088. put_rac(c, &keystate, 0);
  1089. p->key_frame= 0;
  1090. }
  1091. if (f->ac>1){
  1092. int i;
  1093. for(i=1; i<256; i++){
  1094. c->one_state[i]= f->state_transition[i];
  1095. c->zero_state[256-i]= 256-c->one_state[i];
  1096. }
  1097. }
  1098. for(i=1; i<f->slice_count; i++){
  1099. FFV1Context *fs= f->slice_context[i];
  1100. uint8_t *start = pkt->data + (pkt->size-used_count)*i/f->slice_count;
  1101. int len = pkt->size/f->slice_count;
  1102. ff_init_range_encoder(&fs->c, start, len);
  1103. }
  1104. avctx->execute(avctx, encode_slice, &f->slice_context[0], NULL, f->slice_count, sizeof(void*));
  1105. buf_p = pkt->data;
  1106. for(i=0; i<f->slice_count; i++){
  1107. FFV1Context *fs= f->slice_context[i];
  1108. int bytes;
  1109. if(fs->ac){
  1110. uint8_t state=128;
  1111. put_rac(&fs->c, &state, 0);
  1112. bytes= ff_rac_terminate(&fs->c);
  1113. }else{
  1114. flush_put_bits(&fs->pb); //nicer padding FIXME
  1115. bytes= fs->ac_byte_count + (put_bits_count(&fs->pb)+7)/8;
  1116. }
  1117. if(i>0 || f->version>2){
  1118. av_assert0(bytes < pkt->size/f->slice_count);
  1119. memmove(buf_p, fs->c.bytestream_start, bytes);
  1120. av_assert0(bytes < (1<<24));
  1121. AV_WB24(buf_p+bytes, bytes);
  1122. bytes+=3;
  1123. }
  1124. if(f->ec){
  1125. unsigned v;
  1126. buf_p[bytes++] = 0;
  1127. v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, bytes);
  1128. AV_WL32(buf_p + bytes, v); bytes += 4;
  1129. }
  1130. buf_p += bytes;
  1131. }
  1132. if((avctx->flags&CODEC_FLAG_PASS1) && (f->picture_number&31)==0){
  1133. int j, k, m;
  1134. char *p= avctx->stats_out;
  1135. char *end= p + STATS_OUT_SIZE;
  1136. memset(f->rc_stat, 0, sizeof(f->rc_stat));
  1137. for(i=0; i<f->quant_table_count; i++)
  1138. memset(f->rc_stat2[i], 0, f->context_count[i]*sizeof(*f->rc_stat2[i]));
  1139. for(j=0; j<f->slice_count; j++){
  1140. FFV1Context *fs= f->slice_context[j];
  1141. for(i=0; i<256; i++){
  1142. f->rc_stat[i][0] += fs->rc_stat[i][0];
  1143. f->rc_stat[i][1] += fs->rc_stat[i][1];
  1144. }
  1145. for(i=0; i<f->quant_table_count; i++){
  1146. for(k=0; k<f->context_count[i]; k++){
  1147. for(m=0; m<32; m++){
  1148. f->rc_stat2[i][k][m][0] += fs->rc_stat2[i][k][m][0];
  1149. f->rc_stat2[i][k][m][1] += fs->rc_stat2[i][k][m][1];
  1150. }
  1151. }
  1152. }
  1153. }
  1154. for(j=0; j<256; j++){
  1155. snprintf(p, end-p, "%"PRIu64" %"PRIu64" ", f->rc_stat[j][0], f->rc_stat[j][1]);
  1156. p+= strlen(p);
  1157. }
  1158. snprintf(p, end-p, "\n");
  1159. for(i=0; i<f->quant_table_count; i++){
  1160. for(j=0; j<f->context_count[i]; j++){
  1161. for(m=0; m<32; m++){
  1162. snprintf(p, end-p, "%"PRIu64" %"PRIu64" ", f->rc_stat2[i][j][m][0], f->rc_stat2[i][j][m][1]);
  1163. p+= strlen(p);
  1164. }
  1165. }
  1166. }
  1167. snprintf(p, end-p, "%d\n", f->gob_count);
  1168. } else if(avctx->flags&CODEC_FLAG_PASS1)
  1169. avctx->stats_out[0] = '\0';
  1170. f->picture_number++;
  1171. pkt->size = buf_p - pkt->data;
  1172. pkt->flags |= AV_PKT_FLAG_KEY*p->key_frame;
  1173. *got_packet = 1;
  1174. return 0;
  1175. }
  1176. #endif /* CONFIG_FFV1_ENCODER */
  1177. static av_cold int common_end(AVCodecContext *avctx){
  1178. FFV1Context *s = avctx->priv_data;
  1179. int i, j;
  1180. if (avctx->codec->decode && s->picture.data[0])
  1181. avctx->release_buffer(avctx, &s->picture);
  1182. for(j=0; j<s->slice_count; j++){
  1183. FFV1Context *fs= s->slice_context[j];
  1184. for(i=0; i<s->plane_count; i++){
  1185. PlaneContext *p= &fs->plane[i];
  1186. av_freep(&p->state);
  1187. av_freep(&p->vlc_state);
  1188. }
  1189. av_freep(&fs->sample_buffer);
  1190. }
  1191. av_freep(&avctx->stats_out);
  1192. for(j=0; j<s->quant_table_count; j++){
  1193. av_freep(&s->initial_states[j]);
  1194. for(i=0; i<s->slice_count; i++){
  1195. FFV1Context *sf= s->slice_context[i];
  1196. av_freep(&sf->rc_stat2[j]);
  1197. }
  1198. av_freep(&s->rc_stat2[j]);
  1199. }
  1200. for(i=0; i<s->slice_count; i++){
  1201. av_freep(&s->slice_context[i]);
  1202. }
  1203. return 0;
  1204. }
  1205. static av_always_inline void decode_line(FFV1Context *s, int w,
  1206. int16_t *sample[2],
  1207. int plane_index, int bits)
  1208. {
  1209. PlaneContext * const p= &s->plane[plane_index];
  1210. RangeCoder * const c= &s->c;
  1211. int x;
  1212. int run_count=0;
  1213. int run_mode=0;
  1214. int run_index= s->run_index;
  1215. for(x=0; x<w; x++){
  1216. int diff, context, sign;
  1217. context= get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
  1218. if(context < 0){
  1219. context= -context;
  1220. sign=1;
  1221. }else
  1222. sign=0;
  1223. av_assert2(context < p->context_count);
  1224. if(s->ac){
  1225. diff= get_symbol_inline(c, p->state[context], 1);
  1226. }else{
  1227. if(context == 0 && run_mode==0) run_mode=1;
  1228. if(run_mode){
  1229. if(run_count==0 && run_mode==1){
  1230. if(get_bits1(&s->gb)){
  1231. run_count = 1<<ff_log2_run[run_index];
  1232. if(x + run_count <= w) run_index++;
  1233. }else{
  1234. if(ff_log2_run[run_index]) run_count = get_bits(&s->gb, ff_log2_run[run_index]);
  1235. else run_count=0;
  1236. if(run_index) run_index--;
  1237. run_mode=2;
  1238. }
  1239. }
  1240. run_count--;
  1241. if(run_count < 0){
  1242. run_mode=0;
  1243. run_count=0;
  1244. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  1245. if(diff>=0) diff++;
  1246. }else
  1247. diff=0;
  1248. }else
  1249. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  1250. // 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));
  1251. }
  1252. if(sign) diff= -diff;
  1253. sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
  1254. }
  1255. s->run_index= run_index;
  1256. }
  1257. static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  1258. int x, y;
  1259. int16_t *sample[2];
  1260. sample[0]=s->sample_buffer +3;
  1261. sample[1]=s->sample_buffer+w+6+3;
  1262. s->run_index=0;
  1263. memset(s->sample_buffer, 0, 2*(w+6)*sizeof(*s->sample_buffer));
  1264. for(y=0; y<h; y++){
  1265. int16_t *temp = sample[0]; //FIXME try a normal buffer
  1266. sample[0]= sample[1];
  1267. sample[1]= temp;
  1268. sample[1][-1]= sample[0][0 ];
  1269. sample[0][ w]= sample[0][w-1];
  1270. //{START_TIMER
  1271. if(s->avctx->bits_per_raw_sample <= 8){
  1272. decode_line(s, w, sample, plane_index, 8);
  1273. for(x=0; x<w; x++){
  1274. src[x + stride*y]= sample[1][x];
  1275. }
  1276. }else{
  1277. decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
  1278. if(s->packed_at_lsb){
  1279. for(x=0; x<w; x++){
  1280. ((uint16_t*)(src + stride*y))[x]= sample[1][x];
  1281. }
  1282. }else{
  1283. for(x=0; x<w; x++){
  1284. ((uint16_t*)(src + stride*y))[x]= sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
  1285. }
  1286. }
  1287. }
  1288. //STOP_TIMER("decode-line")}
  1289. }
  1290. }
  1291. static void decode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  1292. int x, y, p;
  1293. int16_t *sample[4][2];
  1294. for(x=0; x<4; x++){
  1295. sample[x][0] = s->sample_buffer + x*2 *(w+6) + 3;
  1296. sample[x][1] = s->sample_buffer + (x*2+1)*(w+6) + 3;
  1297. }
  1298. s->run_index=0;
  1299. memset(s->sample_buffer, 0, 8*(w+6)*sizeof(*s->sample_buffer));
  1300. for(y=0; y<h; y++){
  1301. for(p=0; p<3 + s->transparency; p++){
  1302. int16_t *temp = sample[p][0]; //FIXME try a normal buffer
  1303. sample[p][0]= sample[p][1];
  1304. sample[p][1]= temp;
  1305. sample[p][1][-1]= sample[p][0][0 ];
  1306. sample[p][0][ w]= sample[p][0][w-1];
  1307. decode_line(s, w, sample[p], (p+1)/2, 9);
  1308. }
  1309. for(x=0; x<w; x++){
  1310. int g= sample[0][1][x];
  1311. int b= sample[1][1][x];
  1312. int r= sample[2][1][x];
  1313. int a= sample[3][1][x];
  1314. // assert(g>=0 && b>=0 && r>=0);
  1315. // assert(g<256 && b<512 && r<512);
  1316. b -= 0x100;
  1317. r -= 0x100;
  1318. g -= (b + r)>>2;
  1319. b += g;
  1320. r += g;
  1321. src[x + stride*y]= b + (g<<8) + (r<<16) + (a<<24);
  1322. }
  1323. }
  1324. }
  1325. static int decode_slice_header(FFV1Context *f, FFV1Context *fs){
  1326. RangeCoder *c = &fs->c;
  1327. uint8_t state[CONTEXT_SIZE];
  1328. unsigned ps, i, context_count;
  1329. memset(state, 128, sizeof(state));
  1330. av_assert0(f->version > 2);
  1331. fs->slice_x = get_symbol(c, state, 0) *f->width ;
  1332. fs->slice_y = get_symbol(c, state, 0) *f->height;
  1333. fs->slice_width =(get_symbol(c, state, 0)+1)*f->width + fs->slice_x;
  1334. fs->slice_height=(get_symbol(c, state, 0)+1)*f->height + fs->slice_y;
  1335. fs->slice_x /= f->num_h_slices;
  1336. fs->slice_y /= f->num_v_slices;
  1337. fs->slice_width = fs->slice_width /f->num_h_slices - fs->slice_x;
  1338. fs->slice_height = fs->slice_height/f->num_v_slices - fs->slice_y;
  1339. if((unsigned)fs->slice_width > f->width || (unsigned)fs->slice_height > f->height)
  1340. return -1;
  1341. if( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
  1342. || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
  1343. return -1;
  1344. for(i=0; i<f->plane_count; i++){
  1345. PlaneContext * const p= &fs->plane[i];
  1346. int idx=get_symbol(c, state, 0);
  1347. if(idx > (unsigned)f->quant_table_count){
  1348. av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
  1349. return -1;
  1350. }
  1351. p->quant_table_index= idx;
  1352. memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
  1353. context_count= f->context_count[idx];
  1354. if(p->context_count < context_count){
  1355. av_freep(&p->state);
  1356. av_freep(&p->vlc_state);
  1357. }
  1358. p->context_count= context_count;
  1359. }
  1360. ps = get_symbol(c, state, 0);
  1361. if(ps==1){
  1362. f->picture.interlaced_frame = 1;
  1363. f->picture.top_field_first = 1;
  1364. } else if(ps==2){
  1365. f->picture.interlaced_frame = 1;
  1366. f->picture.top_field_first = 0;
  1367. } else if(ps==3){
  1368. f->picture.interlaced_frame = 0;
  1369. }
  1370. f->picture.sample_aspect_ratio.num = get_symbol(c, state, 0);
  1371. f->picture.sample_aspect_ratio.den = get_symbol(c, state, 0);
  1372. return 0;
  1373. }
  1374. static int decode_slice(AVCodecContext *c, void *arg){
  1375. FFV1Context *fs= *(void**)arg;
  1376. FFV1Context *f= fs->avctx->priv_data;
  1377. int width, height, x, y;
  1378. const int ps= (c->bits_per_raw_sample>8)+1;
  1379. AVFrame * const p= &f->picture;
  1380. if(f->version > 2){
  1381. if(init_slice_state(f, fs) < 0)
  1382. return AVERROR(ENOMEM);
  1383. if(decode_slice_header(f, fs) < 0)
  1384. return AVERROR_INVALIDDATA;
  1385. }
  1386. if(init_slice_state(f, fs) < 0)
  1387. return AVERROR(ENOMEM);
  1388. if(f->picture.key_frame)
  1389. clear_slice_state(f, fs);
  1390. width = fs->slice_width;
  1391. height= fs->slice_height;
  1392. x= fs->slice_x;
  1393. y= fs->slice_y;
  1394. if(!fs->ac){
  1395. fs->ac_byte_count = f->version > 2 || (!x&&!y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0;
  1396. init_get_bits(&fs->gb,
  1397. fs->c.bytestream_start + fs->ac_byte_count,
  1398. (fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count) * 8);
  1399. }
  1400. av_assert1(width && height);
  1401. if(f->colorspace==0){
  1402. const int chroma_width = -((-width )>>f->chroma_h_shift);
  1403. const int chroma_height= -((-height)>>f->chroma_v_shift);
  1404. const int cx= x>>f->chroma_h_shift;
  1405. const int cy= y>>f->chroma_v_shift;
  1406. decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0);
  1407. if (f->chroma_planes){
  1408. decode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
  1409. decode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1);
  1410. }
  1411. if (fs->transparency)
  1412. decode_plane(fs, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2);
  1413. }else{
  1414. decode_rgb_frame(fs, (uint32_t*)p->data[0] + ps*x + y*(p->linesize[0]/4), width, height, p->linesize[0]/4);
  1415. }
  1416. emms_c();
  1417. return 0;
  1418. }
  1419. static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){
  1420. int v;
  1421. int i=0;
  1422. uint8_t state[CONTEXT_SIZE];
  1423. memset(state, 128, sizeof(state));
  1424. for(v=0; i<128 ; v++){
  1425. int len= get_symbol(c, state, 0) + 1;
  1426. if(len + i > 128) return -1;
  1427. while(len--){
  1428. quant_table[i] = scale*v;
  1429. i++;
  1430. //printf("%2d ",v);
  1431. //if(i%16==0) printf("\n");
  1432. }
  1433. }
  1434. for(i=1; i<128; i++){
  1435. quant_table[256-i]= -quant_table[i];
  1436. }
  1437. quant_table[128]= -quant_table[127];
  1438. return 2*v - 1;
  1439. }
  1440. static int read_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256]){
  1441. int i;
  1442. int context_count=1;
  1443. for(i=0; i<5; i++){
  1444. context_count*= read_quant_table(c, quant_table[i], context_count);
  1445. if(context_count > 32768U){
  1446. return -1;
  1447. }
  1448. }
  1449. return (context_count+1)/2;
  1450. }
  1451. static int read_extra_header(FFV1Context *f){
  1452. RangeCoder * const c= &f->c;
  1453. uint8_t state[CONTEXT_SIZE];
  1454. int i, j, k;
  1455. uint8_t state2[32][CONTEXT_SIZE];
  1456. memset(state2, 128, sizeof(state2));
  1457. memset(state, 128, sizeof(state));
  1458. ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
  1459. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  1460. f->version= get_symbol(c, state, 0);
  1461. if(f->version > 2)
  1462. f->minor_version= get_symbol(c, state, 0);
  1463. f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
  1464. if(f->ac>1){
  1465. for(i=1; i<256; i++){
  1466. f->state_transition[i]= get_symbol(c, state, 1) + c->one_state[i];
  1467. }
  1468. }
  1469. f->colorspace= get_symbol(c, state, 0); //YUV cs type
  1470. f->avctx->bits_per_raw_sample= get_symbol(c, state, 0);
  1471. f->chroma_planes= get_rac(c, state);
  1472. f->chroma_h_shift= get_symbol(c, state, 0);
  1473. f->chroma_v_shift= get_symbol(c, state, 0);
  1474. f->transparency= get_rac(c, state);
  1475. f->plane_count= 2 + f->transparency;
  1476. f->num_h_slices= 1 + get_symbol(c, state, 0);
  1477. f->num_v_slices= 1 + get_symbol(c, state, 0);
  1478. if(f->num_h_slices > (unsigned)f->width || f->num_v_slices > (unsigned)f->height){
  1479. av_log(f->avctx, AV_LOG_ERROR, "too many slices\n");
  1480. return -1;
  1481. }
  1482. f->quant_table_count= get_symbol(c, state, 0);
  1483. if(f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
  1484. return -1;
  1485. for(i=0; i<f->quant_table_count; i++){
  1486. if((f->context_count[i]= read_quant_tables(c, f->quant_tables[i])) < 0){
  1487. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  1488. return -1;
  1489. }
  1490. }
  1491. if(allocate_initial_states(f) < 0)
  1492. return AVERROR(ENOMEM);
  1493. for(i=0; i<f->quant_table_count; i++){
  1494. if(get_rac(c, state)){
  1495. for(j=0; j<f->context_count[i]; j++){
  1496. for(k=0; k<CONTEXT_SIZE; k++){
  1497. int pred= j ? f->initial_states[i][j-1][k] : 128;
  1498. f->initial_states[i][j][k]= (pred+get_symbol(c, state2[k], 1))&0xFF;
  1499. }
  1500. }
  1501. }
  1502. }
  1503. if(f->version > 2){
  1504. f->ec = get_symbol(c, state, 0);
  1505. }
  1506. if(f->version > 2){
  1507. unsigned v;
  1508. v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, f->avctx->extradata, f->avctx->extradata_size);
  1509. if(v){
  1510. av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
  1511. return AVERROR_INVALIDDATA;
  1512. }
  1513. }
  1514. return 0;
  1515. }
  1516. static int read_header(FFV1Context *f){
  1517. uint8_t state[CONTEXT_SIZE];
  1518. int i, j, context_count;
  1519. RangeCoder * const c= &f->slice_context[0]->c;
  1520. memset(state, 128, sizeof(state));
  1521. if(f->version < 2){
  1522. f->version= get_symbol(c, state, 0);
  1523. f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
  1524. if(f->ac>1){
  1525. for(i=1; i<256; i++){
  1526. f->state_transition[i]= get_symbol(c, state, 1) + c->one_state[i];
  1527. }
  1528. }
  1529. f->colorspace= get_symbol(c, state, 0); //YUV cs type
  1530. if(f->version>0)
  1531. f->avctx->bits_per_raw_sample= get_symbol(c, state, 0);
  1532. f->chroma_planes= get_rac(c, state);
  1533. f->chroma_h_shift= get_symbol(c, state, 0);
  1534. f->chroma_v_shift= get_symbol(c, state, 0);
  1535. f->transparency= get_rac(c, state);
  1536. f->plane_count= 2 + f->transparency;
  1537. }
  1538. if(f->colorspace==0){
  1539. if(!f->transparency && !f->chroma_planes){
  1540. if (f->avctx->bits_per_raw_sample<=8)
  1541. f->avctx->pix_fmt= PIX_FMT_GRAY8;
  1542. else
  1543. f->avctx->pix_fmt= PIX_FMT_GRAY16;
  1544. }else if(f->avctx->bits_per_raw_sample<=8 && !f->transparency){
  1545. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  1546. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
  1547. case 0x01: f->avctx->pix_fmt= PIX_FMT_YUV440P; break;
  1548. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
  1549. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
  1550. case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
  1551. case 0x22: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
  1552. default:
  1553. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  1554. return -1;
  1555. }
  1556. }else if(f->avctx->bits_per_raw_sample<=8 && f->transparency){
  1557. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  1558. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUVA444P; break;
  1559. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUVA420P; break;
  1560. default:
  1561. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  1562. return -1;
  1563. }
  1564. }else if(f->avctx->bits_per_raw_sample==9) {
  1565. f->packed_at_lsb=1;
  1566. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  1567. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P9; break;
  1568. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P9; break;
  1569. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P9; break;
  1570. default:
  1571. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  1572. return -1;
  1573. }
  1574. }else if(f->avctx->bits_per_raw_sample==10) {
  1575. f->packed_at_lsb=1;
  1576. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  1577. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P10; break;
  1578. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P10; break;
  1579. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P10; break;
  1580. default:
  1581. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  1582. return -1;
  1583. }
  1584. }else {
  1585. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  1586. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P16; break;
  1587. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P16; break;
  1588. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P16; break;
  1589. default:
  1590. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  1591. return -1;
  1592. }
  1593. }
  1594. }else if(f->colorspace==1){
  1595. if(f->chroma_h_shift || f->chroma_v_shift){
  1596. av_log(f->avctx, AV_LOG_ERROR, "chroma subsampling not supported in this colorspace\n");
  1597. return -1;
  1598. }
  1599. if(f->transparency) f->avctx->pix_fmt= PIX_FMT_RGB32;
  1600. else f->avctx->pix_fmt= PIX_FMT_0RGB32;
  1601. }else{
  1602. av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
  1603. return -1;
  1604. }
  1605. //printf("%d %d %d\n", f->chroma_h_shift, f->chroma_v_shift,f->avctx->pix_fmt);
  1606. if(f->version < 2){
  1607. context_count= read_quant_tables(c, f->quant_table);
  1608. if(context_count < 0){
  1609. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  1610. return -1;
  1611. }
  1612. }else if(f->version < 3){
  1613. f->slice_count= get_symbol(c, state, 0);
  1614. }else{
  1615. const uint8_t *p= c->bytestream_end;
  1616. for(f->slice_count = 0; f->slice_count < MAX_SLICES && 3 < p - c->bytestream_start; f->slice_count++){
  1617. int trailer = 3 + 5*!!f->ec;
  1618. int size = AV_RB24(p-trailer);
  1619. if(size + trailer > p - c->bytestream_start)
  1620. break;
  1621. p -= size + trailer;
  1622. }
  1623. }
  1624. if(f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0){
  1625. av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid\n", f->slice_count);
  1626. return -1;
  1627. }
  1628. for(j=0; j<f->slice_count; j++){
  1629. FFV1Context *fs= f->slice_context[j];
  1630. fs->ac= f->ac;
  1631. fs->packed_at_lsb= f->packed_at_lsb;
  1632. if(f->version == 2){
  1633. fs->slice_x = get_symbol(c, state, 0) *f->width ;
  1634. fs->slice_y = get_symbol(c, state, 0) *f->height;
  1635. fs->slice_width =(get_symbol(c, state, 0)+1)*f->width + fs->slice_x;
  1636. fs->slice_height=(get_symbol(c, state, 0)+1)*f->height + fs->slice_y;
  1637. fs->slice_x /= f->num_h_slices;
  1638. fs->slice_y /= f->num_v_slices;
  1639. fs->slice_width = fs->slice_width /f->num_h_slices - fs->slice_x;
  1640. fs->slice_height = fs->slice_height/f->num_v_slices - fs->slice_y;
  1641. if((unsigned)fs->slice_width > f->width || (unsigned)fs->slice_height > f->height)
  1642. return -1;
  1643. if( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
  1644. || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
  1645. return -1;
  1646. }
  1647. for(i=0; i<f->plane_count; i++){
  1648. PlaneContext * const p= &fs->plane[i];
  1649. if(f->version == 2){
  1650. int idx=get_symbol(c, state, 0);
  1651. if(idx > (unsigned)f->quant_table_count){
  1652. av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
  1653. return -1;
  1654. }
  1655. p->quant_table_index= idx;
  1656. memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
  1657. context_count= f->context_count[idx];
  1658. }else{
  1659. memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
  1660. }
  1661. if(f->version <= 2){
  1662. if(p->context_count < context_count){
  1663. av_freep(&p->state);
  1664. av_freep(&p->vlc_state);
  1665. }
  1666. p->context_count= context_count;
  1667. }
  1668. }
  1669. }
  1670. return 0;
  1671. }
  1672. static av_cold int decode_init(AVCodecContext *avctx)
  1673. {
  1674. FFV1Context *f = avctx->priv_data;
  1675. common_init(avctx);
  1676. if(avctx->extradata && read_extra_header(f) < 0)
  1677. return -1;
  1678. if(init_slice_contexts(f) < 0)
  1679. return -1;
  1680. return 0;
  1681. }
  1682. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt){
  1683. const uint8_t *buf = avpkt->data;
  1684. int buf_size = avpkt->size;
  1685. FFV1Context *f = avctx->priv_data;
  1686. RangeCoder * const c= &f->slice_context[0]->c;
  1687. AVFrame * const p= &f->picture;
  1688. int bytes_read, i;
  1689. uint8_t keystate= 128;
  1690. const uint8_t *buf_p;
  1691. AVFrame *picture = data;
  1692. /* release previously stored data */
  1693. if (p->data[0])
  1694. avctx->release_buffer(avctx, p);
  1695. ff_init_range_decoder(c, buf, buf_size);
  1696. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  1697. p->pict_type= AV_PICTURE_TYPE_I; //FIXME I vs. P
  1698. if(get_rac(c, &keystate)){
  1699. p->key_frame= 1;
  1700. f->key_frame_ok = 0;
  1701. if(read_header(f) < 0)
  1702. return -1;
  1703. f->key_frame_ok = 1;
  1704. }else{
  1705. if (!f->key_frame_ok) {
  1706. av_log(avctx, AV_LOG_ERROR, "Cant decode non keyframe without valid keyframe\n");
  1707. return AVERROR_INVALIDDATA;
  1708. }
  1709. p->key_frame= 0;
  1710. }
  1711. p->reference= 0;
  1712. if(avctx->get_buffer(avctx, p) < 0){
  1713. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1714. return -1;
  1715. }
  1716. if(avctx->debug&FF_DEBUG_PICT_INFO)
  1717. av_log(avctx, AV_LOG_ERROR, "keyframe:%d coder:%d\n", p->key_frame, f->ac);
  1718. buf_p= buf + buf_size;
  1719. for(i=f->slice_count-1; i>=0; i--){
  1720. FFV1Context *fs= f->slice_context[i];
  1721. int trailer = 3 + 5*!!f->ec;
  1722. int v;
  1723. if(i || f->version>2) v = AV_RB24(buf_p-trailer)+trailer;
  1724. else v = buf_p - c->bytestream_start;
  1725. if(buf_p - c->bytestream_start < v){
  1726. av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
  1727. return -1;
  1728. }
  1729. buf_p -= v;
  1730. if(f->ec){
  1731. unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
  1732. if(crc){
  1733. av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", crc);
  1734. }
  1735. }
  1736. if(i){
  1737. ff_init_range_decoder(&fs->c, buf_p, v);
  1738. }
  1739. }
  1740. avctx->execute(avctx, decode_slice, &f->slice_context[0], NULL, f->slice_count, sizeof(void*));
  1741. f->picture_number++;
  1742. *picture= *p;
  1743. *data_size = sizeof(AVFrame);
  1744. return buf_size;
  1745. }
  1746. AVCodec ff_ffv1_decoder = {
  1747. .name = "ffv1",
  1748. .type = AVMEDIA_TYPE_VIDEO,
  1749. .id = CODEC_ID_FFV1,
  1750. .priv_data_size = sizeof(FFV1Context),
  1751. .init = decode_init,
  1752. .close = common_end,
  1753. .decode = decode_frame,
  1754. .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/ |
  1755. CODEC_CAP_SLICE_THREADS,
  1756. .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  1757. };
  1758. #if CONFIG_FFV1_ENCODER
  1759. AVCodec ff_ffv1_encoder = {
  1760. .name = "ffv1",
  1761. .type = AVMEDIA_TYPE_VIDEO,
  1762. .id = CODEC_ID_FFV1,
  1763. .priv_data_size = sizeof(FFV1Context),
  1764. .init = encode_init,
  1765. .encode2 = encode_frame,
  1766. .close = common_end,
  1767. .capabilities = CODEC_CAP_SLICE_THREADS,
  1768. .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},
  1769. .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  1770. };
  1771. #endif