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.

1153 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 (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. #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, 1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};
  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, 3, 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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=range coder <-> 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->bits_per_raw_sample <=8){
  581. av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
  582. return -1;
  583. }
  584. if(!s->ac){
  585. av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample of more than 8 needs -coder 1 currently\n");
  586. return -1;
  587. }
  588. s->version= 1;
  589. case PIX_FMT_YUV444P:
  590. case PIX_FMT_YUV422P:
  591. case PIX_FMT_YUV420P:
  592. case PIX_FMT_YUV411P:
  593. case PIX_FMT_YUV410P:
  594. s->colorspace= 0;
  595. break;
  596. case PIX_FMT_RGB32:
  597. s->colorspace= 1;
  598. break;
  599. default:
  600. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  601. return -1;
  602. }
  603. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  604. s->picture_number=0;
  605. return 0;
  606. }
  607. #endif /* CONFIG_FFV1_ENCODER */
  608. static void clear_state(FFV1Context *f){
  609. int i, j;
  610. for(i=0; i<f->plane_count; i++){
  611. PlaneContext *p= &f->plane[i];
  612. p->interlace_bit_state[0]= 128;
  613. p->interlace_bit_state[1]= 128;
  614. for(j=0; j<p->context_count; j++){
  615. if(f->ac){
  616. memset(p->state[j], 128, sizeof(uint8_t)*CONTEXT_SIZE);
  617. }else{
  618. p->vlc_state[j].drift= 0;
  619. p->vlc_state[j].error_sum= 4; //FFMAX((RANGE + 32)/64, 2);
  620. p->vlc_state[j].bias= 0;
  621. p->vlc_state[j].count= 1;
  622. }
  623. }
  624. }
  625. }
  626. #if CONFIG_FFV1_ENCODER
  627. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  628. FFV1Context *f = avctx->priv_data;
  629. RangeCoder * const c= &f->c;
  630. AVFrame *pict = data;
  631. const int width= f->width;
  632. const int height= f->height;
  633. AVFrame * const p= &f->picture;
  634. int used_count= 0;
  635. uint8_t keystate=128;
  636. ff_init_range_encoder(c, buf, buf_size);
  637. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  638. *p = *pict;
  639. p->pict_type= FF_I_TYPE;
  640. if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){
  641. put_rac(c, &keystate, 1);
  642. p->key_frame= 1;
  643. write_header(f);
  644. clear_state(f);
  645. }else{
  646. put_rac(c, &keystate, 0);
  647. p->key_frame= 0;
  648. }
  649. if(!f->ac){
  650. used_count += ff_rac_terminate(c);
  651. //printf("pos=%d\n", used_count);
  652. init_put_bits(&f->pb, buf + used_count, buf_size - used_count);
  653. }
  654. if(f->colorspace==0){
  655. const int chroma_width = -((-width )>>f->chroma_h_shift);
  656. const int chroma_height= -((-height)>>f->chroma_v_shift);
  657. encode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  658. encode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  659. encode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
  660. }else{
  661. encode_rgb_frame(f, (uint32_t*)(p->data[0]), width, height, p->linesize[0]/4);
  662. }
  663. emms_c();
  664. f->picture_number++;
  665. if(f->ac){
  666. return ff_rac_terminate(c);
  667. }else{
  668. flush_put_bits(&f->pb); //nicer padding FIXME
  669. return used_count + (put_bits_count(&f->pb)+7)/8;
  670. }
  671. }
  672. #endif /* CONFIG_FFV1_ENCODER */
  673. static av_cold int common_end(AVCodecContext *avctx){
  674. FFV1Context *s = avctx->priv_data;
  675. int i;
  676. for(i=0; i<s->plane_count; i++){
  677. PlaneContext *p= &s->plane[i];
  678. av_freep(&p->state);
  679. av_freep(&p->vlc_state);
  680. }
  681. return 0;
  682. }
  683. static av_always_inline void decode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
  684. PlaneContext * const p= &s->plane[plane_index];
  685. RangeCoder * const c= &s->c;
  686. int x;
  687. int run_count=0;
  688. int run_mode=0;
  689. int run_index= s->run_index;
  690. for(x=0; x<w; x++){
  691. int diff, context, sign;
  692. context= get_context(s, sample[1] + x, sample[0] + x, sample[1] + x);
  693. if(context < 0){
  694. context= -context;
  695. sign=1;
  696. }else
  697. sign=0;
  698. if(s->ac){
  699. diff= get_symbol_inline(c, p->state[context], 1);
  700. }else{
  701. if(context == 0 && run_mode==0) run_mode=1;
  702. if(run_mode){
  703. if(run_count==0 && run_mode==1){
  704. if(get_bits1(&s->gb)){
  705. run_count = 1<<ff_log2_run[run_index];
  706. if(x + run_count <= w) run_index++;
  707. }else{
  708. if(ff_log2_run[run_index]) run_count = get_bits(&s->gb, ff_log2_run[run_index]);
  709. else run_count=0;
  710. if(run_index) run_index--;
  711. run_mode=2;
  712. }
  713. }
  714. run_count--;
  715. if(run_count < 0){
  716. run_mode=0;
  717. run_count=0;
  718. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  719. if(diff>=0) diff++;
  720. }else
  721. diff=0;
  722. }else
  723. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  724. // 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));
  725. }
  726. if(sign) diff= -diff;
  727. sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
  728. }
  729. s->run_index= run_index;
  730. }
  731. static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  732. int x, y;
  733. int_fast16_t sample_buffer[2][w+6];
  734. int_fast16_t *sample[2];
  735. sample[0]=sample_buffer[0]+3;
  736. sample[1]=sample_buffer[1]+3;
  737. s->run_index=0;
  738. memset(sample_buffer, 0, sizeof(sample_buffer));
  739. for(y=0; y<h; y++){
  740. int_fast16_t *temp= sample[0]; //FIXME try a normal buffer
  741. sample[0]= sample[1];
  742. sample[1]= temp;
  743. sample[1][-1]= sample[0][0 ];
  744. sample[0][ w]= sample[0][w-1];
  745. //{START_TIMER
  746. if(s->avctx->bits_per_raw_sample <= 8){
  747. decode_line(s, w, sample, plane_index, 8);
  748. for(x=0; x<w; x++){
  749. src[x + stride*y]= sample[1][x];
  750. }
  751. }else{
  752. decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
  753. for(x=0; x<w; x++){
  754. ((uint16_t*)(src + stride*y))[x]= sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
  755. }
  756. }
  757. //STOP_TIMER("decode-line")}
  758. }
  759. }
  760. static void decode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  761. int x, y, p;
  762. int_fast16_t sample_buffer[3][2][w+6];
  763. int_fast16_t *sample[3][2];
  764. for(x=0; x<3; x++){
  765. sample[x][0] = sample_buffer[x][0]+3;
  766. sample[x][1] = sample_buffer[x][1]+3;
  767. }
  768. s->run_index=0;
  769. memset(sample_buffer, 0, sizeof(sample_buffer));
  770. for(y=0; y<h; y++){
  771. for(p=0; p<3; p++){
  772. int_fast16_t *temp= sample[p][0]; //FIXME try a normal buffer
  773. sample[p][0]= sample[p][1];
  774. sample[p][1]= temp;
  775. sample[p][1][-1]= sample[p][0][0 ];
  776. sample[p][0][ w]= sample[p][0][w-1];
  777. decode_line(s, w, sample[p], FFMIN(p, 1), 9);
  778. }
  779. for(x=0; x<w; x++){
  780. int g= sample[0][1][x];
  781. int b= sample[1][1][x];
  782. int r= sample[2][1][x];
  783. // assert(g>=0 && b>=0 && r>=0);
  784. // assert(g<256 && b<512 && r<512);
  785. b -= 0x100;
  786. r -= 0x100;
  787. g -= (b + r)>>2;
  788. b += g;
  789. r += g;
  790. src[x + stride*y]= b + (g<<8) + (r<<16);
  791. }
  792. }
  793. }
  794. static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){
  795. int v;
  796. int i=0;
  797. uint8_t state[CONTEXT_SIZE];
  798. memset(state, 128, sizeof(state));
  799. for(v=0; i<128 ; v++){
  800. int len= get_symbol(c, state, 0) + 1;
  801. if(len + i > 128) return -1;
  802. while(len--){
  803. quant_table[i] = scale*v;
  804. i++;
  805. //printf("%2d ",v);
  806. //if(i%16==0) printf("\n");
  807. }
  808. }
  809. for(i=1; i<128; i++){
  810. quant_table[256-i]= -quant_table[i];
  811. }
  812. quant_table[128]= -quant_table[127];
  813. return 2*v - 1;
  814. }
  815. static int read_header(FFV1Context *f){
  816. uint8_t state[CONTEXT_SIZE];
  817. int i, context_count;
  818. RangeCoder * const c= &f->c;
  819. memset(state, 128, sizeof(state));
  820. f->version= get_symbol(c, state, 0);
  821. f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
  822. f->colorspace= get_symbol(c, state, 0); //YUV cs type
  823. if(f->version>0)
  824. f->avctx->bits_per_raw_sample= get_symbol(c, state, 0);
  825. get_rac(c, state); //no chroma = false
  826. f->chroma_h_shift= get_symbol(c, state, 0);
  827. f->chroma_v_shift= get_symbol(c, state, 0);
  828. get_rac(c, state); //transparency plane
  829. f->plane_count= 2;
  830. if(f->colorspace==0){
  831. if(f->avctx->bits_per_raw_sample<=8){
  832. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  833. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
  834. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
  835. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
  836. case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
  837. case 0x22: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
  838. default:
  839. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  840. return -1;
  841. }
  842. }else{
  843. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  844. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P16; break;
  845. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P16; break;
  846. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P16; break;
  847. default:
  848. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  849. return -1;
  850. }
  851. }
  852. }else if(f->colorspace==1){
  853. if(f->chroma_h_shift || f->chroma_v_shift){
  854. av_log(f->avctx, AV_LOG_ERROR, "chroma subsampling not supported in this colorspace\n");
  855. return -1;
  856. }
  857. f->avctx->pix_fmt= PIX_FMT_RGB32;
  858. }else{
  859. av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
  860. return -1;
  861. }
  862. //printf("%d %d %d\n", f->chroma_h_shift, f->chroma_v_shift,f->avctx->pix_fmt);
  863. context_count=1;
  864. for(i=0; i<5; i++){
  865. context_count*= read_quant_table(c, f->quant_table[i], context_count);
  866. if(context_count < 0 || context_count > 32768){
  867. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  868. return -1;
  869. }
  870. }
  871. context_count= (context_count+1)/2;
  872. for(i=0; i<f->plane_count; i++){
  873. PlaneContext * const p= &f->plane[i];
  874. p->context_count= context_count;
  875. if(f->ac){
  876. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  877. }else{
  878. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  879. }
  880. }
  881. return 0;
  882. }
  883. static av_cold int decode_init(AVCodecContext *avctx)
  884. {
  885. // FFV1Context *s = avctx->priv_data;
  886. common_init(avctx);
  887. return 0;
  888. }
  889. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt){
  890. const uint8_t *buf = avpkt->data;
  891. int buf_size = avpkt->size;
  892. FFV1Context *f = avctx->priv_data;
  893. RangeCoder * const c= &f->c;
  894. const int width= f->width;
  895. const int height= f->height;
  896. AVFrame * const p= &f->picture;
  897. int bytes_read;
  898. uint8_t keystate= 128;
  899. AVFrame *picture = data;
  900. ff_init_range_decoder(c, buf, buf_size);
  901. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  902. p->pict_type= FF_I_TYPE; //FIXME I vs. P
  903. if(get_rac(c, &keystate)){
  904. p->key_frame= 1;
  905. if(read_header(f) < 0)
  906. return -1;
  907. clear_state(f);
  908. }else{
  909. p->key_frame= 0;
  910. }
  911. if(!f->plane[0].state && !f->plane[0].vlc_state)
  912. return -1;
  913. p->reference= 0;
  914. if(avctx->get_buffer(avctx, p) < 0){
  915. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  916. return -1;
  917. }
  918. if(avctx->debug&FF_DEBUG_PICT_INFO)
  919. av_log(avctx, AV_LOG_ERROR, "keyframe:%d coder:%d\n", p->key_frame, f->ac);
  920. if(!f->ac){
  921. bytes_read = c->bytestream - c->bytestream_start - 1;
  922. if(bytes_read ==0) av_log(avctx, AV_LOG_ERROR, "error at end of AC stream\n"); //FIXME
  923. //printf("pos=%d\n", bytes_read);
  924. init_get_bits(&f->gb, buf + bytes_read, buf_size - bytes_read);
  925. } else {
  926. bytes_read = 0; /* avoid warning */
  927. }
  928. if(f->colorspace==0){
  929. const int chroma_width = -((-width )>>f->chroma_h_shift);
  930. const int chroma_height= -((-height)>>f->chroma_v_shift);
  931. decode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  932. decode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  933. decode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
  934. }else{
  935. decode_rgb_frame(f, (uint32_t*)p->data[0], width, height, p->linesize[0]/4);
  936. }
  937. emms_c();
  938. f->picture_number++;
  939. *picture= *p;
  940. avctx->release_buffer(avctx, p); //FIXME
  941. *data_size = sizeof(AVFrame);
  942. if(f->ac){
  943. bytes_read= c->bytestream - c->bytestream_start - 1;
  944. if(bytes_read ==0) av_log(f->avctx, AV_LOG_ERROR, "error at end of frame\n");
  945. }else{
  946. bytes_read+= (get_bits_count(&f->gb)+7)/8;
  947. }
  948. return bytes_read;
  949. }
  950. AVCodec ffv1_decoder = {
  951. "ffv1",
  952. CODEC_TYPE_VIDEO,
  953. CODEC_ID_FFV1,
  954. sizeof(FFV1Context),
  955. decode_init,
  956. NULL,
  957. common_end,
  958. decode_frame,
  959. CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  960. NULL,
  961. .long_name= NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  962. };
  963. #if CONFIG_FFV1_ENCODER
  964. AVCodec ffv1_encoder = {
  965. "ffv1",
  966. CODEC_TYPE_VIDEO,
  967. CODEC_ID_FFV1,
  968. sizeof(FFV1Context),
  969. encode_init,
  970. encode_frame,
  971. common_end,
  972. .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_NONE},
  973. .long_name= NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  974. };
  975. #endif