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.

1603 lines
51KB

  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. uint64_t rc_stat[256][2];
  221. int version;
  222. int width, height;
  223. int chroma_h_shift, chroma_v_shift;
  224. int flags;
  225. int picture_number;
  226. AVFrame picture;
  227. int plane_count;
  228. int ac; ///< 1=range coder <-> 0=golomb rice
  229. PlaneContext plane[MAX_PLANES];
  230. int16_t quant_table[MAX_CONTEXT_INPUTS][256];
  231. int16_t quant_tables[MAX_QUANT_TABLES][MAX_CONTEXT_INPUTS][256];
  232. int context_count[MAX_QUANT_TABLES];
  233. uint8_t state_transition[256];
  234. int run_index;
  235. int colorspace;
  236. int_fast16_t *sample_buffer;
  237. int quant_table_count;
  238. DSPContext dsp;
  239. struct FFV1Context *slice_context[MAX_SLICES];
  240. int slice_count;
  241. int num_v_slices;
  242. int num_h_slices;
  243. int slice_width;
  244. int slice_height;
  245. int slice_x;
  246. int slice_y;
  247. }FFV1Context;
  248. static av_always_inline int fold(int diff, int bits){
  249. if(bits==8)
  250. diff= (int8_t)diff;
  251. else{
  252. diff+= 1<<(bits-1);
  253. diff&=(1<<bits)-1;
  254. diff-= 1<<(bits-1);
  255. }
  256. return diff;
  257. }
  258. static inline int predict(int_fast16_t *src, int_fast16_t *last){
  259. const int LT= last[-1];
  260. const int T= last[ 0];
  261. const int L = src[-1];
  262. return mid_pred(L, L + T - LT, T);
  263. }
  264. static inline int get_context(PlaneContext *p, int_fast16_t *src, int_fast16_t *last, int_fast16_t *last2){
  265. const int LT= last[-1];
  266. const int T= last[ 0];
  267. const int RT= last[ 1];
  268. const int L = src[-1];
  269. if(p->quant_table[3][127]){
  270. const int TT= last2[0];
  271. const int LL= src[-2];
  272. return p->quant_table[0][(L-LT) & 0xFF] + p->quant_table[1][(LT-T) & 0xFF] + p->quant_table[2][(T-RT) & 0xFF]
  273. +p->quant_table[3][(LL-L) & 0xFF] + p->quant_table[4][(TT-T) & 0xFF];
  274. }else
  275. return p->quant_table[0][(L-LT) & 0xFF] + p->quant_table[1][(LT-T) & 0xFF] + p->quant_table[2][(T-RT) & 0xFF];
  276. }
  277. static inline void put_symbol_inline(RangeCoder *c, uint8_t *state, int v, int is_signed, uint64_t rc_stat[256][2]){
  278. int i;
  279. #define put_rac(C,S,B) \
  280. do{\
  281. rc_stat[*(S)][B]++;\
  282. put_rac(C,S,B);\
  283. }while(0)
  284. if(v){
  285. const int a= FFABS(v);
  286. const int e= av_log2(a);
  287. put_rac(c, state+0, 0);
  288. if(e<=9){
  289. for(i=0; i<e; i++){
  290. put_rac(c, state+1+i, 1); //1..10
  291. }
  292. put_rac(c, state+1+i, 0);
  293. for(i=e-1; i>=0; i--){
  294. put_rac(c, state+22+i, (a>>i)&1); //22..31
  295. }
  296. if(is_signed)
  297. put_rac(c, state+11 + e, v < 0); //11..21
  298. }else{
  299. for(i=0; i<e; i++){
  300. put_rac(c, state+1+FFMIN(i,9), 1); //1..10
  301. }
  302. put_rac(c, state+1+9, 0);
  303. for(i=e-1; i>=0; i--){
  304. put_rac(c, state+22+FFMIN(i,9), (a>>i)&1); //22..31
  305. }
  306. if(is_signed)
  307. put_rac(c, state+11 + 10, v < 0); //11..21
  308. }
  309. }else{
  310. put_rac(c, state+0, 1);
  311. }
  312. #undef put_rac
  313. }
  314. static void av_noinline put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
  315. uint64_t rc_stat[256][2]; //we dont bother counting header bits.
  316. put_symbol_inline(c, state, v, is_signed, rc_stat);
  317. }
  318. static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state, int is_signed){
  319. if(get_rac(c, state+0))
  320. return 0;
  321. else{
  322. int i, e, a;
  323. e= 0;
  324. while(get_rac(c, state+1 + FFMIN(e,9))){ //1..10
  325. e++;
  326. }
  327. a= 1;
  328. for(i=e-1; i>=0; i--){
  329. a += a + get_rac(c, state+22 + FFMIN(i,9)); //22..31
  330. }
  331. e= -(is_signed && get_rac(c, state+11 + FFMIN(e, 10))); //11..21
  332. return (a^e)-e;
  333. }
  334. }
  335. static int av_noinline get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
  336. return get_symbol_inline(c, state, is_signed);
  337. }
  338. static inline void update_vlc_state(VlcState * const state, const int v){
  339. int drift= state->drift;
  340. int count= state->count;
  341. state->error_sum += FFABS(v);
  342. drift += v;
  343. if(count == 128){ //FIXME variable
  344. count >>= 1;
  345. drift >>= 1;
  346. state->error_sum >>= 1;
  347. }
  348. count++;
  349. if(drift <= -count){
  350. if(state->bias > -128) state->bias--;
  351. drift += count;
  352. if(drift <= -count)
  353. drift= -count + 1;
  354. }else if(drift > 0){
  355. if(state->bias < 127) state->bias++;
  356. drift -= count;
  357. if(drift > 0)
  358. drift= 0;
  359. }
  360. state->drift= drift;
  361. state->count= count;
  362. }
  363. static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int v, int bits){
  364. int i, k, code;
  365. //printf("final: %d ", v);
  366. v = fold(v - state->bias, bits);
  367. i= state->count;
  368. k=0;
  369. while(i < state->error_sum){ //FIXME optimize
  370. k++;
  371. i += i;
  372. }
  373. assert(k<=8);
  374. #if 0 // JPEG LS
  375. if(k==0 && 2*state->drift <= - state->count) code= v ^ (-1);
  376. else code= v;
  377. #else
  378. code= v ^ ((2*state->drift + state->count)>>31);
  379. #endif
  380. //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);
  381. set_sr_golomb(pb, code, k, 12, bits);
  382. update_vlc_state(state, v);
  383. }
  384. static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state, int bits){
  385. int k, i, v, ret;
  386. i= state->count;
  387. k=0;
  388. while(i < state->error_sum){ //FIXME optimize
  389. k++;
  390. i += i;
  391. }
  392. assert(k<=8);
  393. v= get_sr_golomb(gb, k, 12, bits);
  394. //printf("v:%d bias:%d error:%d drift:%d count:%d k:%d", v, state->bias, state->error_sum, state->drift, state->count, k);
  395. #if 0 // JPEG LS
  396. if(k==0 && 2*state->drift <= - state->count) v ^= (-1);
  397. #else
  398. v ^= ((2*state->drift + state->count)>>31);
  399. #endif
  400. ret= fold(v + state->bias, bits);
  401. update_vlc_state(state, v);
  402. //printf("final: %d\n", ret);
  403. return ret;
  404. }
  405. #if CONFIG_FFV1_ENCODER
  406. static inline int encode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
  407. PlaneContext * const p= &s->plane[plane_index];
  408. RangeCoder * const c= &s->c;
  409. int x;
  410. int run_index= s->run_index;
  411. int run_count=0;
  412. int run_mode=0;
  413. if(s->ac){
  414. if(c->bytestream_end - c->bytestream < w*20){
  415. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  416. return -1;
  417. }
  418. }else{
  419. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < w*4){
  420. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  421. return -1;
  422. }
  423. }
  424. for(x=0; x<w; x++){
  425. int diff, context;
  426. context= get_context(p, sample[0]+x, sample[1]+x, sample[2]+x);
  427. diff= sample[0][x] - predict(sample[0]+x, sample[1]+x);
  428. if(context < 0){
  429. context = -context;
  430. diff= -diff;
  431. }
  432. diff= fold(diff, bits);
  433. if(s->ac){
  434. put_symbol_inline(c, p->state[context], diff, 1, s->rc_stat);
  435. }else{
  436. if(context == 0) run_mode=1;
  437. if(run_mode){
  438. if(diff){
  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. put_bits(&s->pb, 1 + ff_log2_run[run_index], run_count);
  445. if(run_index) run_index--;
  446. run_count=0;
  447. run_mode=0;
  448. if(diff>0) diff--;
  449. }else{
  450. run_count++;
  451. }
  452. }
  453. // 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));
  454. if(run_mode == 0)
  455. put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
  456. }
  457. }
  458. if(run_mode){
  459. while(run_count >= 1<<ff_log2_run[run_index]){
  460. run_count -= 1<<ff_log2_run[run_index];
  461. run_index++;
  462. put_bits(&s->pb, 1, 1);
  463. }
  464. if(run_count)
  465. put_bits(&s->pb, 1, 1);
  466. }
  467. s->run_index= run_index;
  468. return 0;
  469. }
  470. static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  471. int x,y,i;
  472. const int ring_size= s->avctx->context_model ? 3 : 2;
  473. int_fast16_t *sample[3];
  474. s->run_index=0;
  475. memset(s->sample_buffer, 0, ring_size*(w+6)*sizeof(*s->sample_buffer));
  476. for(y=0; y<h; y++){
  477. for(i=0; i<ring_size; i++)
  478. sample[i]= s->sample_buffer + (w+6)*((h+i-y)%ring_size) + 3;
  479. sample[0][-1]= sample[1][0 ];
  480. sample[1][ w]= sample[1][w-1];
  481. //{START_TIMER
  482. if(s->avctx->bits_per_raw_sample<=8){
  483. for(x=0; x<w; x++){
  484. sample[0][x]= src[x + stride*y];
  485. }
  486. encode_line(s, w, sample, plane_index, 8);
  487. }else{
  488. for(x=0; x<w; x++){
  489. sample[0][x]= ((uint16_t*)(src + stride*y))[x] >> (16 - s->avctx->bits_per_raw_sample);
  490. }
  491. encode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
  492. }
  493. //STOP_TIMER("encode line")}
  494. }
  495. }
  496. static void encode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  497. int x, y, p, i;
  498. const int ring_size= s->avctx->context_model ? 3 : 2;
  499. int_fast16_t *sample[3][3];
  500. s->run_index=0;
  501. memset(s->sample_buffer, 0, ring_size*3*(w+6)*sizeof(*s->sample_buffer));
  502. for(y=0; y<h; y++){
  503. for(i=0; i<ring_size; i++)
  504. for(p=0; p<3; p++)
  505. sample[p][i]= s->sample_buffer + p*ring_size*(w+6) + ((h+i-y)%ring_size)*(w+6) + 3;
  506. for(x=0; x<w; x++){
  507. int v= src[x + stride*y];
  508. int b= v&0xFF;
  509. int g= (v>>8)&0xFF;
  510. int r= (v>>16)&0xFF;
  511. b -= g;
  512. r -= g;
  513. g += (b + r)>>2;
  514. b += 0x100;
  515. r += 0x100;
  516. // assert(g>=0 && b>=0 && r>=0);
  517. // assert(g<256 && b<512 && r<512);
  518. sample[0][0][x]= g;
  519. sample[1][0][x]= b;
  520. sample[2][0][x]= r;
  521. }
  522. for(p=0; p<3; p++){
  523. sample[p][0][-1]= sample[p][1][0 ];
  524. sample[p][1][ w]= sample[p][1][w-1];
  525. encode_line(s, w, sample[p], FFMIN(p, 1), 9);
  526. }
  527. }
  528. }
  529. static void write_quant_table(RangeCoder *c, int16_t *quant_table){
  530. int last=0;
  531. int i;
  532. uint8_t state[CONTEXT_SIZE];
  533. memset(state, 128, sizeof(state));
  534. for(i=1; i<128 ; i++){
  535. if(quant_table[i] != quant_table[i-1]){
  536. put_symbol(c, state, i-last-1, 0);
  537. last= i;
  538. }
  539. }
  540. put_symbol(c, state, i-last-1, 0);
  541. }
  542. static void write_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256]){
  543. int i;
  544. for(i=0; i<5; i++)
  545. write_quant_table(c, quant_table[i]);
  546. }
  547. static void write_header(FFV1Context *f){
  548. uint8_t state[CONTEXT_SIZE];
  549. int i, j;
  550. RangeCoder * const c= &f->slice_context[0]->c;
  551. memset(state, 128, sizeof(state));
  552. if(f->version < 2){
  553. put_symbol(c, state, f->version, 0);
  554. put_symbol(c, state, f->ac, 0);
  555. if(f->ac>1){
  556. for(i=1; i<256; i++){
  557. put_symbol(c, state, f->state_transition[i] - c->one_state[i], 1);
  558. }
  559. }
  560. put_symbol(c, state, f->colorspace, 0); //YUV cs type
  561. if(f->version>0)
  562. put_symbol(c, state, f->avctx->bits_per_raw_sample, 0);
  563. put_rac(c, state, 1); //chroma planes
  564. put_symbol(c, state, f->chroma_h_shift, 0);
  565. put_symbol(c, state, f->chroma_v_shift, 0);
  566. put_rac(c, state, 0); //no transparency plane
  567. write_quant_tables(c, f->quant_table);
  568. }else{
  569. put_symbol(c, state, f->slice_count, 0);
  570. for(i=0; i<f->slice_count; i++){
  571. FFV1Context *fs= f->slice_context[i];
  572. put_symbol(c, state, (fs->slice_x +1)*f->num_h_slices / f->width , 0);
  573. put_symbol(c, state, (fs->slice_y +1)*f->num_v_slices / f->height , 0);
  574. put_symbol(c, state, (fs->slice_width +1)*f->num_h_slices / f->width -1, 0);
  575. put_symbol(c, state, (fs->slice_height+1)*f->num_v_slices / f->height-1, 0);
  576. for(j=0; j<f->plane_count; j++)
  577. put_symbol(c, state, f->avctx->context_model, 0);
  578. }
  579. }
  580. }
  581. #endif /* CONFIG_FFV1_ENCODER */
  582. static av_cold int common_init(AVCodecContext *avctx){
  583. FFV1Context *s = avctx->priv_data;
  584. s->avctx= avctx;
  585. s->flags= avctx->flags;
  586. dsputil_init(&s->dsp, avctx);
  587. s->width = avctx->width;
  588. s->height= avctx->height;
  589. assert(s->width && s->height);
  590. //defaults
  591. s->num_h_slices=1;
  592. s->num_v_slices=1;
  593. return 0;
  594. }
  595. static int init_slice_state(FFV1Context *f){
  596. int i, j;
  597. for(i=0; i<f->slice_count; i++){
  598. FFV1Context *fs= f->slice_context[i];
  599. for(j=0; j<f->plane_count; j++){
  600. PlaneContext * const p= &fs->plane[j];
  601. if(fs->ac){
  602. if(!p-> state) p-> state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  603. if(!p-> state)
  604. return AVERROR(ENOMEM);
  605. }else{
  606. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  607. if(!p->vlc_state)
  608. return AVERROR(ENOMEM);
  609. }
  610. }
  611. if (fs->ac>1){
  612. //FIXME only redo if state_transition changed
  613. for(j=1; j<256; j++){
  614. fs->c.one_state [ j]= fs->state_transition[j];
  615. fs->c.zero_state[256-j]= 256-fs->c.one_state [j];
  616. }
  617. }
  618. }
  619. return 0;
  620. }
  621. static av_cold int init_slice_contexts(FFV1Context *f){
  622. int i;
  623. f->slice_count= f->num_h_slices * f->num_v_slices;
  624. for(i=0; i<f->slice_count; i++){
  625. FFV1Context *fs= av_mallocz(sizeof(*fs));
  626. int sx= i % f->num_h_slices;
  627. int sy= i / f->num_h_slices;
  628. int sxs= f->avctx->width * sx / f->num_h_slices;
  629. int sxe= f->avctx->width *(sx+1) / f->num_h_slices;
  630. int sys= f->avctx->height* sy / f->num_v_slices;
  631. int sye= f->avctx->height*(sy+1) / f->num_v_slices;
  632. f->slice_context[i]= fs;
  633. memcpy(fs, f, sizeof(*fs));
  634. fs->slice_width = sxe - sxs;
  635. fs->slice_height= sye - sys;
  636. fs->slice_x = sxs;
  637. fs->slice_y = sys;
  638. fs->sample_buffer = av_malloc(6 * (fs->width+6) * sizeof(*fs->sample_buffer));
  639. if (!fs->sample_buffer)
  640. return AVERROR(ENOMEM);
  641. }
  642. return 0;
  643. }
  644. #if CONFIG_FFV1_ENCODER
  645. static int write_extra_header(FFV1Context *f){
  646. RangeCoder * const c= &f->c;
  647. uint8_t state[CONTEXT_SIZE];
  648. int i;
  649. memset(state, 128, sizeof(state));
  650. f->avctx->extradata= av_malloc(f->avctx->extradata_size= 10000);
  651. ff_init_range_encoder(c, f->avctx->extradata, f->avctx->extradata_size);
  652. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  653. put_symbol(c, state, f->version, 0);
  654. put_symbol(c, state, f->ac, 0);
  655. if(f->ac>1){
  656. for(i=1; i<256; i++){
  657. put_symbol(c, state, f->state_transition[i] - c->one_state[i], 1);
  658. }
  659. }
  660. put_symbol(c, state, f->colorspace, 0); //YUV cs type
  661. put_symbol(c, state, f->avctx->bits_per_raw_sample, 0);
  662. put_rac(c, state, 1); //chroma planes
  663. put_symbol(c, state, f->chroma_h_shift, 0);
  664. put_symbol(c, state, f->chroma_v_shift, 0);
  665. put_rac(c, state, 0); //no transparency plane
  666. put_symbol(c, state, f->num_h_slices-1, 0);
  667. put_symbol(c, state, f->num_v_slices-1, 0);
  668. put_symbol(c, state, f->quant_table_count, 0);
  669. for(i=0; i<f->quant_table_count; i++)
  670. write_quant_tables(c, f->quant_tables[i]);
  671. f->avctx->extradata_size= ff_rac_terminate(c);
  672. return 0;
  673. }
  674. static av_cold int encode_init(AVCodecContext *avctx)
  675. {
  676. FFV1Context *s = avctx->priv_data;
  677. int i, j, i2;
  678. common_init(avctx);
  679. s->version=0;
  680. s->ac= avctx->coder_type ? 2:0;
  681. if(s->ac>1)
  682. for(i=1; i<256; i++)
  683. s->state_transition[i]=ver2_state[i];
  684. s->plane_count=2;
  685. for(i=0; i<256; i++){
  686. s->quant_table_count=2;
  687. if(avctx->bits_per_raw_sample <=8){
  688. s->quant_tables[0][0][i]= quant11[i];
  689. s->quant_tables[0][1][i]= 11*quant11[i];
  690. s->quant_tables[0][2][i]= 11*11*quant11[i];
  691. s->quant_tables[1][0][i]= quant11[i];
  692. s->quant_tables[1][1][i]= 11*quant11[i];
  693. s->quant_tables[1][2][i]= 11*11*quant5 [i];
  694. s->quant_tables[1][3][i]= 5*11*11*quant5 [i];
  695. s->quant_tables[1][4][i]= 5*5*11*11*quant5 [i];
  696. }else{
  697. s->quant_tables[0][0][i]= quant9_10bit[i];
  698. s->quant_tables[0][1][i]= 11*quant9_10bit[i];
  699. s->quant_tables[0][2][i]= 11*11*quant9_10bit[i];
  700. s->quant_tables[1][0][i]= quant9_10bit[i];
  701. s->quant_tables[1][1][i]= 11*quant9_10bit[i];
  702. s->quant_tables[1][2][i]= 11*11*quant5_10bit[i];
  703. s->quant_tables[1][3][i]= 5*11*11*quant5_10bit[i];
  704. s->quant_tables[1][4][i]= 5*5*11*11*quant5_10bit[i];
  705. }
  706. }
  707. memcpy(s->quant_table, s->quant_tables[avctx->context_model], sizeof(s->quant_table));
  708. for(i=0; i<s->plane_count; i++){
  709. PlaneContext * const p= &s->plane[i];
  710. memcpy(p->quant_table, s->quant_table, sizeof(p->quant_table));
  711. if(avctx->context_model==0){
  712. p->context_count= (11*11*11+1)/2;
  713. }else{
  714. p->context_count= (11*11*5*5*5+1)/2;
  715. }
  716. }
  717. avctx->coded_frame= &s->picture;
  718. switch(avctx->pix_fmt){
  719. case PIX_FMT_YUV444P16:
  720. case PIX_FMT_YUV422P16:
  721. case PIX_FMT_YUV420P16:
  722. if(avctx->bits_per_raw_sample <=8){
  723. av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
  724. return -1;
  725. }
  726. if(!s->ac){
  727. av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample of more than 8 needs -coder 1 currently\n");
  728. return -1;
  729. }
  730. s->version= FFMAX(s->version, 1);
  731. case PIX_FMT_YUV444P:
  732. case PIX_FMT_YUV422P:
  733. case PIX_FMT_YUV420P:
  734. case PIX_FMT_YUV411P:
  735. case PIX_FMT_YUV410P:
  736. s->colorspace= 0;
  737. break;
  738. case PIX_FMT_RGB32:
  739. s->colorspace= 1;
  740. break;
  741. default:
  742. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  743. return -1;
  744. }
  745. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  746. s->picture_number=0;
  747. if(avctx->stats_in){
  748. char *p= avctx->stats_in;
  749. int changed;
  750. for(;;){
  751. for(j=0; j<256; j++){
  752. for(i=0; i<2; i++){
  753. char *next;
  754. s->rc_stat[j][i]= strtol(p, &next, 0);
  755. if(next==p){
  756. av_log(avctx, AV_LOG_ERROR, "2Pass file invalid at %d %d [%s]\n", j,i,p);
  757. return -1;
  758. }
  759. p=next;
  760. }
  761. }
  762. while(*p=='\n' || *p==' ') p++;
  763. if(p[0]==0) break;
  764. }
  765. do{
  766. changed=0;
  767. for(i=12; i<244; i++){
  768. for(i2=i+1; i2<245 && i2<i+4; i2++){
  769. #define COST(old, new) \
  770. s->rc_stat[old][0]*-log2((256-(new))/256.0)\
  771. +s->rc_stat[old][1]*-log2( (new) /256.0)
  772. #define COST2(old, new) \
  773. COST(old, new)\
  774. +COST(256-(old), 256-(new))
  775. double size0= COST2(i, i ) + COST2(i2, i2);
  776. double sizeX= COST2(i, i2) + COST2(i2, i );
  777. if(sizeX < size0 && i!=128 && i2!=128){ //128 is special we cant swap it around FIXME 127<->129 swap
  778. int j;
  779. FFSWAP(int, s->state_transition[ i], s->state_transition[ i2]);
  780. FFSWAP(int, s->state_transition[256-i], s->state_transition[256-i2]);
  781. FFSWAP(int, s->rc_stat[i ][0],s->rc_stat[ i2][0]);
  782. FFSWAP(int, s->rc_stat[i ][1],s->rc_stat[ i2][1]);
  783. FFSWAP(int, s->rc_stat[256-i][0],s->rc_stat[256-i2][0]);
  784. FFSWAP(int, s->rc_stat[256-i][1],s->rc_stat[256-i2][1]);
  785. for(j=1; j<256; j++){
  786. if (s->state_transition[j] == i ) s->state_transition[j] = i2;
  787. else if(s->state_transition[j] == i2) s->state_transition[j] = i ;
  788. if (s->state_transition[256-j] == 256-i ) s->state_transition[256-j] = 256-i2;
  789. else if(s->state_transition[256-j] == 256-i2) s->state_transition[256-j] = 256-i ;
  790. }
  791. changed=1;
  792. }
  793. }
  794. }
  795. }while(changed);
  796. }
  797. if(s->version>1){
  798. s->num_h_slices=2;
  799. s->num_v_slices=2;
  800. write_extra_header(s);
  801. }
  802. if(init_slice_contexts(s) < 0)
  803. return -1;
  804. if(init_slice_state(s) < 0)
  805. return -1;
  806. avctx->stats_out= av_mallocz(1024*30);
  807. return 0;
  808. }
  809. #endif /* CONFIG_FFV1_ENCODER */
  810. static void clear_state(FFV1Context *f){
  811. int i, si, j;
  812. for(si=0; si<f->slice_count; si++){
  813. FFV1Context *fs= f->slice_context[si];
  814. for(i=0; i<f->plane_count; i++){
  815. PlaneContext *p= &fs->plane[i];
  816. p->interlace_bit_state[0]= 128;
  817. p->interlace_bit_state[1]= 128;
  818. for(j=0; j<p->context_count; j++){
  819. if(fs->ac){
  820. memset(p->state[j], 128, sizeof(uint8_t)*CONTEXT_SIZE);
  821. }else{
  822. p->vlc_state[j].drift= 0;
  823. p->vlc_state[j].error_sum= 4; //FFMAX((RANGE + 32)/64, 2);
  824. p->vlc_state[j].bias= 0;
  825. p->vlc_state[j].count= 1;
  826. }
  827. }
  828. }
  829. }
  830. }
  831. #if CONFIG_FFV1_ENCODER
  832. static int encode_slice(AVCodecContext *c, void *arg){
  833. FFV1Context *fs= *(void**)arg;
  834. FFV1Context *f= fs->avctx->priv_data;
  835. int width = fs->slice_width;
  836. int height= fs->slice_height;
  837. int x= fs->slice_x;
  838. int y= fs->slice_y;
  839. AVFrame * const p= &f->picture;
  840. if(f->colorspace==0){
  841. const int chroma_width = -((-width )>>f->chroma_h_shift);
  842. const int chroma_height= -((-height)>>f->chroma_v_shift);
  843. const int cx= x>>f->chroma_h_shift;
  844. const int cy= y>>f->chroma_v_shift;
  845. encode_plane(fs, p->data[0] + x + y*p->linesize[0], width, height, p->linesize[0], 0);
  846. encode_plane(fs, p->data[1] + cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
  847. encode_plane(fs, p->data[2] + cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1);
  848. }else{
  849. encode_rgb_frame(fs, (uint32_t*)(p->data[0]) + x + y*(p->linesize[0]/4), width, height, p->linesize[0]/4);
  850. }
  851. emms_c();
  852. return 0;
  853. }
  854. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  855. FFV1Context *f = avctx->priv_data;
  856. RangeCoder * const c= &f->slice_context[0]->c;
  857. AVFrame *pict = data;
  858. AVFrame * const p= &f->picture;
  859. int used_count= 0;
  860. uint8_t keystate=128;
  861. uint8_t *buf_p;
  862. int i;
  863. ff_init_range_encoder(c, buf, buf_size);
  864. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  865. *p = *pict;
  866. p->pict_type= FF_I_TYPE;
  867. if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){
  868. put_rac(c, &keystate, 1);
  869. p->key_frame= 1;
  870. write_header(f);
  871. clear_state(f);
  872. }else{
  873. put_rac(c, &keystate, 0);
  874. p->key_frame= 0;
  875. }
  876. if(!f->ac){
  877. used_count += ff_rac_terminate(c);
  878. //printf("pos=%d\n", used_count);
  879. init_put_bits(&f->slice_context[0]->pb, buf + used_count, buf_size - used_count);
  880. }else if (f->ac>1){
  881. int i;
  882. for(i=1; i<256; i++){
  883. c->one_state[i]= f->state_transition[i];
  884. c->zero_state[256-i]= 256-c->one_state[i];
  885. }
  886. }
  887. for(i=1; i<f->slice_count; i++){
  888. FFV1Context *fs= f->slice_context[i];
  889. uint8_t *start= buf + (buf_size-used_count)*i/f->slice_count;
  890. int len= buf_size/f->slice_count;
  891. if(fs->ac){
  892. ff_init_range_encoder(&fs->c, start, len);
  893. }else{
  894. init_put_bits(&fs->pb, start, len);
  895. }
  896. }
  897. avctx->execute(avctx, encode_slice, &f->slice_context[0], NULL, f->slice_count, sizeof(void*));
  898. buf_p=buf;
  899. for(i=0; i<f->slice_count; i++){
  900. FFV1Context *fs= f->slice_context[i];
  901. int bytes;
  902. if(fs->ac){
  903. uint8_t state=128;
  904. put_rac(&fs->c, &state, 0);
  905. bytes= ff_rac_terminate(&fs->c);
  906. }else{
  907. flush_put_bits(&fs->pb); //nicer padding FIXME
  908. bytes= used_count + (put_bits_count(&fs->pb)+7)/8;
  909. used_count= 0;
  910. }
  911. if(i>0){
  912. av_assert0(bytes < buf_size/f->slice_count);
  913. memmove(buf_p, fs->ac ? fs->c.bytestream_start : fs->pb.buf, bytes);
  914. av_assert0(bytes < (1<<24));
  915. AV_WB24(buf_p+bytes, bytes);
  916. bytes+=3;
  917. }
  918. buf_p += bytes;
  919. }
  920. if((avctx->flags&CODEC_FLAG_PASS1) && (f->picture_number&31)==0){
  921. int j;
  922. char *p= avctx->stats_out;
  923. char *end= p + 1024*30;
  924. memset(f->rc_stat, 0, sizeof(f->rc_stat));
  925. for(j=0; j<f->slice_count; j++){
  926. FFV1Context *fs= f->slice_context[j];
  927. for(i=0; i<256; i++){
  928. f->rc_stat[i][0] += fs->rc_stat[i][0];
  929. f->rc_stat[i][1] += fs->rc_stat[i][1];
  930. }
  931. }
  932. for(j=0; j<256; j++){
  933. snprintf(p, end-p, "%"PRIu64" %"PRIu64" ", f->rc_stat[j][0], f->rc_stat[j][1]);
  934. p+= strlen(p);
  935. }
  936. snprintf(p, end-p, "\n");
  937. } else
  938. avctx->stats_out[0] = '\0';
  939. f->picture_number++;
  940. return buf_p-buf;
  941. }
  942. #endif /* CONFIG_FFV1_ENCODER */
  943. static av_cold int common_end(AVCodecContext *avctx){
  944. FFV1Context *s = avctx->priv_data;
  945. int i, j;
  946. for(j=0; j<s->slice_count; j++){
  947. FFV1Context *fs= s->slice_context[j];
  948. for(i=0; i<s->plane_count; i++){
  949. PlaneContext *p= &fs->plane[i];
  950. av_freep(&p->state);
  951. av_freep(&p->vlc_state);
  952. }
  953. av_freep(&fs->sample_buffer);
  954. }
  955. av_freep(&avctx->stats_out);
  956. return 0;
  957. }
  958. static av_always_inline void decode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
  959. PlaneContext * const p= &s->plane[plane_index];
  960. RangeCoder * const c= &s->c;
  961. int x;
  962. int run_count=0;
  963. int run_mode=0;
  964. int run_index= s->run_index;
  965. for(x=0; x<w; x++){
  966. int diff, context, sign;
  967. context= get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
  968. if(context < 0){
  969. context= -context;
  970. sign=1;
  971. }else
  972. sign=0;
  973. av_assert2(context < p->context_count);
  974. if(s->ac){
  975. diff= get_symbol_inline(c, p->state[context], 1);
  976. }else{
  977. if(context == 0 && run_mode==0) run_mode=1;
  978. if(run_mode){
  979. if(run_count==0 && run_mode==1){
  980. if(get_bits1(&s->gb)){
  981. run_count = 1<<ff_log2_run[run_index];
  982. if(x + run_count <= w) run_index++;
  983. }else{
  984. if(ff_log2_run[run_index]) run_count = get_bits(&s->gb, ff_log2_run[run_index]);
  985. else run_count=0;
  986. if(run_index) run_index--;
  987. run_mode=2;
  988. }
  989. }
  990. run_count--;
  991. if(run_count < 0){
  992. run_mode=0;
  993. run_count=0;
  994. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  995. if(diff>=0) diff++;
  996. }else
  997. diff=0;
  998. }else
  999. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  1000. // 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));
  1001. }
  1002. if(sign) diff= -diff;
  1003. sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
  1004. }
  1005. s->run_index= run_index;
  1006. }
  1007. static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  1008. int x, y;
  1009. int_fast16_t *sample[2];
  1010. sample[0]=s->sample_buffer +3;
  1011. sample[1]=s->sample_buffer+w+6+3;
  1012. s->run_index=0;
  1013. memset(s->sample_buffer, 0, 2*(w+6)*sizeof(*s->sample_buffer));
  1014. for(y=0; y<h; y++){
  1015. int_fast16_t *temp= sample[0]; //FIXME try a normal buffer
  1016. sample[0]= sample[1];
  1017. sample[1]= temp;
  1018. sample[1][-1]= sample[0][0 ];
  1019. sample[0][ w]= sample[0][w-1];
  1020. //{START_TIMER
  1021. if(s->avctx->bits_per_raw_sample <= 8){
  1022. decode_line(s, w, sample, plane_index, 8);
  1023. for(x=0; x<w; x++){
  1024. src[x + stride*y]= sample[1][x];
  1025. }
  1026. }else{
  1027. decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
  1028. for(x=0; x<w; x++){
  1029. ((uint16_t*)(src + stride*y))[x]= sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
  1030. }
  1031. }
  1032. //STOP_TIMER("decode-line")}
  1033. }
  1034. }
  1035. static void decode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  1036. int x, y, p;
  1037. int_fast16_t *sample[3][2];
  1038. for(x=0; x<3; x++){
  1039. sample[x][0] = s->sample_buffer + x*2 *(w+6) + 3;
  1040. sample[x][1] = s->sample_buffer + (x*2+1)*(w+6) + 3;
  1041. }
  1042. s->run_index=0;
  1043. memset(s->sample_buffer, 0, 6*(w+6)*sizeof(*s->sample_buffer));
  1044. for(y=0; y<h; y++){
  1045. for(p=0; p<3; p++){
  1046. int_fast16_t *temp= sample[p][0]; //FIXME try a normal buffer
  1047. sample[p][0]= sample[p][1];
  1048. sample[p][1]= temp;
  1049. sample[p][1][-1]= sample[p][0][0 ];
  1050. sample[p][0][ w]= sample[p][0][w-1];
  1051. decode_line(s, w, sample[p], FFMIN(p, 1), 9);
  1052. }
  1053. for(x=0; x<w; x++){
  1054. int g= sample[0][1][x];
  1055. int b= sample[1][1][x];
  1056. int r= sample[2][1][x];
  1057. // assert(g>=0 && b>=0 && r>=0);
  1058. // assert(g<256 && b<512 && r<512);
  1059. b -= 0x100;
  1060. r -= 0x100;
  1061. g -= (b + r)>>2;
  1062. b += g;
  1063. r += g;
  1064. src[x + stride*y]= b + (g<<8) + (r<<16) + (0xFF<<24);
  1065. }
  1066. }
  1067. }
  1068. static int decode_slice(AVCodecContext *c, void *arg){
  1069. FFV1Context *fs= *(void**)arg;
  1070. FFV1Context *f= fs->avctx->priv_data;
  1071. int width = fs->slice_width;
  1072. int height= fs->slice_height;
  1073. int x= fs->slice_x;
  1074. int y= fs->slice_y;
  1075. AVFrame * const p= &f->picture;
  1076. av_assert1(width && height);
  1077. if(f->colorspace==0){
  1078. const int chroma_width = -((-width )>>f->chroma_h_shift);
  1079. const int chroma_height= -((-height)>>f->chroma_v_shift);
  1080. const int cx= x>>f->chroma_h_shift;
  1081. const int cy= y>>f->chroma_v_shift;
  1082. decode_plane(fs, p->data[0] + x + y*p->linesize[0], width, height, p->linesize[0], 0);
  1083. decode_plane(fs, p->data[1] + cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
  1084. decode_plane(fs, p->data[2] + cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[2], 1);
  1085. }else{
  1086. decode_rgb_frame(fs, (uint32_t*)p->data[0] + x + y*(p->linesize[0]/4), width, height, p->linesize[0]/4);
  1087. }
  1088. emms_c();
  1089. return 0;
  1090. }
  1091. static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){
  1092. int v;
  1093. int i=0;
  1094. uint8_t state[CONTEXT_SIZE];
  1095. memset(state, 128, sizeof(state));
  1096. for(v=0; i<128 ; v++){
  1097. int len= get_symbol(c, state, 0) + 1;
  1098. if(len + i > 128) return -1;
  1099. while(len--){
  1100. quant_table[i] = scale*v;
  1101. i++;
  1102. //printf("%2d ",v);
  1103. //if(i%16==0) printf("\n");
  1104. }
  1105. }
  1106. for(i=1; i<128; i++){
  1107. quant_table[256-i]= -quant_table[i];
  1108. }
  1109. quant_table[128]= -quant_table[127];
  1110. return 2*v - 1;
  1111. }
  1112. static int read_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256]){
  1113. int i;
  1114. int context_count=1;
  1115. for(i=0; i<5; i++){
  1116. context_count*= read_quant_table(c, quant_table[i], context_count);
  1117. if(context_count > 32768U){
  1118. return -1;
  1119. }
  1120. }
  1121. return (context_count+1)/2;
  1122. }
  1123. static int read_extra_header(FFV1Context *f){
  1124. RangeCoder * const c= &f->c;
  1125. uint8_t state[CONTEXT_SIZE];
  1126. int i;
  1127. memset(state, 128, sizeof(state));
  1128. ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
  1129. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  1130. f->version= get_symbol(c, state, 0);
  1131. f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
  1132. if(f->ac>1){
  1133. for(i=1; i<256; i++){
  1134. f->state_transition[i]= get_symbol(c, state, 1) + c->one_state[i];
  1135. }
  1136. }
  1137. f->colorspace= get_symbol(c, state, 0); //YUV cs type
  1138. f->avctx->bits_per_raw_sample= get_symbol(c, state, 0);
  1139. get_rac(c, state); //no chroma = false
  1140. f->chroma_h_shift= get_symbol(c, state, 0);
  1141. f->chroma_v_shift= get_symbol(c, state, 0);
  1142. get_rac(c, state); //transparency plane
  1143. f->plane_count= 2;
  1144. f->num_h_slices= 1 + get_symbol(c, state, 0);
  1145. f->num_v_slices= 1 + get_symbol(c, state, 0);
  1146. if(f->num_h_slices > (unsigned)f->width || f->num_v_slices > (unsigned)f->height){
  1147. av_log(f->avctx, AV_LOG_ERROR, "too many slices\n");
  1148. return -1;
  1149. }
  1150. f->quant_table_count= get_symbol(c, state, 0);
  1151. if(f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
  1152. return -1;
  1153. for(i=0; i<f->quant_table_count; i++){
  1154. if((f->context_count[i]= read_quant_tables(c, f->quant_tables[i])) < 0){
  1155. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  1156. return -1;
  1157. }
  1158. }
  1159. return 0;
  1160. }
  1161. static int read_header(FFV1Context *f){
  1162. uint8_t state[CONTEXT_SIZE];
  1163. int i, j, context_count;
  1164. RangeCoder * const c= &f->slice_context[0]->c;
  1165. memset(state, 128, sizeof(state));
  1166. if(f->version < 2){
  1167. f->version= get_symbol(c, state, 0);
  1168. f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
  1169. if(f->ac>1){
  1170. for(i=1; i<256; i++){
  1171. f->state_transition[i]= get_symbol(c, state, 1) + c->one_state[i];
  1172. }
  1173. }
  1174. f->colorspace= get_symbol(c, state, 0); //YUV cs type
  1175. if(f->version>0)
  1176. f->avctx->bits_per_raw_sample= get_symbol(c, state, 0);
  1177. get_rac(c, state); //no chroma = false
  1178. f->chroma_h_shift= get_symbol(c, state, 0);
  1179. f->chroma_v_shift= get_symbol(c, state, 0);
  1180. get_rac(c, state); //transparency plane
  1181. f->plane_count= 2;
  1182. }
  1183. if(f->colorspace==0){
  1184. if(f->avctx->bits_per_raw_sample<=8){
  1185. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  1186. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
  1187. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
  1188. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
  1189. case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
  1190. case 0x22: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
  1191. default:
  1192. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  1193. return -1;
  1194. }
  1195. }else{
  1196. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  1197. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P16; break;
  1198. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P16; break;
  1199. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P16; break;
  1200. default:
  1201. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  1202. return -1;
  1203. }
  1204. }
  1205. }else if(f->colorspace==1){
  1206. if(f->chroma_h_shift || f->chroma_v_shift){
  1207. av_log(f->avctx, AV_LOG_ERROR, "chroma subsampling not supported in this colorspace\n");
  1208. return -1;
  1209. }
  1210. f->avctx->pix_fmt= PIX_FMT_RGB32;
  1211. }else{
  1212. av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
  1213. return -1;
  1214. }
  1215. //printf("%d %d %d\n", f->chroma_h_shift, f->chroma_v_shift,f->avctx->pix_fmt);
  1216. if(f->version < 2){
  1217. context_count= read_quant_tables(c, f->quant_table);
  1218. if(context_count < 0){
  1219. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  1220. return -1;
  1221. }
  1222. }else{
  1223. f->slice_count= get_symbol(c, state, 0);
  1224. if(f->slice_count > (unsigned)MAX_SLICES)
  1225. return -1;
  1226. }
  1227. for(j=0; j<f->slice_count; j++){
  1228. FFV1Context *fs= f->slice_context[j];
  1229. fs->ac= f->ac;
  1230. if(f->version >= 2){
  1231. fs->slice_x = get_symbol(c, state, 0) *f->width ;
  1232. fs->slice_y = get_symbol(c, state, 0) *f->height;
  1233. fs->slice_width =(get_symbol(c, state, 0)+1)*f->width + fs->slice_x;
  1234. fs->slice_height=(get_symbol(c, state, 0)+1)*f->height + fs->slice_y;
  1235. fs->slice_x /= f->num_h_slices;
  1236. fs->slice_y /= f->num_v_slices;
  1237. fs->slice_width = fs->slice_width /f->num_h_slices - fs->slice_x;
  1238. fs->slice_height = fs->slice_height/f->num_v_slices - fs->slice_y;
  1239. if((unsigned)fs->slice_width > f->width || (unsigned)fs->slice_height > f->height)
  1240. return -1;
  1241. if( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
  1242. || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
  1243. return -1;
  1244. }
  1245. for(i=0; i<f->plane_count; i++){
  1246. PlaneContext * const p= &fs->plane[i];
  1247. if(f->version >= 2){
  1248. int idx=get_symbol(c, state, 0);
  1249. if(idx > (unsigned)f->quant_table_count){
  1250. av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
  1251. return -1;
  1252. }
  1253. memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
  1254. context_count= f->context_count[idx];
  1255. }else{
  1256. memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
  1257. }
  1258. if(p->context_count < context_count){
  1259. av_freep(&p->state);
  1260. av_freep(&p->vlc_state);
  1261. }
  1262. p->context_count= context_count;
  1263. }
  1264. }
  1265. return 0;
  1266. }
  1267. static av_cold int decode_init(AVCodecContext *avctx)
  1268. {
  1269. FFV1Context *f = avctx->priv_data;
  1270. common_init(avctx);
  1271. if(avctx->extradata && read_extra_header(f) < 0)
  1272. return -1;
  1273. if(init_slice_contexts(f) < 0)
  1274. return -1;
  1275. return 0;
  1276. }
  1277. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt){
  1278. const uint8_t *buf = avpkt->data;
  1279. int buf_size = avpkt->size;
  1280. FFV1Context *f = avctx->priv_data;
  1281. RangeCoder * const c= &f->slice_context[0]->c;
  1282. AVFrame * const p= &f->picture;
  1283. int bytes_read, i;
  1284. uint8_t keystate= 128;
  1285. const uint8_t *buf_p;
  1286. AVFrame *picture = data;
  1287. ff_init_range_decoder(c, buf, buf_size);
  1288. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  1289. p->pict_type= FF_I_TYPE; //FIXME I vs. P
  1290. if(get_rac(c, &keystate)){
  1291. p->key_frame= 1;
  1292. if(read_header(f) < 0)
  1293. return -1;
  1294. if(init_slice_state(f) < 0)
  1295. return -1;
  1296. clear_state(f);
  1297. }else{
  1298. p->key_frame= 0;
  1299. }
  1300. if(f->ac>1){
  1301. int i;
  1302. for(i=1; i<256; i++){
  1303. c->one_state[i]= f->state_transition[i];
  1304. c->zero_state[256-i]= 256-c->one_state[i];
  1305. }
  1306. }
  1307. p->reference= 0;
  1308. if(avctx->get_buffer(avctx, p) < 0){
  1309. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1310. return -1;
  1311. }
  1312. if(avctx->debug&FF_DEBUG_PICT_INFO)
  1313. av_log(avctx, AV_LOG_ERROR, "keyframe:%d coder:%d\n", p->key_frame, f->ac);
  1314. if(!f->ac){
  1315. bytes_read = c->bytestream - c->bytestream_start - 1;
  1316. if(bytes_read ==0) av_log(avctx, AV_LOG_ERROR, "error at end of AC stream\n"); //FIXME
  1317. //printf("pos=%d\n", bytes_read);
  1318. init_get_bits(&f->slice_context[0]->gb, buf + bytes_read, buf_size - bytes_read);
  1319. } else {
  1320. bytes_read = 0; /* avoid warning */
  1321. }
  1322. buf_p= buf + buf_size;
  1323. for(i=f->slice_count-1; i>0; i--){
  1324. FFV1Context *fs= f->slice_context[i];
  1325. int v= AV_RB24(buf_p-3)+3;
  1326. if(buf_p - buf <= v){
  1327. av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
  1328. return -1;
  1329. }
  1330. buf_p -= v;
  1331. if(fs->ac){
  1332. ff_init_range_decoder(&fs->c, buf_p, v);
  1333. }else{
  1334. init_get_bits(&fs->gb, buf_p, v);
  1335. }
  1336. }
  1337. avctx->execute(avctx, decode_slice, &f->slice_context[0], NULL, f->slice_count, sizeof(void*));
  1338. f->picture_number++;
  1339. *picture= *p;
  1340. avctx->release_buffer(avctx, p); //FIXME
  1341. *data_size = sizeof(AVFrame);
  1342. return buf_size;
  1343. }
  1344. AVCodec ffv1_decoder = {
  1345. "ffv1",
  1346. AVMEDIA_TYPE_VIDEO,
  1347. CODEC_ID_FFV1,
  1348. sizeof(FFV1Context),
  1349. decode_init,
  1350. NULL,
  1351. common_end,
  1352. decode_frame,
  1353. CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  1354. NULL,
  1355. .long_name= NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  1356. };
  1357. #if CONFIG_FFV1_ENCODER
  1358. AVCodec ffv1_encoder = {
  1359. "ffv1",
  1360. AVMEDIA_TYPE_VIDEO,
  1361. CODEC_ID_FFV1,
  1362. sizeof(FFV1Context),
  1363. encode_init,
  1364. encode_frame,
  1365. common_end,
  1366. .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},
  1367. .long_name= NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  1368. };
  1369. #endif