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.

1155 lines
35KB

  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 libavcodec/ffv1.c
  24. * FF Video Codec 1 (an experimental 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. #define MAX_PLANES 4
  34. #define CONTEXT_SIZE 32
  35. extern const uint8_t ff_log2_run[32];
  36. static const int8_t quant3[256]={
  37. 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  38. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  39. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  40. 1, 1, 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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  107. };
  108. static const int8_t quant9[256]={
  109. 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  110. 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  111. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  112. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  113. 4, 4, 4, 4, 4, 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,-3,-3,-3,-3,
  124. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-1,-1,
  125. };
  126. static const int8_t quant9_10bit[256]={
  127. 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
  128. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3,
  129. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 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6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  173. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  174. -6,-6,-6,-6,-6,-6,-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,-5,
  176. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  177. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  178. -4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,-2,-2,-1,
  179. };
  180. typedef struct VlcState{
  181. int16_t drift;
  182. uint16_t error_sum;
  183. int8_t bias;
  184. uint8_t count;
  185. } VlcState;
  186. typedef struct PlaneContext{
  187. int context_count;
  188. uint8_t (*state)[CONTEXT_SIZE];
  189. VlcState *vlc_state;
  190. uint8_t interlace_bit_state[2];
  191. } PlaneContext;
  192. typedef struct FFV1Context{
  193. AVCodecContext *avctx;
  194. RangeCoder c;
  195. GetBitContext gb;
  196. PutBitContext pb;
  197. int version;
  198. int width, height;
  199. int chroma_h_shift, chroma_v_shift;
  200. int flags;
  201. int picture_number;
  202. AVFrame picture;
  203. int plane_count;
  204. int ac; ///< 1-> CABAC 0-> golomb rice
  205. PlaneContext plane[MAX_PLANES];
  206. int16_t quant_table[5][256];
  207. int run_index;
  208. int colorspace;
  209. DSPContext dsp;
  210. }FFV1Context;
  211. static av_always_inline int fold(int diff, int bits){
  212. if(bits==8)
  213. diff= (int8_t)diff;
  214. else{
  215. diff+= 1<<(bits-1);
  216. diff&=(1<<bits)-1;
  217. diff-= 1<<(bits-1);
  218. }
  219. return diff;
  220. }
  221. static inline int predict(int_fast16_t *src, int_fast16_t *last){
  222. const int LT= last[-1];
  223. const int T= last[ 0];
  224. const int L = src[-1];
  225. return mid_pred(L, L + T - LT, T);
  226. }
  227. static inline int get_context(FFV1Context *f, int_fast16_t *src, int_fast16_t *last, int_fast16_t *last2){
  228. const int LT= last[-1];
  229. const int T= last[ 0];
  230. const int RT= last[ 1];
  231. const int L = src[-1];
  232. if(f->quant_table[3][127]){
  233. const int TT= last2[0];
  234. const int LL= src[-2];
  235. return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF]
  236. +f->quant_table[3][(LL-L) & 0xFF] + f->quant_table[4][(TT-T) & 0xFF];
  237. }else
  238. return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF];
  239. }
  240. static inline void put_symbol_inline(RangeCoder *c, uint8_t *state, int v, int is_signed){
  241. int i;
  242. if(v){
  243. const int a= FFABS(v);
  244. const int e= av_log2(a);
  245. put_rac(c, state+0, 0);
  246. if(e<=9){
  247. for(i=0; i<e; i++){
  248. put_rac(c, state+1+i, 1); //1..10
  249. }
  250. put_rac(c, state+1+i, 0);
  251. for(i=e-1; i>=0; i--){
  252. put_rac(c, state+22+i, (a>>i)&1); //22..31
  253. }
  254. if(is_signed)
  255. put_rac(c, state+11 + e, v < 0); //11..21
  256. }else{
  257. for(i=0; i<e; i++){
  258. put_rac(c, state+1+FFMIN(i,9), 1); //1..10
  259. }
  260. put_rac(c, state+1+9, 0);
  261. for(i=e-1; i>=0; i--){
  262. put_rac(c, state+22+FFMIN(i,9), (a>>i)&1); //22..31
  263. }
  264. if(is_signed)
  265. put_rac(c, state+11 + 10, v < 0); //11..21
  266. }
  267. }else{
  268. put_rac(c, state+0, 1);
  269. }
  270. }
  271. static void av_noinline put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
  272. put_symbol_inline(c, state, v, is_signed);
  273. }
  274. static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state, int is_signed){
  275. if(get_rac(c, state+0))
  276. return 0;
  277. else{
  278. int i, e, a;
  279. e= 0;
  280. while(get_rac(c, state+1 + FFMIN(e,9))){ //1..10
  281. e++;
  282. }
  283. a= 1;
  284. for(i=e-1; i>=0; i--){
  285. a += a + get_rac(c, state+22 + FFMIN(i,9)); //22..31
  286. }
  287. e= -(is_signed && get_rac(c, state+11 + FFMIN(e, 10))); //11..21
  288. return (a^e)-e;
  289. }
  290. }
  291. static int av_noinline get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
  292. return get_symbol_inline(c, state, is_signed);
  293. }
  294. static inline void update_vlc_state(VlcState * const state, const int v){
  295. int drift= state->drift;
  296. int count= state->count;
  297. state->error_sum += FFABS(v);
  298. drift += v;
  299. if(count == 128){ //FIXME variable
  300. count >>= 1;
  301. drift >>= 1;
  302. state->error_sum >>= 1;
  303. }
  304. count++;
  305. if(drift <= -count){
  306. if(state->bias > -128) state->bias--;
  307. drift += count;
  308. if(drift <= -count)
  309. drift= -count + 1;
  310. }else if(drift > 0){
  311. if(state->bias < 127) state->bias++;
  312. drift -= count;
  313. if(drift > 0)
  314. drift= 0;
  315. }
  316. state->drift= drift;
  317. state->count= count;
  318. }
  319. static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int v, int bits){
  320. int i, k, code;
  321. //printf("final: %d ", v);
  322. v = fold(v - state->bias, bits);
  323. i= state->count;
  324. k=0;
  325. while(i < state->error_sum){ //FIXME optimize
  326. k++;
  327. i += i;
  328. }
  329. assert(k<=8);
  330. #if 0 // JPEG LS
  331. if(k==0 && 2*state->drift <= - state->count) code= v ^ (-1);
  332. else code= v;
  333. #else
  334. code= v ^ ((2*state->drift + state->count)>>31);
  335. #endif
  336. //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);
  337. set_sr_golomb(pb, code, k, 12, bits);
  338. update_vlc_state(state, v);
  339. }
  340. static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state, int bits){
  341. int k, i, v, ret;
  342. i= state->count;
  343. k=0;
  344. while(i < state->error_sum){ //FIXME optimize
  345. k++;
  346. i += i;
  347. }
  348. assert(k<=8);
  349. v= get_sr_golomb(gb, k, 12, bits);
  350. //printf("v:%d bias:%d error:%d drift:%d count:%d k:%d", v, state->bias, state->error_sum, state->drift, state->count, k);
  351. #if 0 // JPEG LS
  352. if(k==0 && 2*state->drift <= - state->count) v ^= (-1);
  353. #else
  354. v ^= ((2*state->drift + state->count)>>31);
  355. #endif
  356. ret= fold(v + state->bias, bits);
  357. update_vlc_state(state, v);
  358. //printf("final: %d\n", ret);
  359. return ret;
  360. }
  361. #if CONFIG_FFV1_ENCODER
  362. static inline int encode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
  363. PlaneContext * const p= &s->plane[plane_index];
  364. RangeCoder * const c= &s->c;
  365. int x;
  366. int run_index= s->run_index;
  367. int run_count=0;
  368. int run_mode=0;
  369. if(s->ac){
  370. if(c->bytestream_end - c->bytestream < w*20){
  371. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  372. return -1;
  373. }
  374. }else{
  375. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < w*4){
  376. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  377. return -1;
  378. }
  379. }
  380. for(x=0; x<w; x++){
  381. int diff, context;
  382. context= get_context(s, sample[0]+x, sample[1]+x, sample[2]+x);
  383. diff= sample[0][x] - predict(sample[0]+x, sample[1]+x);
  384. if(context < 0){
  385. context = -context;
  386. diff= -diff;
  387. }
  388. diff= fold(diff, bits);
  389. if(s->ac){
  390. put_symbol_inline(c, p->state[context], diff, 1);
  391. }else{
  392. if(context == 0) run_mode=1;
  393. if(run_mode){
  394. if(diff){
  395. while(run_count >= 1<<ff_log2_run[run_index]){
  396. run_count -= 1<<ff_log2_run[run_index];
  397. run_index++;
  398. put_bits(&s->pb, 1, 1);
  399. }
  400. put_bits(&s->pb, 1 + ff_log2_run[run_index], run_count);
  401. if(run_index) run_index--;
  402. run_count=0;
  403. run_mode=0;
  404. if(diff>0) diff--;
  405. }else{
  406. run_count++;
  407. }
  408. }
  409. // 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));
  410. if(run_mode == 0)
  411. put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
  412. }
  413. }
  414. if(run_mode){
  415. while(run_count >= 1<<ff_log2_run[run_index]){
  416. run_count -= 1<<ff_log2_run[run_index];
  417. run_index++;
  418. put_bits(&s->pb, 1, 1);
  419. }
  420. if(run_count)
  421. put_bits(&s->pb, 1, 1);
  422. }
  423. s->run_index= run_index;
  424. return 0;
  425. }
  426. static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  427. int x,y,i;
  428. const int ring_size= s->avctx->context_model ? 3 : 2;
  429. int_fast16_t sample_buffer[ring_size][w+6], *sample[ring_size];
  430. s->run_index=0;
  431. memset(sample_buffer, 0, sizeof(sample_buffer));
  432. for(y=0; y<h; y++){
  433. for(i=0; i<ring_size; i++)
  434. sample[i]= sample_buffer[(h+i-y)%ring_size]+3;
  435. sample[0][-1]= sample[1][0 ];
  436. sample[1][ w]= sample[1][w-1];
  437. //{START_TIMER
  438. if(s->avctx->bits_per_raw_sample<=8){
  439. for(x=0; x<w; x++){
  440. sample[0][x]= src[x + stride*y];
  441. }
  442. encode_line(s, w, sample, plane_index, 8);
  443. }else{
  444. for(x=0; x<w; x++){
  445. sample[0][x]= ((uint16_t*)(src + stride*y))[x] >> (16 - s->avctx->bits_per_raw_sample);
  446. }
  447. encode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
  448. }
  449. //STOP_TIMER("encode line")}
  450. }
  451. }
  452. static void encode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  453. int x, y, p, i;
  454. const int ring_size= s->avctx->context_model ? 3 : 2;
  455. int_fast16_t sample_buffer[3][ring_size][w+6], *sample[3][ring_size];
  456. s->run_index=0;
  457. memset(sample_buffer, 0, sizeof(sample_buffer));
  458. for(y=0; y<h; y++){
  459. for(i=0; i<ring_size; i++)
  460. for(p=0; p<3; p++)
  461. sample[p][i]= sample_buffer[p][(h+i-y)%ring_size]+3;
  462. for(x=0; x<w; x++){
  463. int v= src[x + stride*y];
  464. int b= v&0xFF;
  465. int g= (v>>8)&0xFF;
  466. int r= (v>>16)&0xFF;
  467. b -= g;
  468. r -= g;
  469. g += (b + r)>>2;
  470. b += 0x100;
  471. r += 0x100;
  472. // assert(g>=0 && b>=0 && r>=0);
  473. // assert(g<256 && b<512 && r<512);
  474. sample[0][0][x]= g;
  475. sample[1][0][x]= b;
  476. sample[2][0][x]= r;
  477. }
  478. for(p=0; p<3; p++){
  479. sample[p][0][-1]= sample[p][1][0 ];
  480. sample[p][1][ w]= sample[p][1][w-1];
  481. encode_line(s, w, sample[p], FFMIN(p, 1), 9);
  482. }
  483. }
  484. }
  485. static void write_quant_table(RangeCoder *c, int16_t *quant_table){
  486. int last=0;
  487. int i;
  488. uint8_t state[CONTEXT_SIZE];
  489. memset(state, 128, sizeof(state));
  490. for(i=1; i<128 ; i++){
  491. if(quant_table[i] != quant_table[i-1]){
  492. put_symbol(c, state, i-last-1, 0);
  493. last= i;
  494. }
  495. }
  496. put_symbol(c, state, i-last-1, 0);
  497. }
  498. static void write_header(FFV1Context *f){
  499. uint8_t state[CONTEXT_SIZE];
  500. int i;
  501. RangeCoder * const c= &f->c;
  502. memset(state, 128, sizeof(state));
  503. put_symbol(c, state, f->version, 0);
  504. put_symbol(c, state, f->avctx->coder_type, 0);
  505. put_symbol(c, state, f->colorspace, 0); //YUV cs type
  506. if(f->version>0)
  507. put_symbol(c, state, f->avctx->bits_per_raw_sample, 0);
  508. put_rac(c, state, 1); //chroma planes
  509. put_symbol(c, state, f->chroma_h_shift, 0);
  510. put_symbol(c, state, f->chroma_v_shift, 0);
  511. put_rac(c, state, 0); //no transparency plane
  512. for(i=0; i<5; i++)
  513. write_quant_table(c, f->quant_table[i]);
  514. }
  515. #endif /* CONFIG_FFV1_ENCODER */
  516. static av_cold int common_init(AVCodecContext *avctx){
  517. FFV1Context *s = avctx->priv_data;
  518. s->avctx= avctx;
  519. s->flags= avctx->flags;
  520. dsputil_init(&s->dsp, avctx);
  521. s->width = avctx->width;
  522. s->height= avctx->height;
  523. assert(s->width && s->height);
  524. return 0;
  525. }
  526. #if CONFIG_FFV1_ENCODER
  527. static av_cold int encode_init(AVCodecContext *avctx)
  528. {
  529. FFV1Context *s = avctx->priv_data;
  530. int i;
  531. common_init(avctx);
  532. s->version=0;
  533. s->ac= avctx->coder_type;
  534. s->plane_count=2;
  535. for(i=0; i<256; i++){
  536. if(avctx->bits_per_raw_sample <=8){
  537. s->quant_table[0][i]= quant11[i];
  538. s->quant_table[1][i]= 11*quant11[i];
  539. if(avctx->context_model==0){
  540. s->quant_table[2][i]= 11*11*quant11[i];
  541. s->quant_table[3][i]=
  542. s->quant_table[4][i]=0;
  543. }else{
  544. s->quant_table[2][i]= 11*11*quant5 [i];
  545. s->quant_table[3][i]= 5*11*11*quant5 [i];
  546. s->quant_table[4][i]= 5*5*11*11*quant5 [i];
  547. }
  548. }else{
  549. s->quant_table[0][i]= quant9_10bit[i];
  550. s->quant_table[1][i]= 11*quant9_10bit[i];
  551. if(avctx->context_model==0){
  552. s->quant_table[2][i]= 11*11*quant9_10bit[i];
  553. s->quant_table[3][i]=
  554. s->quant_table[4][i]=0;
  555. }else{
  556. s->quant_table[2][i]= 11*11*quant5_10bit[i];
  557. s->quant_table[3][i]= 5*11*11*quant5_10bit[i];
  558. s->quant_table[4][i]= 5*5*11*11*quant5_10bit[i];
  559. }
  560. }
  561. }
  562. for(i=0; i<s->plane_count; i++){
  563. PlaneContext * const p= &s->plane[i];
  564. if(avctx->context_model==0){
  565. p->context_count= (11*11*11+1)/2;
  566. }else{
  567. p->context_count= (11*11*5*5*5+1)/2;
  568. }
  569. if(s->ac){
  570. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  571. }else{
  572. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  573. }
  574. }
  575. avctx->coded_frame= &s->picture;
  576. switch(avctx->pix_fmt){
  577. case PIX_FMT_YUV444P16:
  578. case PIX_FMT_YUV422P16:
  579. case PIX_FMT_YUV420P16:
  580. if(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
  581. av_log(avctx, AV_LOG_ERROR, "More than 8 bit per component is still experimental and no gurantee is yet made for future compatibility\n"
  582. "Use vstrict=-2 / -strict -2 to use it anyway.\n");
  583. return -1;
  584. }
  585. if(avctx->bits_per_raw_sample <=8){
  586. av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample inavlid\n");
  587. return -1;
  588. }
  589. s->version= 1;
  590. case PIX_FMT_YUV444P:
  591. case PIX_FMT_YUV422P:
  592. case PIX_FMT_YUV420P:
  593. case PIX_FMT_YUV411P:
  594. case PIX_FMT_YUV410P:
  595. s->colorspace= 0;
  596. break;
  597. case PIX_FMT_RGB32:
  598. s->colorspace= 1;
  599. break;
  600. default:
  601. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  602. return -1;
  603. }
  604. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  605. s->picture_number=0;
  606. return 0;
  607. }
  608. #endif /* CONFIG_FFV1_ENCODER */
  609. static void clear_state(FFV1Context *f){
  610. int i, j;
  611. for(i=0; i<f->plane_count; i++){
  612. PlaneContext *p= &f->plane[i];
  613. p->interlace_bit_state[0]= 128;
  614. p->interlace_bit_state[1]= 128;
  615. for(j=0; j<p->context_count; j++){
  616. if(f->ac){
  617. memset(p->state[j], 128, sizeof(uint8_t)*CONTEXT_SIZE);
  618. }else{
  619. p->vlc_state[j].drift= 0;
  620. p->vlc_state[j].error_sum= 4; //FFMAX((RANGE + 32)/64, 2);
  621. p->vlc_state[j].bias= 0;
  622. p->vlc_state[j].count= 1;
  623. }
  624. }
  625. }
  626. }
  627. #if CONFIG_FFV1_ENCODER
  628. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  629. FFV1Context *f = avctx->priv_data;
  630. RangeCoder * const c= &f->c;
  631. AVFrame *pict = data;
  632. const int width= f->width;
  633. const int height= f->height;
  634. AVFrame * const p= &f->picture;
  635. int used_count= 0;
  636. uint8_t keystate=128;
  637. ff_init_range_encoder(c, buf, buf_size);
  638. // ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  639. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  640. *p = *pict;
  641. p->pict_type= FF_I_TYPE;
  642. if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){
  643. put_rac(c, &keystate, 1);
  644. p->key_frame= 1;
  645. write_header(f);
  646. clear_state(f);
  647. }else{
  648. put_rac(c, &keystate, 0);
  649. p->key_frame= 0;
  650. }
  651. if(!f->ac){
  652. used_count += ff_rac_terminate(c);
  653. //printf("pos=%d\n", used_count);
  654. init_put_bits(&f->pb, buf + used_count, buf_size - used_count);
  655. }
  656. if(f->colorspace==0){
  657. const int chroma_width = -((-width )>>f->chroma_h_shift);
  658. const int chroma_height= -((-height)>>f->chroma_v_shift);
  659. encode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  660. encode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  661. encode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
  662. }else{
  663. encode_rgb_frame(f, (uint32_t*)(p->data[0]), width, height, p->linesize[0]/4);
  664. }
  665. emms_c();
  666. f->picture_number++;
  667. if(f->ac){
  668. return ff_rac_terminate(c);
  669. }else{
  670. flush_put_bits(&f->pb); //nicer padding FIXME
  671. return used_count + (put_bits_count(&f->pb)+7)/8;
  672. }
  673. }
  674. #endif /* CONFIG_FFV1_ENCODER */
  675. static av_cold int common_end(AVCodecContext *avctx){
  676. FFV1Context *s = avctx->priv_data;
  677. int i;
  678. for(i=0; i<s->plane_count; i++){
  679. PlaneContext *p= &s->plane[i];
  680. av_freep(&p->state);
  681. av_freep(&p->vlc_state);
  682. }
  683. return 0;
  684. }
  685. static av_always_inline void decode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
  686. PlaneContext * const p= &s->plane[plane_index];
  687. RangeCoder * const c= &s->c;
  688. int x;
  689. int run_count=0;
  690. int run_mode=0;
  691. int run_index= s->run_index;
  692. for(x=0; x<w; x++){
  693. int diff, context, sign;
  694. context= get_context(s, sample[1] + x, sample[0] + x, sample[1] + x);
  695. if(context < 0){
  696. context= -context;
  697. sign=1;
  698. }else
  699. sign=0;
  700. if(s->ac){
  701. diff= get_symbol_inline(c, p->state[context], 1);
  702. }else{
  703. if(context == 0 && run_mode==0) run_mode=1;
  704. if(run_mode){
  705. if(run_count==0 && run_mode==1){
  706. if(get_bits1(&s->gb)){
  707. run_count = 1<<ff_log2_run[run_index];
  708. if(x + run_count <= w) run_index++;
  709. }else{
  710. if(ff_log2_run[run_index]) run_count = get_bits(&s->gb, ff_log2_run[run_index]);
  711. else run_count=0;
  712. if(run_index) run_index--;
  713. run_mode=2;
  714. }
  715. }
  716. run_count--;
  717. if(run_count < 0){
  718. run_mode=0;
  719. run_count=0;
  720. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  721. if(diff>=0) diff++;
  722. }else
  723. diff=0;
  724. }else
  725. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  726. // 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));
  727. }
  728. if(sign) diff= -diff;
  729. sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
  730. }
  731. s->run_index= run_index;
  732. }
  733. static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  734. int x, y;
  735. int_fast16_t sample_buffer[2][w+6];
  736. int_fast16_t *sample[2];
  737. sample[0]=sample_buffer[0]+3;
  738. sample[1]=sample_buffer[1]+3;
  739. s->run_index=0;
  740. memset(sample_buffer, 0, sizeof(sample_buffer));
  741. for(y=0; y<h; y++){
  742. int_fast16_t *temp= sample[0]; //FIXME try a normal buffer
  743. sample[0]= sample[1];
  744. sample[1]= temp;
  745. sample[1][-1]= sample[0][0 ];
  746. sample[0][ w]= sample[0][w-1];
  747. //{START_TIMER
  748. if(s->avctx->bits_per_raw_sample <= 8){
  749. decode_line(s, w, sample, plane_index, 8);
  750. for(x=0; x<w; x++){
  751. src[x + stride*y]= sample[1][x];
  752. }
  753. }else{
  754. decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
  755. for(x=0; x<w; x++){
  756. ((uint16_t*)(src + stride*y))[x]= sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
  757. }
  758. }
  759. //STOP_TIMER("decode-line")}
  760. }
  761. }
  762. static void decode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  763. int x, y, p;
  764. int_fast16_t sample_buffer[3][2][w+6];
  765. int_fast16_t *sample[3][2];
  766. for(x=0; x<3; x++){
  767. sample[x][0] = sample_buffer[x][0]+3;
  768. sample[x][1] = sample_buffer[x][1]+3;
  769. }
  770. s->run_index=0;
  771. memset(sample_buffer, 0, sizeof(sample_buffer));
  772. for(y=0; y<h; y++){
  773. for(p=0; p<3; p++){
  774. int_fast16_t *temp= sample[p][0]; //FIXME try a normal buffer
  775. sample[p][0]= sample[p][1];
  776. sample[p][1]= temp;
  777. sample[p][1][-1]= sample[p][0][0 ];
  778. sample[p][0][ w]= sample[p][0][w-1];
  779. decode_line(s, w, sample[p], FFMIN(p, 1), 9);
  780. }
  781. for(x=0; x<w; x++){
  782. int g= sample[0][1][x];
  783. int b= sample[1][1][x];
  784. int r= sample[2][1][x];
  785. // assert(g>=0 && b>=0 && r>=0);
  786. // assert(g<256 && b<512 && r<512);
  787. b -= 0x100;
  788. r -= 0x100;
  789. g -= (b + r)>>2;
  790. b += g;
  791. r += g;
  792. src[x + stride*y]= b + (g<<8) + (r<<16);
  793. }
  794. }
  795. }
  796. static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){
  797. int v;
  798. int i=0;
  799. uint8_t state[CONTEXT_SIZE];
  800. memset(state, 128, sizeof(state));
  801. for(v=0; i<128 ; v++){
  802. int len= get_symbol(c, state, 0) + 1;
  803. if(len + i > 128) return -1;
  804. while(len--){
  805. quant_table[i] = scale*v;
  806. i++;
  807. //printf("%2d ",v);
  808. //if(i%16==0) printf("\n");
  809. }
  810. }
  811. for(i=1; i<128; i++){
  812. quant_table[256-i]= -quant_table[i];
  813. }
  814. quant_table[128]= -quant_table[127];
  815. return 2*v - 1;
  816. }
  817. static int read_header(FFV1Context *f){
  818. uint8_t state[CONTEXT_SIZE];
  819. int i, context_count;
  820. RangeCoder * const c= &f->c;
  821. memset(state, 128, sizeof(state));
  822. f->version= get_symbol(c, state, 0);
  823. f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
  824. f->colorspace= get_symbol(c, state, 0); //YUV cs type
  825. if(f->version>0)
  826. f->avctx->bits_per_raw_sample= get_symbol(c, state, 0);
  827. get_rac(c, state); //no chroma = false
  828. f->chroma_h_shift= get_symbol(c, state, 0);
  829. f->chroma_v_shift= get_symbol(c, state, 0);
  830. get_rac(c, state); //transparency plane
  831. f->plane_count= 2;
  832. if(f->colorspace==0){
  833. if(f->avctx->bits_per_raw_sample<=8){
  834. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  835. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
  836. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
  837. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
  838. case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
  839. case 0x22: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
  840. default:
  841. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  842. return -1;
  843. }
  844. }else{
  845. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  846. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P16; break;
  847. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P16; break;
  848. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P16; break;
  849. default:
  850. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  851. return -1;
  852. }
  853. }
  854. }else if(f->colorspace==1){
  855. if(f->chroma_h_shift || f->chroma_v_shift){
  856. av_log(f->avctx, AV_LOG_ERROR, "chroma subsampling not supported in this colorspace\n");
  857. return -1;
  858. }
  859. f->avctx->pix_fmt= PIX_FMT_RGB32;
  860. }else{
  861. av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
  862. return -1;
  863. }
  864. //printf("%d %d %d\n", f->chroma_h_shift, f->chroma_v_shift,f->avctx->pix_fmt);
  865. context_count=1;
  866. for(i=0; i<5; i++){
  867. context_count*= read_quant_table(c, f->quant_table[i], context_count);
  868. if(context_count < 0 || context_count > 32768){
  869. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  870. return -1;
  871. }
  872. }
  873. context_count= (context_count+1)/2;
  874. for(i=0; i<f->plane_count; i++){
  875. PlaneContext * const p= &f->plane[i];
  876. p->context_count= context_count;
  877. if(f->ac){
  878. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  879. }else{
  880. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  881. }
  882. }
  883. return 0;
  884. }
  885. static av_cold int decode_init(AVCodecContext *avctx)
  886. {
  887. // FFV1Context *s = avctx->priv_data;
  888. common_init(avctx);
  889. return 0;
  890. }
  891. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt){
  892. const uint8_t *buf = avpkt->data;
  893. int buf_size = avpkt->size;
  894. FFV1Context *f = avctx->priv_data;
  895. RangeCoder * const c= &f->c;
  896. const int width= f->width;
  897. const int height= f->height;
  898. AVFrame * const p= &f->picture;
  899. int bytes_read;
  900. uint8_t keystate= 128;
  901. AVFrame *picture = data;
  902. ff_init_range_decoder(c, buf, buf_size);
  903. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  904. p->pict_type= FF_I_TYPE; //FIXME I vs. P
  905. if(get_rac(c, &keystate)){
  906. p->key_frame= 1;
  907. if(read_header(f) < 0)
  908. return -1;
  909. clear_state(f);
  910. }else{
  911. p->key_frame= 0;
  912. }
  913. if(!f->plane[0].state && !f->plane[0].vlc_state)
  914. return -1;
  915. p->reference= 0;
  916. if(avctx->get_buffer(avctx, p) < 0){
  917. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  918. return -1;
  919. }
  920. if(avctx->debug&FF_DEBUG_PICT_INFO)
  921. av_log(avctx, AV_LOG_ERROR, "keyframe:%d coder:%d\n", p->key_frame, f->ac);
  922. if(!f->ac){
  923. bytes_read = c->bytestream - c->bytestream_start - 1;
  924. if(bytes_read ==0) av_log(avctx, AV_LOG_ERROR, "error at end of AC stream\n"); //FIXME
  925. //printf("pos=%d\n", bytes_read);
  926. init_get_bits(&f->gb, buf + bytes_read, buf_size - bytes_read);
  927. } else {
  928. bytes_read = 0; /* avoid warning */
  929. }
  930. if(f->colorspace==0){
  931. const int chroma_width = -((-width )>>f->chroma_h_shift);
  932. const int chroma_height= -((-height)>>f->chroma_v_shift);
  933. decode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  934. decode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  935. decode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
  936. }else{
  937. decode_rgb_frame(f, (uint32_t*)p->data[0], width, height, p->linesize[0]/4);
  938. }
  939. emms_c();
  940. f->picture_number++;
  941. *picture= *p;
  942. avctx->release_buffer(avctx, p); //FIXME
  943. *data_size = sizeof(AVFrame);
  944. if(f->ac){
  945. bytes_read= c->bytestream - c->bytestream_start - 1;
  946. if(bytes_read ==0) av_log(f->avctx, AV_LOG_ERROR, "error at end of frame\n");
  947. }else{
  948. bytes_read+= (get_bits_count(&f->gb)+7)/8;
  949. }
  950. return bytes_read;
  951. }
  952. AVCodec ffv1_decoder = {
  953. "ffv1",
  954. CODEC_TYPE_VIDEO,
  955. CODEC_ID_FFV1,
  956. sizeof(FFV1Context),
  957. decode_init,
  958. NULL,
  959. common_end,
  960. decode_frame,
  961. CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  962. NULL,
  963. .long_name= NULL_IF_CONFIG_SMALL("FFmpeg codec #1"),
  964. };
  965. #if CONFIG_FFV1_ENCODER
  966. AVCodec ffv1_encoder = {
  967. "ffv1",
  968. CODEC_TYPE_VIDEO,
  969. CODEC_ID_FFV1,
  970. sizeof(FFV1Context),
  971. encode_init,
  972. encode_frame,
  973. common_end,
  974. .pix_fmts= (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_NONE},
  975. .long_name= NULL_IF_CONFIG_SMALL("FFmpeg codec #1"),
  976. };
  977. #endif