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.

2044 lines
66KB

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