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.

1203 lines
37KB

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