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.

1865 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_YUV420P9:
  754. if (!avctx->bits_per_raw_sample)
  755. s->bits_per_raw_sample = 9;
  756. case PIX_FMT_YUV420P10:
  757. case PIX_FMT_YUV422P10:
  758. s->packed_at_lsb = 1;
  759. if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample)
  760. s->bits_per_raw_sample = 10;
  761. case PIX_FMT_GRAY16:
  762. case PIX_FMT_YUV444P16:
  763. case PIX_FMT_YUV422P16:
  764. case PIX_FMT_YUV420P16:
  765. if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample) {
  766. s->bits_per_raw_sample = 16;
  767. } else if (!s->bits_per_raw_sample){
  768. s->bits_per_raw_sample = avctx->bits_per_raw_sample;
  769. }
  770. if(s->bits_per_raw_sample <=8){
  771. av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
  772. return -1;
  773. }
  774. if(!s->ac){
  775. av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample of more than 8 needs -coder 1 currently\n");
  776. return -1;
  777. }
  778. s->version= FFMAX(s->version, 1);
  779. case PIX_FMT_GRAY8:
  780. case PIX_FMT_YUV444P:
  781. case PIX_FMT_YUV440P:
  782. case PIX_FMT_YUV422P:
  783. case PIX_FMT_YUV420P:
  784. case PIX_FMT_YUV411P:
  785. case PIX_FMT_YUV410P:
  786. s->chroma_planes= av_pix_fmt_descriptors[avctx->pix_fmt].nb_components < 3 ? 0 : 1;
  787. s->colorspace= 0;
  788. break;
  789. case PIX_FMT_YUVA444P:
  790. case PIX_FMT_YUVA420P:
  791. s->chroma_planes= 1;
  792. s->colorspace= 0;
  793. s->transparency= 1;
  794. break;
  795. case PIX_FMT_RGB32:
  796. s->colorspace= 1;
  797. s->transparency= 1;
  798. break;
  799. case PIX_FMT_0RGB32:
  800. s->colorspace= 1;
  801. break;
  802. default:
  803. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  804. return -1;
  805. }
  806. if (s->transparency) {
  807. av_log(avctx, AV_LOG_WARNING, "Storing alpha plane, this will require a recent FFV1 decoder to playback!\n");
  808. }
  809. for(i=0; i<256; i++){
  810. s->quant_table_count=2;
  811. if(s->bits_per_raw_sample <=8){
  812. s->quant_tables[0][0][i]= quant11[i];
  813. s->quant_tables[0][1][i]= 11*quant11[i];
  814. s->quant_tables[0][2][i]= 11*11*quant11[i];
  815. s->quant_tables[1][0][i]= quant11[i];
  816. s->quant_tables[1][1][i]= 11*quant11[i];
  817. s->quant_tables[1][2][i]= 11*11*quant5 [i];
  818. s->quant_tables[1][3][i]= 5*11*11*quant5 [i];
  819. s->quant_tables[1][4][i]= 5*5*11*11*quant5 [i];
  820. }else{
  821. s->quant_tables[0][0][i]= quant9_10bit[i];
  822. s->quant_tables[0][1][i]= 11*quant9_10bit[i];
  823. s->quant_tables[0][2][i]= 11*11*quant9_10bit[i];
  824. s->quant_tables[1][0][i]= quant9_10bit[i];
  825. s->quant_tables[1][1][i]= 11*quant9_10bit[i];
  826. s->quant_tables[1][2][i]= 11*11*quant5_10bit[i];
  827. s->quant_tables[1][3][i]= 5*11*11*quant5_10bit[i];
  828. s->quant_tables[1][4][i]= 5*5*11*11*quant5_10bit[i];
  829. }
  830. }
  831. s->context_count[0]= (11*11*11+1)/2;
  832. s->context_count[1]= (11*11*5*5*5+1)/2;
  833. memcpy(s->quant_table, s->quant_tables[avctx->context_model], sizeof(s->quant_table));
  834. for(i=0; i<s->plane_count; i++){
  835. PlaneContext * const p= &s->plane[i];
  836. memcpy(p->quant_table, s->quant_table, sizeof(p->quant_table));
  837. p->quant_table_index= avctx->context_model;
  838. p->context_count= s->context_count[p->quant_table_index];
  839. }
  840. if(allocate_initial_states(s) < 0)
  841. return AVERROR(ENOMEM);
  842. avctx->coded_frame= &s->picture;
  843. if(!s->transparency)
  844. s->plane_count= 2;
  845. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  846. s->picture_number=0;
  847. if(avctx->flags & (CODEC_FLAG_PASS1|CODEC_FLAG_PASS2)){
  848. for(i=0; i<s->quant_table_count; i++){
  849. s->rc_stat2[i]= av_mallocz(s->context_count[i]*sizeof(*s->rc_stat2[i]));
  850. if(!s->rc_stat2[i])
  851. return AVERROR(ENOMEM);
  852. }
  853. }
  854. if(avctx->stats_in){
  855. char *p= avctx->stats_in;
  856. uint8_t best_state[256][256];
  857. int gob_count=0;
  858. char *next;
  859. av_assert0(s->version>=2);
  860. for(;;){
  861. for(j=0; j<256; j++){
  862. for(i=0; i<2; i++){
  863. s->rc_stat[j][i]= strtol(p, &next, 0);
  864. if(next==p){
  865. av_log(avctx, AV_LOG_ERROR, "2Pass file invalid at %d %d [%s]\n", j,i,p);
  866. return -1;
  867. }
  868. p=next;
  869. }
  870. }
  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. for(m=0; m<2; m++){
  875. s->rc_stat2[i][j][k][m]= strtol(p, &next, 0);
  876. if(next==p){
  877. av_log(avctx, AV_LOG_ERROR, "2Pass file invalid at %d %d %d %d [%s]\n", i,j,k,m,p);
  878. return -1;
  879. }
  880. p=next;
  881. }
  882. }
  883. }
  884. }
  885. gob_count= strtol(p, &next, 0);
  886. if(next==p || gob_count <0){
  887. av_log(avctx, AV_LOG_ERROR, "2Pass file invalid\n");
  888. return -1;
  889. }
  890. p=next;
  891. while(*p=='\n' || *p==' ') p++;
  892. if(p[0]==0) break;
  893. }
  894. sort_stt(s, s->state_transition);
  895. find_best_state(best_state, s->state_transition);
  896. for(i=0; i<s->quant_table_count; i++){
  897. for(j=0; j<s->context_count[i]; j++){
  898. for(k=0; k<32; k++){
  899. double p= 128;
  900. if(s->rc_stat2[i][j][k][0]+s->rc_stat2[i][j][k][1]){
  901. p=256.0*s->rc_stat2[i][j][k][1] / (s->rc_stat2[i][j][k][0]+s->rc_stat2[i][j][k][1]);
  902. }
  903. 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)];
  904. }
  905. }
  906. }
  907. }
  908. if(s->version>1){
  909. s->num_h_slices=2;
  910. s->num_v_slices=2;
  911. write_extra_header(s);
  912. }
  913. if(init_slice_contexts(s) < 0)
  914. return -1;
  915. if(init_slice_state(s) < 0)
  916. return -1;
  917. #define STATS_OUT_SIZE 1024*1024*6
  918. if(avctx->flags & CODEC_FLAG_PASS1){
  919. avctx->stats_out= av_mallocz(STATS_OUT_SIZE);
  920. for(i=0; i<s->quant_table_count; i++){
  921. for(j=0; j<s->slice_count; j++){
  922. FFV1Context *sf= s->slice_context[j];
  923. av_assert0(!sf->rc_stat2[i]);
  924. sf->rc_stat2[i]= av_mallocz(s->context_count[i]*sizeof(*sf->rc_stat2[i]));
  925. if(!sf->rc_stat2[i])
  926. return AVERROR(ENOMEM);
  927. }
  928. }
  929. }
  930. return 0;
  931. }
  932. #endif /* CONFIG_FFV1_ENCODER */
  933. static void clear_state(FFV1Context *f){
  934. int i, si, j;
  935. for(si=0; si<f->slice_count; si++){
  936. FFV1Context *fs= f->slice_context[si];
  937. for(i=0; i<f->plane_count; i++){
  938. PlaneContext *p= &fs->plane[i];
  939. p->interlace_bit_state[0]= 128;
  940. p->interlace_bit_state[1]= 128;
  941. if(fs->ac){
  942. if(f->initial_states[p->quant_table_index]){
  943. memcpy(p->state, f->initial_states[p->quant_table_index], CONTEXT_SIZE*p->context_count);
  944. }else
  945. memset(p->state, 128, CONTEXT_SIZE*p->context_count);
  946. }else{
  947. for(j=0; j<p->context_count; j++){
  948. p->vlc_state[j].drift= 0;
  949. p->vlc_state[j].error_sum= 4; //FFMAX((RANGE + 32)/64, 2);
  950. p->vlc_state[j].bias= 0;
  951. p->vlc_state[j].count= 1;
  952. }
  953. }
  954. }
  955. }
  956. }
  957. #if CONFIG_FFV1_ENCODER
  958. static int encode_slice(AVCodecContext *c, void *arg){
  959. FFV1Context *fs= *(void**)arg;
  960. FFV1Context *f= fs->avctx->priv_data;
  961. int width = fs->slice_width;
  962. int height= fs->slice_height;
  963. int x= fs->slice_x;
  964. int y= fs->slice_y;
  965. AVFrame * const p= &f->picture;
  966. const int ps= (f->bits_per_raw_sample>8)+1;
  967. if(f->colorspace==0){
  968. const int chroma_width = -((-width )>>f->chroma_h_shift);
  969. const int chroma_height= -((-height)>>f->chroma_v_shift);
  970. const int cx= x>>f->chroma_h_shift;
  971. const int cy= y>>f->chroma_v_shift;
  972. encode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0);
  973. if (f->chroma_planes){
  974. encode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
  975. encode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1);
  976. }
  977. if (fs->transparency)
  978. encode_plane(fs, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2);
  979. }else{
  980. encode_rgb_frame(fs, (uint32_t*)(p->data[0]) + ps*x + y*(p->linesize[0]/4), width, height, p->linesize[0]/4);
  981. }
  982. emms_c();
  983. return 0;
  984. }
  985. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  986. const AVFrame *pict, int *got_packet)
  987. {
  988. FFV1Context *f = avctx->priv_data;
  989. RangeCoder * const c= &f->slice_context[0]->c;
  990. AVFrame * const p= &f->picture;
  991. int used_count= 0;
  992. uint8_t keystate=128;
  993. uint8_t *buf_p;
  994. int i, ret;
  995. if (!pkt->data &&
  996. (ret = av_new_packet(pkt, avctx->width*avctx->height*((8*2+1+1)*4)/8
  997. + FF_MIN_BUFFER_SIZE)) < 0) {
  998. av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
  999. return ret;
  1000. }
  1001. ff_init_range_encoder(c, pkt->data, pkt->size);
  1002. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  1003. *p = *pict;
  1004. p->pict_type= AV_PICTURE_TYPE_I;
  1005. if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){
  1006. put_rac(c, &keystate, 1);
  1007. p->key_frame= 1;
  1008. f->gob_count++;
  1009. write_header(f);
  1010. clear_state(f);
  1011. }else{
  1012. put_rac(c, &keystate, 0);
  1013. p->key_frame= 0;
  1014. }
  1015. if(!f->ac){
  1016. used_count += ff_rac_terminate(c);
  1017. //printf("pos=%d\n", used_count);
  1018. init_put_bits(&f->slice_context[0]->pb, pkt->data + used_count, pkt->size - used_count);
  1019. }else if (f->ac>1){
  1020. int i;
  1021. for(i=1; i<256; i++){
  1022. c->one_state[i]= f->state_transition[i];
  1023. c->zero_state[256-i]= 256-c->one_state[i];
  1024. }
  1025. }
  1026. for(i=1; i<f->slice_count; i++){
  1027. FFV1Context *fs= f->slice_context[i];
  1028. uint8_t *start = pkt->data + (pkt->size-used_count)*i/f->slice_count;
  1029. int len = pkt->size/f->slice_count;
  1030. if(fs->ac){
  1031. ff_init_range_encoder(&fs->c, start, len);
  1032. }else{
  1033. init_put_bits(&fs->pb, start, len);
  1034. }
  1035. }
  1036. avctx->execute(avctx, encode_slice, &f->slice_context[0], NULL, f->slice_count, sizeof(void*));
  1037. buf_p = pkt->data;
  1038. for(i=0; i<f->slice_count; i++){
  1039. FFV1Context *fs= f->slice_context[i];
  1040. int bytes;
  1041. if(fs->ac){
  1042. uint8_t state=128;
  1043. put_rac(&fs->c, &state, 0);
  1044. bytes= ff_rac_terminate(&fs->c);
  1045. }else{
  1046. flush_put_bits(&fs->pb); //nicer padding FIXME
  1047. bytes= used_count + (put_bits_count(&fs->pb)+7)/8;
  1048. used_count= 0;
  1049. }
  1050. if(i>0){
  1051. av_assert0(bytes < pkt->size/f->slice_count);
  1052. memmove(buf_p, fs->ac ? fs->c.bytestream_start : fs->pb.buf, bytes);
  1053. av_assert0(bytes < (1<<24));
  1054. AV_WB24(buf_p+bytes, bytes);
  1055. bytes+=3;
  1056. }
  1057. buf_p += bytes;
  1058. }
  1059. if((avctx->flags&CODEC_FLAG_PASS1) && (f->picture_number&31)==0){
  1060. int j, k, m;
  1061. char *p= avctx->stats_out;
  1062. char *end= p + STATS_OUT_SIZE;
  1063. memset(f->rc_stat, 0, sizeof(f->rc_stat));
  1064. for(i=0; i<f->quant_table_count; i++)
  1065. memset(f->rc_stat2[i], 0, f->context_count[i]*sizeof(*f->rc_stat2[i]));
  1066. for(j=0; j<f->slice_count; j++){
  1067. FFV1Context *fs= f->slice_context[j];
  1068. for(i=0; i<256; i++){
  1069. f->rc_stat[i][0] += fs->rc_stat[i][0];
  1070. f->rc_stat[i][1] += fs->rc_stat[i][1];
  1071. }
  1072. for(i=0; i<f->quant_table_count; i++){
  1073. for(k=0; k<f->context_count[i]; k++){
  1074. for(m=0; m<32; m++){
  1075. f->rc_stat2[i][k][m][0] += fs->rc_stat2[i][k][m][0];
  1076. f->rc_stat2[i][k][m][1] += fs->rc_stat2[i][k][m][1];
  1077. }
  1078. }
  1079. }
  1080. }
  1081. for(j=0; j<256; j++){
  1082. snprintf(p, end-p, "%"PRIu64" %"PRIu64" ", f->rc_stat[j][0], f->rc_stat[j][1]);
  1083. p+= strlen(p);
  1084. }
  1085. snprintf(p, end-p, "\n");
  1086. for(i=0; i<f->quant_table_count; i++){
  1087. for(j=0; j<f->context_count[i]; j++){
  1088. for(m=0; m<32; m++){
  1089. snprintf(p, end-p, "%"PRIu64" %"PRIu64" ", f->rc_stat2[i][j][m][0], f->rc_stat2[i][j][m][1]);
  1090. p+= strlen(p);
  1091. }
  1092. }
  1093. }
  1094. snprintf(p, end-p, "%d\n", f->gob_count);
  1095. } else if(avctx->flags&CODEC_FLAG_PASS1)
  1096. avctx->stats_out[0] = '\0';
  1097. f->picture_number++;
  1098. pkt->size = buf_p - pkt->data;
  1099. pkt->flags |= AV_PKT_FLAG_KEY*p->key_frame;
  1100. *got_packet = 1;
  1101. return 0;
  1102. }
  1103. #endif /* CONFIG_FFV1_ENCODER */
  1104. static av_cold int common_end(AVCodecContext *avctx){
  1105. FFV1Context *s = avctx->priv_data;
  1106. int i, j;
  1107. if (avctx->codec->decode && s->picture.data[0])
  1108. avctx->release_buffer(avctx, &s->picture);
  1109. for(j=0; j<s->slice_count; j++){
  1110. FFV1Context *fs= s->slice_context[j];
  1111. for(i=0; i<s->plane_count; i++){
  1112. PlaneContext *p= &fs->plane[i];
  1113. av_freep(&p->state);
  1114. av_freep(&p->vlc_state);
  1115. }
  1116. av_freep(&fs->sample_buffer);
  1117. }
  1118. av_freep(&avctx->stats_out);
  1119. for(j=0; j<s->quant_table_count; j++){
  1120. av_freep(&s->initial_states[j]);
  1121. for(i=0; i<s->slice_count; i++){
  1122. FFV1Context *sf= s->slice_context[i];
  1123. av_freep(&sf->rc_stat2[j]);
  1124. }
  1125. av_freep(&s->rc_stat2[j]);
  1126. }
  1127. for(i=0; i<s->slice_count; i++){
  1128. av_freep(&s->slice_context[i]);
  1129. }
  1130. return 0;
  1131. }
  1132. static av_always_inline void decode_line(FFV1Context *s, int w,
  1133. int16_t *sample[2],
  1134. int plane_index, int bits)
  1135. {
  1136. PlaneContext * const p= &s->plane[plane_index];
  1137. RangeCoder * const c= &s->c;
  1138. int x;
  1139. int run_count=0;
  1140. int run_mode=0;
  1141. int run_index= s->run_index;
  1142. for(x=0; x<w; x++){
  1143. int diff, context, sign;
  1144. context= get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
  1145. if(context < 0){
  1146. context= -context;
  1147. sign=1;
  1148. }else
  1149. sign=0;
  1150. av_assert2(context < p->context_count);
  1151. if(s->ac){
  1152. diff= get_symbol_inline(c, p->state[context], 1);
  1153. }else{
  1154. if(context == 0 && run_mode==0) run_mode=1;
  1155. if(run_mode){
  1156. if(run_count==0 && run_mode==1){
  1157. if(get_bits1(&s->gb)){
  1158. run_count = 1<<ff_log2_run[run_index];
  1159. if(x + run_count <= w) run_index++;
  1160. }else{
  1161. if(ff_log2_run[run_index]) run_count = get_bits(&s->gb, ff_log2_run[run_index]);
  1162. else run_count=0;
  1163. if(run_index) run_index--;
  1164. run_mode=2;
  1165. }
  1166. }
  1167. run_count--;
  1168. if(run_count < 0){
  1169. run_mode=0;
  1170. run_count=0;
  1171. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  1172. if(diff>=0) diff++;
  1173. }else
  1174. diff=0;
  1175. }else
  1176. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  1177. // 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));
  1178. }
  1179. if(sign) diff= -diff;
  1180. sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
  1181. }
  1182. s->run_index= run_index;
  1183. }
  1184. static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  1185. int x, y;
  1186. int16_t *sample[2];
  1187. sample[0]=s->sample_buffer +3;
  1188. sample[1]=s->sample_buffer+w+6+3;
  1189. s->run_index=0;
  1190. memset(s->sample_buffer, 0, 2*(w+6)*sizeof(*s->sample_buffer));
  1191. for(y=0; y<h; y++){
  1192. int16_t *temp = sample[0]; //FIXME try a normal buffer
  1193. sample[0]= sample[1];
  1194. sample[1]= temp;
  1195. sample[1][-1]= sample[0][0 ];
  1196. sample[0][ w]= sample[0][w-1];
  1197. //{START_TIMER
  1198. if(s->avctx->bits_per_raw_sample <= 8){
  1199. decode_line(s, w, sample, plane_index, 8);
  1200. for(x=0; x<w; x++){
  1201. src[x + stride*y]= sample[1][x];
  1202. }
  1203. }else{
  1204. decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
  1205. if(s->packed_at_lsb){
  1206. for(x=0; x<w; x++){
  1207. ((uint16_t*)(src + stride*y))[x]= sample[1][x];
  1208. }
  1209. }else{
  1210. for(x=0; x<w; x++){
  1211. ((uint16_t*)(src + stride*y))[x]= sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
  1212. }
  1213. }
  1214. }
  1215. //STOP_TIMER("decode-line")}
  1216. }
  1217. }
  1218. static void decode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  1219. int x, y, p;
  1220. int16_t *sample[4][2];
  1221. for(x=0; x<4; x++){
  1222. sample[x][0] = s->sample_buffer + x*2 *(w+6) + 3;
  1223. sample[x][1] = s->sample_buffer + (x*2+1)*(w+6) + 3;
  1224. }
  1225. s->run_index=0;
  1226. memset(s->sample_buffer, 0, 8*(w+6)*sizeof(*s->sample_buffer));
  1227. for(y=0; y<h; y++){
  1228. for(p=0; p<3 + s->transparency; p++){
  1229. int16_t *temp = sample[p][0]; //FIXME try a normal buffer
  1230. sample[p][0]= sample[p][1];
  1231. sample[p][1]= temp;
  1232. sample[p][1][-1]= sample[p][0][0 ];
  1233. sample[p][0][ w]= sample[p][0][w-1];
  1234. decode_line(s, w, sample[p], (p+1)/2, 9);
  1235. }
  1236. for(x=0; x<w; x++){
  1237. int g= sample[0][1][x];
  1238. int b= sample[1][1][x];
  1239. int r= sample[2][1][x];
  1240. int a= sample[3][1][x];
  1241. // assert(g>=0 && b>=0 && r>=0);
  1242. // assert(g<256 && b<512 && r<512);
  1243. b -= 0x100;
  1244. r -= 0x100;
  1245. g -= (b + r)>>2;
  1246. b += g;
  1247. r += g;
  1248. src[x + stride*y]= b + (g<<8) + (r<<16) + (a<<24);
  1249. }
  1250. }
  1251. }
  1252. static int decode_slice(AVCodecContext *c, void *arg){
  1253. FFV1Context *fs= *(void**)arg;
  1254. FFV1Context *f= fs->avctx->priv_data;
  1255. int width = fs->slice_width;
  1256. int height= fs->slice_height;
  1257. int x= fs->slice_x;
  1258. int y= fs->slice_y;
  1259. const int ps= (c->bits_per_raw_sample>8)+1;
  1260. AVFrame * const p= &f->picture;
  1261. av_assert1(width && height);
  1262. if(f->colorspace==0){
  1263. const int chroma_width = -((-width )>>f->chroma_h_shift);
  1264. const int chroma_height= -((-height)>>f->chroma_v_shift);
  1265. const int cx= x>>f->chroma_h_shift;
  1266. const int cy= y>>f->chroma_v_shift;
  1267. decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0);
  1268. if (f->chroma_planes){
  1269. decode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
  1270. decode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1);
  1271. }
  1272. if (fs->transparency)
  1273. decode_plane(fs, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2);
  1274. }else{
  1275. decode_rgb_frame(fs, (uint32_t*)p->data[0] + ps*x + y*(p->linesize[0]/4), width, height, p->linesize[0]/4);
  1276. }
  1277. emms_c();
  1278. return 0;
  1279. }
  1280. static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){
  1281. int v;
  1282. int i=0;
  1283. uint8_t state[CONTEXT_SIZE];
  1284. memset(state, 128, sizeof(state));
  1285. for(v=0; i<128 ; v++){
  1286. int len= get_symbol(c, state, 0) + 1;
  1287. if(len + i > 128) return -1;
  1288. while(len--){
  1289. quant_table[i] = scale*v;
  1290. i++;
  1291. //printf("%2d ",v);
  1292. //if(i%16==0) printf("\n");
  1293. }
  1294. }
  1295. for(i=1; i<128; i++){
  1296. quant_table[256-i]= -quant_table[i];
  1297. }
  1298. quant_table[128]= -quant_table[127];
  1299. return 2*v - 1;
  1300. }
  1301. static int read_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256]){
  1302. int i;
  1303. int context_count=1;
  1304. for(i=0; i<5; i++){
  1305. context_count*= read_quant_table(c, quant_table[i], context_count);
  1306. if(context_count > 32768U){
  1307. return -1;
  1308. }
  1309. }
  1310. return (context_count+1)/2;
  1311. }
  1312. static int read_extra_header(FFV1Context *f){
  1313. RangeCoder * const c= &f->c;
  1314. uint8_t state[CONTEXT_SIZE];
  1315. int i, j, k;
  1316. uint8_t state2[32][CONTEXT_SIZE];
  1317. memset(state2, 128, sizeof(state2));
  1318. memset(state, 128, sizeof(state));
  1319. ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
  1320. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  1321. f->version= get_symbol(c, state, 0);
  1322. f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
  1323. if(f->ac>1){
  1324. for(i=1; i<256; i++){
  1325. f->state_transition[i]= get_symbol(c, state, 1) + c->one_state[i];
  1326. }
  1327. }
  1328. f->colorspace= get_symbol(c, state, 0); //YUV cs type
  1329. f->avctx->bits_per_raw_sample= get_symbol(c, state, 0);
  1330. get_rac(c, state); //no chroma = false
  1331. f->chroma_h_shift= get_symbol(c, state, 0);
  1332. f->chroma_v_shift= get_symbol(c, state, 0);
  1333. f->transparency= get_rac(c, state);
  1334. f->plane_count= 2 + f->transparency;
  1335. f->num_h_slices= 1 + get_symbol(c, state, 0);
  1336. f->num_v_slices= 1 + get_symbol(c, state, 0);
  1337. if(f->num_h_slices > (unsigned)f->width || f->num_v_slices > (unsigned)f->height){
  1338. av_log(f->avctx, AV_LOG_ERROR, "too many slices\n");
  1339. return -1;
  1340. }
  1341. f->quant_table_count= get_symbol(c, state, 0);
  1342. if(f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
  1343. return -1;
  1344. for(i=0; i<f->quant_table_count; i++){
  1345. if((f->context_count[i]= read_quant_tables(c, f->quant_tables[i])) < 0){
  1346. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  1347. return -1;
  1348. }
  1349. }
  1350. if(allocate_initial_states(f) < 0)
  1351. return AVERROR(ENOMEM);
  1352. for(i=0; i<f->quant_table_count; i++){
  1353. if(get_rac(c, state)){
  1354. for(j=0; j<f->context_count[i]; j++){
  1355. for(k=0; k<CONTEXT_SIZE; k++){
  1356. int pred= j ? f->initial_states[i][j-1][k] : 128;
  1357. f->initial_states[i][j][k]= (pred+get_symbol(c, state2[k], 1))&0xFF;
  1358. }
  1359. }
  1360. }
  1361. }
  1362. return 0;
  1363. }
  1364. static int read_header(FFV1Context *f){
  1365. uint8_t state[CONTEXT_SIZE];
  1366. int i, j, context_count;
  1367. RangeCoder * const c= &f->slice_context[0]->c;
  1368. memset(state, 128, sizeof(state));
  1369. if(f->version < 2){
  1370. f->version= get_symbol(c, state, 0);
  1371. f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
  1372. if(f->ac>1){
  1373. for(i=1; i<256; i++){
  1374. f->state_transition[i]= get_symbol(c, state, 1) + c->one_state[i];
  1375. }
  1376. }
  1377. f->colorspace= get_symbol(c, state, 0); //YUV cs type
  1378. if(f->version>0)
  1379. f->avctx->bits_per_raw_sample= get_symbol(c, state, 0);
  1380. f->chroma_planes= get_rac(c, state);
  1381. f->chroma_h_shift= get_symbol(c, state, 0);
  1382. f->chroma_v_shift= get_symbol(c, state, 0);
  1383. f->transparency= get_rac(c, state);
  1384. f->plane_count= 2 + f->transparency;
  1385. }
  1386. if(f->colorspace==0){
  1387. if(!f->transparency && !f->chroma_planes){
  1388. if (f->avctx->bits_per_raw_sample<=8)
  1389. f->avctx->pix_fmt= PIX_FMT_GRAY8;
  1390. else
  1391. f->avctx->pix_fmt= PIX_FMT_GRAY16;
  1392. }else if(f->avctx->bits_per_raw_sample<=8 && !f->transparency){
  1393. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  1394. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
  1395. case 0x01: f->avctx->pix_fmt= PIX_FMT_YUV440P; break;
  1396. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
  1397. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
  1398. case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
  1399. case 0x22: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
  1400. default:
  1401. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  1402. return -1;
  1403. }
  1404. }else if(f->avctx->bits_per_raw_sample<=8 && f->transparency){
  1405. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  1406. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUVA444P; break;
  1407. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUVA420P; break;
  1408. default:
  1409. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  1410. return -1;
  1411. }
  1412. }else if(f->avctx->bits_per_raw_sample==9) {
  1413. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  1414. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P16; break;
  1415. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P16; break;
  1416. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P9 ; f->packed_at_lsb=1; break;
  1417. default:
  1418. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  1419. return -1;
  1420. }
  1421. }else if(f->avctx->bits_per_raw_sample==10) {
  1422. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  1423. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P16; break;
  1424. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P10; f->packed_at_lsb=1; break;
  1425. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P10; f->packed_at_lsb=1; break;
  1426. default:
  1427. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  1428. return -1;
  1429. }
  1430. }else {
  1431. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  1432. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P16; break;
  1433. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P16; break;
  1434. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P16; break;
  1435. default:
  1436. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  1437. return -1;
  1438. }
  1439. }
  1440. }else if(f->colorspace==1){
  1441. if(f->chroma_h_shift || f->chroma_v_shift){
  1442. av_log(f->avctx, AV_LOG_ERROR, "chroma subsampling not supported in this colorspace\n");
  1443. return -1;
  1444. }
  1445. if(f->transparency) f->avctx->pix_fmt= PIX_FMT_RGB32;
  1446. else f->avctx->pix_fmt= PIX_FMT_0RGB32;
  1447. }else{
  1448. av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
  1449. return -1;
  1450. }
  1451. //printf("%d %d %d\n", f->chroma_h_shift, f->chroma_v_shift,f->avctx->pix_fmt);
  1452. if(f->version < 2){
  1453. context_count= read_quant_tables(c, f->quant_table);
  1454. if(context_count < 0){
  1455. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  1456. return -1;
  1457. }
  1458. }else{
  1459. f->slice_count= get_symbol(c, state, 0);
  1460. if(f->slice_count > (unsigned)MAX_SLICES)
  1461. return -1;
  1462. }
  1463. for(j=0; j<f->slice_count; j++){
  1464. FFV1Context *fs= f->slice_context[j];
  1465. fs->ac= f->ac;
  1466. fs->packed_at_lsb= f->packed_at_lsb;
  1467. if(f->version >= 2){
  1468. fs->slice_x = get_symbol(c, state, 0) *f->width ;
  1469. fs->slice_y = get_symbol(c, state, 0) *f->height;
  1470. fs->slice_width =(get_symbol(c, state, 0)+1)*f->width + fs->slice_x;
  1471. fs->slice_height=(get_symbol(c, state, 0)+1)*f->height + fs->slice_y;
  1472. fs->slice_x /= f->num_h_slices;
  1473. fs->slice_y /= f->num_v_slices;
  1474. fs->slice_width = fs->slice_width /f->num_h_slices - fs->slice_x;
  1475. fs->slice_height = fs->slice_height/f->num_v_slices - fs->slice_y;
  1476. if((unsigned)fs->slice_width > f->width || (unsigned)fs->slice_height > f->height)
  1477. return -1;
  1478. if( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
  1479. || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
  1480. return -1;
  1481. }
  1482. for(i=0; i<f->plane_count; i++){
  1483. PlaneContext * const p= &fs->plane[i];
  1484. if(f->version >= 2){
  1485. int idx=get_symbol(c, state, 0);
  1486. if(idx > (unsigned)f->quant_table_count){
  1487. av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
  1488. return -1;
  1489. }
  1490. p->quant_table_index= idx;
  1491. memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
  1492. context_count= f->context_count[idx];
  1493. }else{
  1494. memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
  1495. }
  1496. if(p->context_count < context_count){
  1497. av_freep(&p->state);
  1498. av_freep(&p->vlc_state);
  1499. }
  1500. p->context_count= context_count;
  1501. }
  1502. }
  1503. return 0;
  1504. }
  1505. static av_cold int decode_init(AVCodecContext *avctx)
  1506. {
  1507. FFV1Context *f = avctx->priv_data;
  1508. common_init(avctx);
  1509. if(avctx->extradata && read_extra_header(f) < 0)
  1510. return -1;
  1511. if(init_slice_contexts(f) < 0)
  1512. return -1;
  1513. return 0;
  1514. }
  1515. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt){
  1516. const uint8_t *buf = avpkt->data;
  1517. int buf_size = avpkt->size;
  1518. FFV1Context *f = avctx->priv_data;
  1519. RangeCoder * const c= &f->slice_context[0]->c;
  1520. AVFrame * const p= &f->picture;
  1521. int bytes_read, i;
  1522. uint8_t keystate= 128;
  1523. const uint8_t *buf_p;
  1524. AVFrame *picture = data;
  1525. /* release previously stored data */
  1526. if (p->data[0])
  1527. avctx->release_buffer(avctx, p);
  1528. ff_init_range_decoder(c, buf, buf_size);
  1529. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  1530. p->pict_type= AV_PICTURE_TYPE_I; //FIXME I vs. P
  1531. if(get_rac(c, &keystate)){
  1532. p->key_frame= 1;
  1533. if(read_header(f) < 0)
  1534. return -1;
  1535. if(init_slice_state(f) < 0)
  1536. return -1;
  1537. clear_state(f);
  1538. }else{
  1539. p->key_frame= 0;
  1540. }
  1541. if(f->ac>1){
  1542. int i;
  1543. for(i=1; i<256; i++){
  1544. c->one_state[i]= f->state_transition[i];
  1545. c->zero_state[256-i]= 256-c->one_state[i];
  1546. }
  1547. }
  1548. p->reference= 0;
  1549. if(avctx->get_buffer(avctx, p) < 0){
  1550. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1551. return -1;
  1552. }
  1553. if(avctx->debug&FF_DEBUG_PICT_INFO)
  1554. av_log(avctx, AV_LOG_ERROR, "keyframe:%d coder:%d\n", p->key_frame, f->ac);
  1555. if(!f->ac){
  1556. bytes_read = c->bytestream - c->bytestream_start - 1;
  1557. if(bytes_read ==0) av_log(avctx, AV_LOG_ERROR, "error at end of AC stream\n"); //FIXME
  1558. //printf("pos=%d\n", bytes_read);
  1559. init_get_bits(&f->slice_context[0]->gb, buf + bytes_read, (buf_size - bytes_read) * 8);
  1560. } else {
  1561. bytes_read = 0; /* avoid warning */
  1562. }
  1563. buf_p= buf + buf_size;
  1564. for(i=f->slice_count-1; i>0; i--){
  1565. FFV1Context *fs= f->slice_context[i];
  1566. int v= AV_RB24(buf_p-3)+3;
  1567. if(buf_p - buf <= v){
  1568. av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
  1569. return -1;
  1570. }
  1571. buf_p -= v;
  1572. if(fs->ac){
  1573. ff_init_range_decoder(&fs->c, buf_p, v);
  1574. }else{
  1575. init_get_bits(&fs->gb, buf_p, v * 8);
  1576. }
  1577. }
  1578. avctx->execute(avctx, decode_slice, &f->slice_context[0], NULL, f->slice_count, sizeof(void*));
  1579. f->picture_number++;
  1580. *picture= *p;
  1581. *data_size = sizeof(AVFrame);
  1582. return buf_size;
  1583. }
  1584. AVCodec ff_ffv1_decoder = {
  1585. .name = "ffv1",
  1586. .type = AVMEDIA_TYPE_VIDEO,
  1587. .id = CODEC_ID_FFV1,
  1588. .priv_data_size = sizeof(FFV1Context),
  1589. .init = decode_init,
  1590. .close = common_end,
  1591. .decode = decode_frame,
  1592. .capabilities = CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/ | CODEC_CAP_SLICE_THREADS,
  1593. .long_name= NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  1594. };
  1595. #if CONFIG_FFV1_ENCODER
  1596. AVCodec ff_ffv1_encoder = {
  1597. .name = "ffv1",
  1598. .type = AVMEDIA_TYPE_VIDEO,
  1599. .id = CODEC_ID_FFV1,
  1600. .priv_data_size = sizeof(FFV1Context),
  1601. .init = encode_init,
  1602. .encode2 = encode_frame,
  1603. .close = common_end,
  1604. .capabilities = CODEC_CAP_SLICE_THREADS,
  1605. .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_YUV420P9, PIX_FMT_YUV420P10, PIX_FMT_YUV422P10, PIX_FMT_GRAY16, PIX_FMT_GRAY8, PIX_FMT_NONE},
  1606. .long_name= NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  1607. };
  1608. #endif