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
67KB

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