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.

2023 lines
65KB

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