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.

1874 lines
60KB

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