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.

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