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.

1764 lines
56KB

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