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.

1312 lines
40KB

  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. #define MAX_PLANES 4
  34. #define CONTEXT_SIZE 32
  35. #define MAX_QUANT_TABLES 8
  36. extern const uint8_t ff_log2_run[32];
  37. static const int8_t quant3[256]={
  38. 0, 0, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  44. 1, 1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1,-1,
  108. };
  109. static const int8_t quant9[256]={
  110. 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  111. 3, 3, 3, 3, 3, 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,-4,-4,-4,-4,
  124. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,
  125. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-1,-1,
  126. };
  127. static const int8_t quant9_10bit[256]={
  128. 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
  129. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3,
  130. 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,-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,-6,
  176. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-5,
  177. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  178. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  179. -4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,-2,-2,-1,
  180. };
  181. static const uint8_t ver2_state[256]= {
  182. 0, 10, 10, 10, 10, 16, 16, 16, 28, 16, 16, 29, 42, 49, 20, 49,
  183. 59, 25, 26, 26, 27, 31, 33, 33, 33, 34, 34, 37, 67, 38, 39, 39,
  184. 40, 40, 41, 79, 43, 44, 45, 45, 48, 48, 64, 50, 51, 52, 88, 52,
  185. 53, 74, 55, 57, 58, 58, 74, 60, 101, 61, 62, 84, 66, 66, 68, 69,
  186. 87, 82, 71, 97, 73, 73, 82, 75, 111, 77, 94, 78, 87, 81, 83, 97,
  187. 85, 83, 94, 86, 99, 89, 90, 99, 111, 92, 93, 134, 95, 98, 105, 98,
  188. 105, 110, 102, 108, 102, 118, 103, 106, 106, 113, 109, 112, 114, 112, 116, 125,
  189. 115, 116, 117, 117, 126, 119, 125, 121, 121, 123, 145, 124, 126, 131, 127, 129,
  190. 165, 130, 132, 138, 133, 135, 145, 136, 137, 139, 146, 141, 143, 142, 144, 148,
  191. 147, 155, 151, 149, 151, 150, 152, 157, 153, 154, 156, 168, 158, 162, 161, 160,
  192. 172, 163, 169, 164, 166, 184, 167, 170, 177, 174, 171, 173, 182, 176, 180, 178,
  193. 175, 189, 179, 181, 186, 183, 192, 185, 200, 187, 191, 188, 190, 197, 193, 196,
  194. 197, 194, 195, 196, 198, 202, 199, 201, 210, 203, 207, 204, 205, 206, 208, 214,
  195. 209, 211, 221, 212, 213, 215, 224, 216, 217, 218, 219, 220, 222, 228, 223, 225,
  196. 226, 224, 227, 229, 240, 230, 231, 232, 233, 234, 235, 236, 238, 239, 237, 242,
  197. 241, 243, 242, 244, 245, 246, 247, 248, 249, 250, 251, 252, 252, 253, 254, 255,
  198. };
  199. typedef struct VlcState{
  200. int16_t drift;
  201. uint16_t error_sum;
  202. int8_t bias;
  203. uint8_t count;
  204. } VlcState;
  205. typedef struct PlaneContext{
  206. int context_count;
  207. uint8_t (*state)[CONTEXT_SIZE];
  208. VlcState *vlc_state;
  209. uint8_t interlace_bit_state[2];
  210. } PlaneContext;
  211. typedef struct FFV1Context{
  212. AVCodecContext *avctx;
  213. RangeCoder c;
  214. GetBitContext gb;
  215. PutBitContext pb;
  216. int version;
  217. int width, height;
  218. int chroma_h_shift, chroma_v_shift;
  219. int flags;
  220. int picture_number;
  221. AVFrame picture;
  222. int plane_count;
  223. int ac; ///< 1=range coder <-> 0=golomb rice
  224. PlaneContext plane[MAX_PLANES];
  225. int16_t quant_table[5][256];
  226. int16_t quant_tables[MAX_QUANT_TABLES][5][256];
  227. int context_count[MAX_QUANT_TABLES];
  228. uint8_t state_transition[256];
  229. int run_index;
  230. int colorspace;
  231. int_fast16_t *sample_buffer;
  232. int quant_table_count;
  233. DSPContext dsp;
  234. }FFV1Context;
  235. static av_always_inline int fold(int diff, int bits){
  236. if(bits==8)
  237. diff= (int8_t)diff;
  238. else{
  239. diff+= 1<<(bits-1);
  240. diff&=(1<<bits)-1;
  241. diff-= 1<<(bits-1);
  242. }
  243. return diff;
  244. }
  245. static inline int predict(int_fast16_t *src, int_fast16_t *last){
  246. const int LT= last[-1];
  247. const int T= last[ 0];
  248. const int L = src[-1];
  249. return mid_pred(L, L + T - LT, T);
  250. }
  251. static inline int get_context(FFV1Context *f, int_fast16_t *src, int_fast16_t *last, int_fast16_t *last2){
  252. const int LT= last[-1];
  253. const int T= last[ 0];
  254. const int RT= last[ 1];
  255. const int L = src[-1];
  256. if(f->quant_table[3][127]){
  257. const int TT= last2[0];
  258. const int LL= src[-2];
  259. return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF]
  260. +f->quant_table[3][(LL-L) & 0xFF] + f->quant_table[4][(TT-T) & 0xFF];
  261. }else
  262. return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF];
  263. }
  264. static inline void put_symbol_inline(RangeCoder *c, uint8_t *state, int v, int is_signed){
  265. int i;
  266. if(v){
  267. const int a= FFABS(v);
  268. const int e= av_log2(a);
  269. put_rac(c, state+0, 0);
  270. if(e<=9){
  271. for(i=0; i<e; i++){
  272. put_rac(c, state+1+i, 1); //1..10
  273. }
  274. put_rac(c, state+1+i, 0);
  275. for(i=e-1; i>=0; i--){
  276. put_rac(c, state+22+i, (a>>i)&1); //22..31
  277. }
  278. if(is_signed)
  279. put_rac(c, state+11 + e, v < 0); //11..21
  280. }else{
  281. for(i=0; i<e; i++){
  282. put_rac(c, state+1+FFMIN(i,9), 1); //1..10
  283. }
  284. put_rac(c, state+1+9, 0);
  285. for(i=e-1; i>=0; i--){
  286. put_rac(c, state+22+FFMIN(i,9), (a>>i)&1); //22..31
  287. }
  288. if(is_signed)
  289. put_rac(c, state+11 + 10, v < 0); //11..21
  290. }
  291. }else{
  292. put_rac(c, state+0, 1);
  293. }
  294. }
  295. static void av_noinline put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
  296. put_symbol_inline(c, state, v, is_signed);
  297. }
  298. static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state, int is_signed){
  299. if(get_rac(c, state+0))
  300. return 0;
  301. else{
  302. int i, e, a;
  303. e= 0;
  304. while(get_rac(c, state+1 + FFMIN(e,9))){ //1..10
  305. e++;
  306. }
  307. a= 1;
  308. for(i=e-1; i>=0; i--){
  309. a += a + get_rac(c, state+22 + FFMIN(i,9)); //22..31
  310. }
  311. e= -(is_signed && get_rac(c, state+11 + FFMIN(e, 10))); //11..21
  312. return (a^e)-e;
  313. }
  314. }
  315. static int av_noinline get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
  316. return get_symbol_inline(c, state, is_signed);
  317. }
  318. static inline void update_vlc_state(VlcState * const state, const int v){
  319. int drift= state->drift;
  320. int count= state->count;
  321. state->error_sum += FFABS(v);
  322. drift += v;
  323. if(count == 128){ //FIXME variable
  324. count >>= 1;
  325. drift >>= 1;
  326. state->error_sum >>= 1;
  327. }
  328. count++;
  329. if(drift <= -count){
  330. if(state->bias > -128) state->bias--;
  331. drift += count;
  332. if(drift <= -count)
  333. drift= -count + 1;
  334. }else if(drift > 0){
  335. if(state->bias < 127) state->bias++;
  336. drift -= count;
  337. if(drift > 0)
  338. drift= 0;
  339. }
  340. state->drift= drift;
  341. state->count= count;
  342. }
  343. static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int v, int bits){
  344. int i, k, code;
  345. //printf("final: %d ", v);
  346. v = fold(v - state->bias, bits);
  347. i= state->count;
  348. k=0;
  349. while(i < state->error_sum){ //FIXME optimize
  350. k++;
  351. i += i;
  352. }
  353. assert(k<=8);
  354. #if 0 // JPEG LS
  355. if(k==0 && 2*state->drift <= - state->count) code= v ^ (-1);
  356. else code= v;
  357. #else
  358. code= v ^ ((2*state->drift + state->count)>>31);
  359. #endif
  360. //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);
  361. set_sr_golomb(pb, code, k, 12, bits);
  362. update_vlc_state(state, v);
  363. }
  364. static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state, int bits){
  365. int k, i, v, ret;
  366. i= state->count;
  367. k=0;
  368. while(i < state->error_sum){ //FIXME optimize
  369. k++;
  370. i += i;
  371. }
  372. assert(k<=8);
  373. v= get_sr_golomb(gb, k, 12, bits);
  374. //printf("v:%d bias:%d error:%d drift:%d count:%d k:%d", v, state->bias, state->error_sum, state->drift, state->count, k);
  375. #if 0 // JPEG LS
  376. if(k==0 && 2*state->drift <= - state->count) v ^= (-1);
  377. #else
  378. v ^= ((2*state->drift + state->count)>>31);
  379. #endif
  380. ret= fold(v + state->bias, bits);
  381. update_vlc_state(state, v);
  382. //printf("final: %d\n", ret);
  383. return ret;
  384. }
  385. #if CONFIG_FFV1_ENCODER
  386. static inline int encode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
  387. PlaneContext * const p= &s->plane[plane_index];
  388. RangeCoder * const c= &s->c;
  389. int x;
  390. int run_index= s->run_index;
  391. int run_count=0;
  392. int run_mode=0;
  393. if(s->ac){
  394. if(c->bytestream_end - c->bytestream < w*20){
  395. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  396. return -1;
  397. }
  398. }else{
  399. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < w*4){
  400. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  401. return -1;
  402. }
  403. }
  404. for(x=0; x<w; x++){
  405. int diff, context;
  406. context= get_context(s, sample[0]+x, sample[1]+x, sample[2]+x);
  407. diff= sample[0][x] - predict(sample[0]+x, sample[1]+x);
  408. if(context < 0){
  409. context = -context;
  410. diff= -diff;
  411. }
  412. diff= fold(diff, bits);
  413. if(s->ac){
  414. put_symbol_inline(c, p->state[context], diff, 1);
  415. }else{
  416. if(context == 0) run_mode=1;
  417. if(run_mode){
  418. if(diff){
  419. while(run_count >= 1<<ff_log2_run[run_index]){
  420. run_count -= 1<<ff_log2_run[run_index];
  421. run_index++;
  422. put_bits(&s->pb, 1, 1);
  423. }
  424. put_bits(&s->pb, 1 + ff_log2_run[run_index], run_count);
  425. if(run_index) run_index--;
  426. run_count=0;
  427. run_mode=0;
  428. if(diff>0) diff--;
  429. }else{
  430. run_count++;
  431. }
  432. }
  433. // 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));
  434. if(run_mode == 0)
  435. put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
  436. }
  437. }
  438. if(run_mode){
  439. while(run_count >= 1<<ff_log2_run[run_index]){
  440. run_count -= 1<<ff_log2_run[run_index];
  441. run_index++;
  442. put_bits(&s->pb, 1, 1);
  443. }
  444. if(run_count)
  445. put_bits(&s->pb, 1, 1);
  446. }
  447. s->run_index= run_index;
  448. return 0;
  449. }
  450. static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  451. int x,y,i;
  452. const int ring_size= s->avctx->context_model ? 3 : 2;
  453. int_fast16_t *sample[3];
  454. s->run_index=0;
  455. memset(s->sample_buffer, 0, ring_size*(w+6)*sizeof(*s->sample_buffer));
  456. for(y=0; y<h; y++){
  457. for(i=0; i<ring_size; i++)
  458. sample[i]= s->sample_buffer + (w+6)*((h+i-y)%ring_size) + 3;
  459. sample[0][-1]= sample[1][0 ];
  460. sample[1][ w]= sample[1][w-1];
  461. //{START_TIMER
  462. if(s->avctx->bits_per_raw_sample<=8){
  463. for(x=0; x<w; x++){
  464. sample[0][x]= src[x + stride*y];
  465. }
  466. encode_line(s, w, sample, plane_index, 8);
  467. }else{
  468. for(x=0; x<w; x++){
  469. sample[0][x]= ((uint16_t*)(src + stride*y))[x] >> (16 - s->avctx->bits_per_raw_sample);
  470. }
  471. encode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
  472. }
  473. //STOP_TIMER("encode line")}
  474. }
  475. }
  476. static void encode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  477. int x, y, p, i;
  478. const int ring_size= s->avctx->context_model ? 3 : 2;
  479. int_fast16_t *sample[3][3];
  480. s->run_index=0;
  481. memset(s->sample_buffer, 0, ring_size*3*(w+6)*sizeof(*s->sample_buffer));
  482. for(y=0; y<h; y++){
  483. for(i=0; i<ring_size; i++)
  484. for(p=0; p<3; p++)
  485. sample[p][i]= s->sample_buffer + p*ring_size*(w+6) + ((h+i-y)%ring_size)*(w+6) + 3;
  486. for(x=0; x<w; x++){
  487. int v= src[x + stride*y];
  488. int b= v&0xFF;
  489. int g= (v>>8)&0xFF;
  490. int r= (v>>16)&0xFF;
  491. b -= g;
  492. r -= g;
  493. g += (b + r)>>2;
  494. b += 0x100;
  495. r += 0x100;
  496. // assert(g>=0 && b>=0 && r>=0);
  497. // assert(g<256 && b<512 && r<512);
  498. sample[0][0][x]= g;
  499. sample[1][0][x]= b;
  500. sample[2][0][x]= r;
  501. }
  502. for(p=0; p<3; p++){
  503. sample[p][0][-1]= sample[p][1][0 ];
  504. sample[p][1][ w]= sample[p][1][w-1];
  505. encode_line(s, w, sample[p], FFMIN(p, 1), 9);
  506. }
  507. }
  508. }
  509. static void write_quant_table(RangeCoder *c, int16_t *quant_table){
  510. int last=0;
  511. int i;
  512. uint8_t state[CONTEXT_SIZE];
  513. memset(state, 128, sizeof(state));
  514. for(i=1; i<128 ; i++){
  515. if(quant_table[i] != quant_table[i-1]){
  516. put_symbol(c, state, i-last-1, 0);
  517. last= i;
  518. }
  519. }
  520. put_symbol(c, state, i-last-1, 0);
  521. }
  522. static void write_quant_tables(RangeCoder *c, int16_t quant_table[5][256]){
  523. int i;
  524. for(i=0; i<5; i++)
  525. write_quant_table(c, quant_table[i]);
  526. }
  527. static void write_header(FFV1Context *f){
  528. uint8_t state[CONTEXT_SIZE];
  529. int i;
  530. RangeCoder * const c= &f->c;
  531. memset(state, 128, sizeof(state));
  532. if(f->version < 2){
  533. put_symbol(c, state, f->version, 0);
  534. put_symbol(c, state, f->ac, 0);
  535. if(f->ac>1){
  536. for(i=1; i<256; i++){
  537. f->state_transition[i]=ver2_state[i];
  538. put_symbol(c, state, ver2_state[i] - c->one_state[i], 1);
  539. }
  540. }
  541. put_symbol(c, state, f->colorspace, 0); //YUV cs type
  542. if(f->version>0)
  543. put_symbol(c, state, f->avctx->bits_per_raw_sample, 0);
  544. put_rac(c, state, 1); //chroma planes
  545. put_symbol(c, state, f->chroma_h_shift, 0);
  546. put_symbol(c, state, f->chroma_v_shift, 0);
  547. put_rac(c, state, 0); //no transparency plane
  548. write_quant_tables(c, f->quant_table);
  549. }else{
  550. put_symbol(c, state, f->avctx->context_model, 0);
  551. }
  552. }
  553. #endif /* CONFIG_FFV1_ENCODER */
  554. static av_cold int common_init(AVCodecContext *avctx){
  555. FFV1Context *s = avctx->priv_data;
  556. s->avctx= avctx;
  557. s->flags= avctx->flags;
  558. dsputil_init(&s->dsp, avctx);
  559. s->width = avctx->width;
  560. s->height= avctx->height;
  561. assert(s->width && s->height);
  562. s->sample_buffer = av_malloc(6 * (s->width+6) * sizeof(*s->sample_buffer));
  563. if (!s->sample_buffer)
  564. return AVERROR(ENOMEM);
  565. return 0;
  566. }
  567. #if CONFIG_FFV1_ENCODER
  568. static int write_extra_header(FFV1Context *f){
  569. RangeCoder * const c= &f->c;
  570. uint8_t state[CONTEXT_SIZE];
  571. int i;
  572. memset(state, 128, sizeof(state));
  573. f->avctx->extradata= av_malloc(f->avctx->extradata_size= 10000);
  574. ff_init_range_encoder(c, f->avctx->extradata, f->avctx->extradata_size);
  575. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  576. put_symbol(c, state, f->version, 0);
  577. put_symbol(c, state, f->ac, 0);
  578. if(f->ac>1){
  579. for(i=1; i<256; i++){
  580. f->state_transition[i]=ver2_state[i];
  581. put_symbol(c, state, ver2_state[i] - c->one_state[i], 1);
  582. }
  583. }
  584. put_symbol(c, state, f->colorspace, 0); //YUV cs type
  585. put_symbol(c, state, f->avctx->bits_per_raw_sample, 0);
  586. put_rac(c, state, 1); //chroma planes
  587. put_symbol(c, state, f->chroma_h_shift, 0);
  588. put_symbol(c, state, f->chroma_v_shift, 0);
  589. put_rac(c, state, 0); //no transparency plane
  590. put_symbol(c, state, f->quant_table_count, 0);
  591. for(i=0; i<f->quant_table_count; i++)
  592. write_quant_tables(c, f->quant_tables[i]);
  593. f->avctx->extradata_size= ff_rac_terminate(c);
  594. return 0;
  595. }
  596. static av_cold int encode_init(AVCodecContext *avctx)
  597. {
  598. FFV1Context *s = avctx->priv_data;
  599. int i;
  600. common_init(avctx);
  601. s->version=0;
  602. s->ac= avctx->coder_type ? 2:0;
  603. s->plane_count=2;
  604. for(i=0; i<256; i++){
  605. s->quant_table_count=2;
  606. if(avctx->bits_per_raw_sample <=8){
  607. s->quant_tables[0][0][i]= quant11[i];
  608. s->quant_tables[0][1][i]= 11*quant11[i];
  609. s->quant_tables[0][2][i]= 11*11*quant11[i];
  610. s->quant_tables[1][0][i]= quant11[i];
  611. s->quant_tables[1][1][i]= 11*quant11[i];
  612. s->quant_tables[1][2][i]= 11*11*quant5 [i];
  613. s->quant_tables[1][3][i]= 5*11*11*quant5 [i];
  614. s->quant_tables[1][4][i]= 5*5*11*11*quant5 [i];
  615. }else{
  616. s->quant_tables[0][0][i]= quant9_10bit[i];
  617. s->quant_tables[0][1][i]= 11*quant9_10bit[i];
  618. s->quant_tables[0][2][i]= 11*11*quant9_10bit[i];
  619. s->quant_tables[1][0][i]= quant9_10bit[i];
  620. s->quant_tables[1][1][i]= 11*quant9_10bit[i];
  621. s->quant_tables[1][2][i]= 11*11*quant5_10bit[i];
  622. s->quant_tables[1][3][i]= 5*11*11*quant5_10bit[i];
  623. s->quant_tables[1][4][i]= 5*5*11*11*quant5_10bit[i];
  624. }
  625. }
  626. memcpy(s->quant_table, s->quant_tables[avctx->context_model], sizeof(s->quant_table));
  627. for(i=0; i<s->plane_count; i++){
  628. PlaneContext * const p= &s->plane[i];
  629. if(avctx->context_model==0){
  630. p->context_count= (11*11*11+1)/2;
  631. }else{
  632. p->context_count= (11*11*5*5*5+1)/2;
  633. }
  634. if(s->ac){
  635. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  636. }else{
  637. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  638. }
  639. }
  640. avctx->coded_frame= &s->picture;
  641. switch(avctx->pix_fmt){
  642. case PIX_FMT_YUV444P16:
  643. case PIX_FMT_YUV422P16:
  644. case PIX_FMT_YUV420P16:
  645. if(avctx->bits_per_raw_sample <=8){
  646. av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
  647. return -1;
  648. }
  649. if(!s->ac){
  650. av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample of more than 8 needs -coder 1 currently\n");
  651. return -1;
  652. }
  653. s->version= FFMAX(s->version, 1);
  654. case PIX_FMT_YUV444P:
  655. case PIX_FMT_YUV422P:
  656. case PIX_FMT_YUV420P:
  657. case PIX_FMT_YUV411P:
  658. case PIX_FMT_YUV410P:
  659. s->colorspace= 0;
  660. break;
  661. case PIX_FMT_RGB32:
  662. s->colorspace= 1;
  663. break;
  664. default:
  665. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  666. return -1;
  667. }
  668. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  669. s->picture_number=0;
  670. if(s->version>1)
  671. write_extra_header(s);
  672. return 0;
  673. }
  674. #endif /* CONFIG_FFV1_ENCODER */
  675. static void clear_state(FFV1Context *f){
  676. int i, j;
  677. for(i=0; i<f->plane_count; i++){
  678. PlaneContext *p= &f->plane[i];
  679. p->interlace_bit_state[0]= 128;
  680. p->interlace_bit_state[1]= 128;
  681. for(j=0; j<p->context_count; j++){
  682. if(f->ac){
  683. memset(p->state[j], 128, sizeof(uint8_t)*CONTEXT_SIZE);
  684. }else{
  685. p->vlc_state[j].drift= 0;
  686. p->vlc_state[j].error_sum= 4; //FFMAX((RANGE + 32)/64, 2);
  687. p->vlc_state[j].bias= 0;
  688. p->vlc_state[j].count= 1;
  689. }
  690. }
  691. }
  692. }
  693. #if CONFIG_FFV1_ENCODER
  694. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  695. FFV1Context *f = avctx->priv_data;
  696. RangeCoder * const c= &f->c;
  697. AVFrame *pict = data;
  698. const int width= f->width;
  699. const int height= f->height;
  700. AVFrame * const p= &f->picture;
  701. int used_count= 0;
  702. uint8_t keystate=128;
  703. ff_init_range_encoder(c, buf, buf_size);
  704. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  705. *p = *pict;
  706. p->pict_type= FF_I_TYPE;
  707. if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){
  708. put_rac(c, &keystate, 1);
  709. p->key_frame= 1;
  710. write_header(f);
  711. clear_state(f);
  712. }else{
  713. put_rac(c, &keystate, 0);
  714. p->key_frame= 0;
  715. }
  716. if(!f->ac){
  717. used_count += ff_rac_terminate(c);
  718. //printf("pos=%d\n", used_count);
  719. init_put_bits(&f->pb, buf + used_count, buf_size - used_count);
  720. }else if (f->ac>1){
  721. int i;
  722. for(i=1; i<256; i++){
  723. c->one_state[i]= f->state_transition[i];
  724. c->zero_state[256-i]= 256-c->one_state[i];
  725. }
  726. }
  727. if(f->colorspace==0){
  728. const int chroma_width = -((-width )>>f->chroma_h_shift);
  729. const int chroma_height= -((-height)>>f->chroma_v_shift);
  730. encode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  731. encode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  732. encode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
  733. }else{
  734. encode_rgb_frame(f, (uint32_t*)(p->data[0]), width, height, p->linesize[0]/4);
  735. }
  736. emms_c();
  737. f->picture_number++;
  738. if(f->ac){
  739. return ff_rac_terminate(c);
  740. }else{
  741. flush_put_bits(&f->pb); //nicer padding FIXME
  742. return used_count + (put_bits_count(&f->pb)+7)/8;
  743. }
  744. }
  745. #endif /* CONFIG_FFV1_ENCODER */
  746. static av_cold int common_end(AVCodecContext *avctx){
  747. FFV1Context *s = avctx->priv_data;
  748. int i;
  749. for(i=0; i<s->plane_count; i++){
  750. PlaneContext *p= &s->plane[i];
  751. av_freep(&p->state);
  752. av_freep(&p->vlc_state);
  753. }
  754. av_freep(&s->sample_buffer);
  755. return 0;
  756. }
  757. static av_always_inline void decode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
  758. PlaneContext * const p= &s->plane[plane_index];
  759. RangeCoder * const c= &s->c;
  760. int x;
  761. int run_count=0;
  762. int run_mode=0;
  763. int run_index= s->run_index;
  764. for(x=0; x<w; x++){
  765. int diff, context, sign;
  766. context= get_context(s, sample[1] + x, sample[0] + x, sample[1] + x);
  767. if(context < 0){
  768. context= -context;
  769. sign=1;
  770. }else
  771. sign=0;
  772. if(s->ac){
  773. diff= get_symbol_inline(c, p->state[context], 1);
  774. }else{
  775. if(context == 0 && run_mode==0) run_mode=1;
  776. if(run_mode){
  777. if(run_count==0 && run_mode==1){
  778. if(get_bits1(&s->gb)){
  779. run_count = 1<<ff_log2_run[run_index];
  780. if(x + run_count <= w) run_index++;
  781. }else{
  782. if(ff_log2_run[run_index]) run_count = get_bits(&s->gb, ff_log2_run[run_index]);
  783. else run_count=0;
  784. if(run_index) run_index--;
  785. run_mode=2;
  786. }
  787. }
  788. run_count--;
  789. if(run_count < 0){
  790. run_mode=0;
  791. run_count=0;
  792. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  793. if(diff>=0) diff++;
  794. }else
  795. diff=0;
  796. }else
  797. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  798. // 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));
  799. }
  800. if(sign) diff= -diff;
  801. sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
  802. }
  803. s->run_index= run_index;
  804. }
  805. static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  806. int x, y;
  807. int_fast16_t *sample[2];
  808. sample[0]=s->sample_buffer +3;
  809. sample[1]=s->sample_buffer+w+6+3;
  810. s->run_index=0;
  811. memset(s->sample_buffer, 0, 2*(w+6)*sizeof(*s->sample_buffer));
  812. for(y=0; y<h; y++){
  813. int_fast16_t *temp= sample[0]; //FIXME try a normal buffer
  814. sample[0]= sample[1];
  815. sample[1]= temp;
  816. sample[1][-1]= sample[0][0 ];
  817. sample[0][ w]= sample[0][w-1];
  818. //{START_TIMER
  819. if(s->avctx->bits_per_raw_sample <= 8){
  820. decode_line(s, w, sample, plane_index, 8);
  821. for(x=0; x<w; x++){
  822. src[x + stride*y]= sample[1][x];
  823. }
  824. }else{
  825. decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
  826. for(x=0; x<w; x++){
  827. ((uint16_t*)(src + stride*y))[x]= sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
  828. }
  829. }
  830. //STOP_TIMER("decode-line")}
  831. }
  832. }
  833. static void decode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  834. int x, y, p;
  835. int_fast16_t *sample[3][2];
  836. for(x=0; x<3; x++){
  837. sample[x][0] = s->sample_buffer + x*2 *(w+6) + 3;
  838. sample[x][1] = s->sample_buffer + (x*2+1)*(w+6) + 3;
  839. }
  840. s->run_index=0;
  841. memset(s->sample_buffer, 0, 6*(w+6)*sizeof(*s->sample_buffer));
  842. for(y=0; y<h; y++){
  843. for(p=0; p<3; p++){
  844. int_fast16_t *temp= sample[p][0]; //FIXME try a normal buffer
  845. sample[p][0]= sample[p][1];
  846. sample[p][1]= temp;
  847. sample[p][1][-1]= sample[p][0][0 ];
  848. sample[p][0][ w]= sample[p][0][w-1];
  849. decode_line(s, w, sample[p], FFMIN(p, 1), 9);
  850. }
  851. for(x=0; x<w; x++){
  852. int g= sample[0][1][x];
  853. int b= sample[1][1][x];
  854. int r= sample[2][1][x];
  855. // assert(g>=0 && b>=0 && r>=0);
  856. // assert(g<256 && b<512 && r<512);
  857. b -= 0x100;
  858. r -= 0x100;
  859. g -= (b + r)>>2;
  860. b += g;
  861. r += g;
  862. src[x + stride*y]= b + (g<<8) + (r<<16) + (0xFF<<24);
  863. }
  864. }
  865. }
  866. static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){
  867. int v;
  868. int i=0;
  869. uint8_t state[CONTEXT_SIZE];
  870. memset(state, 128, sizeof(state));
  871. for(v=0; i<128 ; v++){
  872. int len= get_symbol(c, state, 0) + 1;
  873. if(len + i > 128) return -1;
  874. while(len--){
  875. quant_table[i] = scale*v;
  876. i++;
  877. //printf("%2d ",v);
  878. //if(i%16==0) printf("\n");
  879. }
  880. }
  881. for(i=1; i<128; i++){
  882. quant_table[256-i]= -quant_table[i];
  883. }
  884. quant_table[128]= -quant_table[127];
  885. return 2*v - 1;
  886. }
  887. static int read_quant_tables(RangeCoder *c, int16_t quant_table[5][256]){
  888. int i;
  889. int context_count=1;
  890. for(i=0; i<5; i++){
  891. context_count*= read_quant_table(c, quant_table[i], context_count);
  892. if(context_count > 32768U){
  893. return -1;
  894. }
  895. }
  896. return (context_count+1)/2;
  897. }
  898. static int read_extra_header(FFV1Context *f){
  899. RangeCoder * const c= &f->c;
  900. uint8_t state[CONTEXT_SIZE];
  901. int i;
  902. memset(state, 128, sizeof(state));
  903. ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
  904. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  905. f->version= get_symbol(c, state, 0);
  906. f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
  907. if(f->ac>1){
  908. for(i=1; i<256; i++){
  909. f->state_transition[i]= get_symbol(c, state, 1) + c->one_state[i];
  910. }
  911. }
  912. f->colorspace= get_symbol(c, state, 0); //YUV cs type
  913. f->avctx->bits_per_raw_sample= get_symbol(c, state, 0);
  914. get_rac(c, state); //no chroma = false
  915. f->chroma_h_shift= get_symbol(c, state, 0);
  916. f->chroma_v_shift= get_symbol(c, state, 0);
  917. get_rac(c, state); //transparency plane
  918. f->plane_count= 2;
  919. f->quant_table_count= get_symbol(c, state, 0);
  920. if(f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
  921. return -1;
  922. for(i=0; i<f->quant_table_count; i++){
  923. if((f->context_count[i]= read_quant_tables(c, f->quant_tables[i])) < 0){
  924. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  925. return -1;
  926. }
  927. }
  928. return 0;
  929. }
  930. static int read_header(FFV1Context *f){
  931. uint8_t state[CONTEXT_SIZE];
  932. int i, context_count;
  933. RangeCoder * const c= &f->c;
  934. memset(state, 128, sizeof(state));
  935. if(f->version < 2){
  936. f->version= get_symbol(c, state, 0);
  937. f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
  938. if(f->ac>1){
  939. for(i=1; i<256; i++){
  940. f->state_transition[i]= get_symbol(c, state, 1) + c->one_state[i];
  941. }
  942. }
  943. f->colorspace= get_symbol(c, state, 0); //YUV cs type
  944. if(f->version>0)
  945. f->avctx->bits_per_raw_sample= get_symbol(c, state, 0);
  946. get_rac(c, state); //no chroma = false
  947. f->chroma_h_shift= get_symbol(c, state, 0);
  948. f->chroma_v_shift= get_symbol(c, state, 0);
  949. get_rac(c, state); //transparency plane
  950. f->plane_count= 2;
  951. }
  952. if(f->colorspace==0){
  953. if(f->avctx->bits_per_raw_sample<=8){
  954. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  955. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
  956. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
  957. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
  958. case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
  959. case 0x22: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
  960. default:
  961. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  962. return -1;
  963. }
  964. }else{
  965. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  966. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P16; break;
  967. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P16; break;
  968. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P16; break;
  969. default:
  970. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  971. return -1;
  972. }
  973. }
  974. }else if(f->colorspace==1){
  975. if(f->chroma_h_shift || f->chroma_v_shift){
  976. av_log(f->avctx, AV_LOG_ERROR, "chroma subsampling not supported in this colorspace\n");
  977. return -1;
  978. }
  979. f->avctx->pix_fmt= PIX_FMT_RGB32;
  980. }else{
  981. av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
  982. return -1;
  983. }
  984. //printf("%d %d %d\n", f->chroma_h_shift, f->chroma_v_shift,f->avctx->pix_fmt);
  985. if(f->version < 2){
  986. context_count= read_quant_tables(c, f->quant_table);
  987. if(context_count < 0){
  988. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  989. return -1;
  990. }
  991. }else{
  992. i=get_symbol(c, state, 0);
  993. if(i > (unsigned)f->quant_table_count){
  994. av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
  995. return -1;
  996. }
  997. memcpy(f->quant_table, f->quant_tables[i], sizeof(f->quant_table));
  998. context_count= f->context_count[i];
  999. }
  1000. for(i=0; i<f->plane_count; i++){
  1001. PlaneContext * const p= &f->plane[i];
  1002. p->context_count= context_count;
  1003. if(f->ac){
  1004. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  1005. }else{
  1006. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  1007. }
  1008. }
  1009. return 0;
  1010. }
  1011. static av_cold int decode_init(AVCodecContext *avctx)
  1012. {
  1013. FFV1Context *f = avctx->priv_data;
  1014. common_init(avctx);
  1015. if(avctx->extradata)
  1016. return read_extra_header(f);
  1017. return 0;
  1018. }
  1019. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt){
  1020. const uint8_t *buf = avpkt->data;
  1021. int buf_size = avpkt->size;
  1022. FFV1Context *f = avctx->priv_data;
  1023. RangeCoder * const c= &f->c;
  1024. const int width= f->width;
  1025. const int height= f->height;
  1026. AVFrame * const p= &f->picture;
  1027. int bytes_read;
  1028. uint8_t keystate= 128;
  1029. AVFrame *picture = data;
  1030. ff_init_range_decoder(c, buf, buf_size);
  1031. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  1032. p->pict_type= FF_I_TYPE; //FIXME I vs. P
  1033. if(get_rac(c, &keystate)){
  1034. p->key_frame= 1;
  1035. if(read_header(f) < 0)
  1036. return -1;
  1037. clear_state(f);
  1038. }else{
  1039. p->key_frame= 0;
  1040. }
  1041. if(f->ac>1){
  1042. int i;
  1043. for(i=1; i<256; i++){
  1044. c->one_state[i]= f->state_transition[i];
  1045. c->zero_state[256-i]= 256-c->one_state[i];
  1046. }
  1047. }
  1048. if(!f->plane[0].state && !f->plane[0].vlc_state)
  1049. return -1;
  1050. p->reference= 0;
  1051. if(avctx->get_buffer(avctx, p) < 0){
  1052. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1053. return -1;
  1054. }
  1055. if(avctx->debug&FF_DEBUG_PICT_INFO)
  1056. av_log(avctx, AV_LOG_ERROR, "keyframe:%d coder:%d\n", p->key_frame, f->ac);
  1057. if(!f->ac){
  1058. bytes_read = c->bytestream - c->bytestream_start - 1;
  1059. if(bytes_read ==0) av_log(avctx, AV_LOG_ERROR, "error at end of AC stream\n"); //FIXME
  1060. //printf("pos=%d\n", bytes_read);
  1061. init_get_bits(&f->gb, buf + bytes_read, buf_size - bytes_read);
  1062. } else {
  1063. bytes_read = 0; /* avoid warning */
  1064. }
  1065. if(f->colorspace==0){
  1066. const int chroma_width = -((-width )>>f->chroma_h_shift);
  1067. const int chroma_height= -((-height)>>f->chroma_v_shift);
  1068. decode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  1069. decode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  1070. decode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
  1071. }else{
  1072. decode_rgb_frame(f, (uint32_t*)p->data[0], width, height, p->linesize[0]/4);
  1073. }
  1074. emms_c();
  1075. f->picture_number++;
  1076. *picture= *p;
  1077. avctx->release_buffer(avctx, p); //FIXME
  1078. *data_size = sizeof(AVFrame);
  1079. if(f->ac){
  1080. bytes_read= c->bytestream - c->bytestream_start - 1;
  1081. if(bytes_read ==0) av_log(f->avctx, AV_LOG_ERROR, "error at end of frame\n");
  1082. }else{
  1083. bytes_read+= (get_bits_count(&f->gb)+7)/8;
  1084. }
  1085. return bytes_read;
  1086. }
  1087. AVCodec ffv1_decoder = {
  1088. "ffv1",
  1089. AVMEDIA_TYPE_VIDEO,
  1090. CODEC_ID_FFV1,
  1091. sizeof(FFV1Context),
  1092. decode_init,
  1093. NULL,
  1094. common_end,
  1095. decode_frame,
  1096. CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  1097. NULL,
  1098. .long_name= NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  1099. };
  1100. #if CONFIG_FFV1_ENCODER
  1101. AVCodec ffv1_encoder = {
  1102. "ffv1",
  1103. AVMEDIA_TYPE_VIDEO,
  1104. CODEC_ID_FFV1,
  1105. sizeof(FFV1Context),
  1106. encode_init,
  1107. encode_frame,
  1108. common_end,
  1109. .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},
  1110. .long_name= NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  1111. };
  1112. #endif