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.

1870 lines
60KB

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