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.

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