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.

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