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.

1473 lines
45KB

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