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.

1805 lines
58KB

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