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.

1899 lines
61KB

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