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.

1511 lines
47KB

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