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.

1829 lines
59KB

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