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.

1216 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_quant_tables(RangeCoder *c, int16_t quant_table[5][256]){
  519. int i;
  520. for(i=0; i<5; i++)
  521. write_quant_table(c, quant_table[i]);
  522. }
  523. static void write_header(FFV1Context *f){
  524. uint8_t state[CONTEXT_SIZE];
  525. int i;
  526. RangeCoder * const c= &f->c;
  527. memset(state, 128, sizeof(state));
  528. put_symbol(c, state, f->version, 0);
  529. put_symbol(c, state, f->ac, 0);
  530. if(f->ac>1){
  531. for(i=1; i<256; i++){
  532. f->state_transition[i]=ver2_state[i];
  533. put_symbol(c, state, ver2_state[i] - c->one_state[i], 1);
  534. }
  535. }
  536. put_symbol(c, state, f->colorspace, 0); //YUV cs type
  537. if(f->version>0)
  538. put_symbol(c, state, f->avctx->bits_per_raw_sample, 0);
  539. put_rac(c, state, 1); //chroma planes
  540. put_symbol(c, state, f->chroma_h_shift, 0);
  541. put_symbol(c, state, f->chroma_v_shift, 0);
  542. put_rac(c, state, 0); //no transparency plane
  543. write_quant_tables(c, f->quant_table);
  544. }
  545. #endif /* CONFIG_FFV1_ENCODER */
  546. static av_cold int common_init(AVCodecContext *avctx){
  547. FFV1Context *s = avctx->priv_data;
  548. s->avctx= avctx;
  549. s->flags= avctx->flags;
  550. dsputil_init(&s->dsp, avctx);
  551. s->width = avctx->width;
  552. s->height= avctx->height;
  553. assert(s->width && s->height);
  554. s->sample_buffer = av_malloc(6 * (s->width+6) * sizeof(*s->sample_buffer));
  555. if (!s->sample_buffer)
  556. return AVERROR(ENOMEM);
  557. return 0;
  558. }
  559. #if CONFIG_FFV1_ENCODER
  560. static av_cold int encode_init(AVCodecContext *avctx)
  561. {
  562. FFV1Context *s = avctx->priv_data;
  563. int i;
  564. common_init(avctx);
  565. s->version=0;
  566. s->ac= avctx->coder_type ? 2:0;
  567. s->plane_count=2;
  568. for(i=0; i<256; i++){
  569. if(avctx->bits_per_raw_sample <=8){
  570. s->quant_table[0][i]= quant11[i];
  571. s->quant_table[1][i]= 11*quant11[i];
  572. if(avctx->context_model==0){
  573. s->quant_table[2][i]= 11*11*quant11[i];
  574. s->quant_table[3][i]=
  575. s->quant_table[4][i]=0;
  576. }else{
  577. s->quant_table[2][i]= 11*11*quant5 [i];
  578. s->quant_table[3][i]= 5*11*11*quant5 [i];
  579. s->quant_table[4][i]= 5*5*11*11*quant5 [i];
  580. }
  581. }else{
  582. s->quant_table[0][i]= quant9_10bit[i];
  583. s->quant_table[1][i]= 11*quant9_10bit[i];
  584. if(avctx->context_model==0){
  585. s->quant_table[2][i]= 11*11*quant9_10bit[i];
  586. s->quant_table[3][i]=
  587. s->quant_table[4][i]=0;
  588. }else{
  589. s->quant_table[2][i]= 11*11*quant5_10bit[i];
  590. s->quant_table[3][i]= 5*11*11*quant5_10bit[i];
  591. s->quant_table[4][i]= 5*5*11*11*quant5_10bit[i];
  592. }
  593. }
  594. }
  595. for(i=0; i<s->plane_count; i++){
  596. PlaneContext * const p= &s->plane[i];
  597. if(avctx->context_model==0){
  598. p->context_count= (11*11*11+1)/2;
  599. }else{
  600. p->context_count= (11*11*5*5*5+1)/2;
  601. }
  602. if(s->ac){
  603. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  604. }else{
  605. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  606. }
  607. }
  608. avctx->coded_frame= &s->picture;
  609. switch(avctx->pix_fmt){
  610. case PIX_FMT_YUV444P16:
  611. case PIX_FMT_YUV422P16:
  612. case PIX_FMT_YUV420P16:
  613. if(avctx->bits_per_raw_sample <=8){
  614. av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
  615. return -1;
  616. }
  617. if(!s->ac){
  618. av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample of more than 8 needs -coder 1 currently\n");
  619. return -1;
  620. }
  621. s->version= 1;
  622. case PIX_FMT_YUV444P:
  623. case PIX_FMT_YUV422P:
  624. case PIX_FMT_YUV420P:
  625. case PIX_FMT_YUV411P:
  626. case PIX_FMT_YUV410P:
  627. s->colorspace= 0;
  628. break;
  629. case PIX_FMT_RGB32:
  630. s->colorspace= 1;
  631. break;
  632. default:
  633. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  634. return -1;
  635. }
  636. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  637. s->picture_number=0;
  638. return 0;
  639. }
  640. #endif /* CONFIG_FFV1_ENCODER */
  641. static void clear_state(FFV1Context *f){
  642. int i, j;
  643. for(i=0; i<f->plane_count; i++){
  644. PlaneContext *p= &f->plane[i];
  645. p->interlace_bit_state[0]= 128;
  646. p->interlace_bit_state[1]= 128;
  647. for(j=0; j<p->context_count; j++){
  648. if(f->ac){
  649. memset(p->state[j], 128, sizeof(uint8_t)*CONTEXT_SIZE);
  650. }else{
  651. p->vlc_state[j].drift= 0;
  652. p->vlc_state[j].error_sum= 4; //FFMAX((RANGE + 32)/64, 2);
  653. p->vlc_state[j].bias= 0;
  654. p->vlc_state[j].count= 1;
  655. }
  656. }
  657. }
  658. }
  659. #if CONFIG_FFV1_ENCODER
  660. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  661. FFV1Context *f = avctx->priv_data;
  662. RangeCoder * const c= &f->c;
  663. AVFrame *pict = data;
  664. const int width= f->width;
  665. const int height= f->height;
  666. AVFrame * const p= &f->picture;
  667. int used_count= 0;
  668. uint8_t keystate=128;
  669. ff_init_range_encoder(c, buf, buf_size);
  670. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  671. *p = *pict;
  672. p->pict_type= FF_I_TYPE;
  673. if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){
  674. put_rac(c, &keystate, 1);
  675. p->key_frame= 1;
  676. write_header(f);
  677. clear_state(f);
  678. }else{
  679. put_rac(c, &keystate, 0);
  680. p->key_frame= 0;
  681. }
  682. if(!f->ac){
  683. used_count += ff_rac_terminate(c);
  684. //printf("pos=%d\n", used_count);
  685. init_put_bits(&f->pb, buf + used_count, buf_size - used_count);
  686. }else if (f->ac>1){
  687. int i;
  688. for(i=1; i<256; i++){
  689. c->one_state[i]= f->state_transition[i];
  690. c->zero_state[256-i]= 256-c->one_state[i];
  691. }
  692. }
  693. if(f->colorspace==0){
  694. const int chroma_width = -((-width )>>f->chroma_h_shift);
  695. const int chroma_height= -((-height)>>f->chroma_v_shift);
  696. encode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  697. encode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  698. encode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
  699. }else{
  700. encode_rgb_frame(f, (uint32_t*)(p->data[0]), width, height, p->linesize[0]/4);
  701. }
  702. emms_c();
  703. f->picture_number++;
  704. if(f->ac){
  705. return ff_rac_terminate(c);
  706. }else{
  707. flush_put_bits(&f->pb); //nicer padding FIXME
  708. return used_count + (put_bits_count(&f->pb)+7)/8;
  709. }
  710. }
  711. #endif /* CONFIG_FFV1_ENCODER */
  712. static av_cold int common_end(AVCodecContext *avctx){
  713. FFV1Context *s = avctx->priv_data;
  714. int i;
  715. for(i=0; i<s->plane_count; i++){
  716. PlaneContext *p= &s->plane[i];
  717. av_freep(&p->state);
  718. av_freep(&p->vlc_state);
  719. }
  720. av_freep(&s->sample_buffer);
  721. return 0;
  722. }
  723. static av_always_inline void decode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
  724. PlaneContext * const p= &s->plane[plane_index];
  725. RangeCoder * const c= &s->c;
  726. int x;
  727. int run_count=0;
  728. int run_mode=0;
  729. int run_index= s->run_index;
  730. for(x=0; x<w; x++){
  731. int diff, context, sign;
  732. context= get_context(s, sample[1] + x, sample[0] + x, sample[1] + x);
  733. if(context < 0){
  734. context= -context;
  735. sign=1;
  736. }else
  737. sign=0;
  738. if(s->ac){
  739. diff= get_symbol_inline(c, p->state[context], 1);
  740. }else{
  741. if(context == 0 && run_mode==0) run_mode=1;
  742. if(run_mode){
  743. if(run_count==0 && run_mode==1){
  744. if(get_bits1(&s->gb)){
  745. run_count = 1<<ff_log2_run[run_index];
  746. if(x + run_count <= w) run_index++;
  747. }else{
  748. if(ff_log2_run[run_index]) run_count = get_bits(&s->gb, ff_log2_run[run_index]);
  749. else run_count=0;
  750. if(run_index) run_index--;
  751. run_mode=2;
  752. }
  753. }
  754. run_count--;
  755. if(run_count < 0){
  756. run_mode=0;
  757. run_count=0;
  758. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  759. if(diff>=0) diff++;
  760. }else
  761. diff=0;
  762. }else
  763. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  764. // 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));
  765. }
  766. if(sign) diff= -diff;
  767. sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
  768. }
  769. s->run_index= run_index;
  770. }
  771. static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  772. int x, y;
  773. int_fast16_t *sample[2];
  774. sample[0]=s->sample_buffer +3;
  775. sample[1]=s->sample_buffer+w+6+3;
  776. s->run_index=0;
  777. memset(s->sample_buffer, 0, 2*(w+6)*sizeof(*s->sample_buffer));
  778. for(y=0; y<h; y++){
  779. int_fast16_t *temp= sample[0]; //FIXME try a normal buffer
  780. sample[0]= sample[1];
  781. sample[1]= temp;
  782. sample[1][-1]= sample[0][0 ];
  783. sample[0][ w]= sample[0][w-1];
  784. //{START_TIMER
  785. if(s->avctx->bits_per_raw_sample <= 8){
  786. decode_line(s, w, sample, plane_index, 8);
  787. for(x=0; x<w; x++){
  788. src[x + stride*y]= sample[1][x];
  789. }
  790. }else{
  791. decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
  792. for(x=0; x<w; x++){
  793. ((uint16_t*)(src + stride*y))[x]= sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
  794. }
  795. }
  796. //STOP_TIMER("decode-line")}
  797. }
  798. }
  799. static void decode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  800. int x, y, p;
  801. int_fast16_t *sample[3][2];
  802. for(x=0; x<3; x++){
  803. sample[x][0] = s->sample_buffer + x*2 *(w+6) + 3;
  804. sample[x][1] = s->sample_buffer + (x*2+1)*(w+6) + 3;
  805. }
  806. s->run_index=0;
  807. memset(s->sample_buffer, 0, 6*(w+6)*sizeof(*s->sample_buffer));
  808. for(y=0; y<h; y++){
  809. for(p=0; p<3; p++){
  810. int_fast16_t *temp= sample[p][0]; //FIXME try a normal buffer
  811. sample[p][0]= sample[p][1];
  812. sample[p][1]= temp;
  813. sample[p][1][-1]= sample[p][0][0 ];
  814. sample[p][0][ w]= sample[p][0][w-1];
  815. decode_line(s, w, sample[p], FFMIN(p, 1), 9);
  816. }
  817. for(x=0; x<w; x++){
  818. int g= sample[0][1][x];
  819. int b= sample[1][1][x];
  820. int r= sample[2][1][x];
  821. // assert(g>=0 && b>=0 && r>=0);
  822. // assert(g<256 && b<512 && r<512);
  823. b -= 0x100;
  824. r -= 0x100;
  825. g -= (b + r)>>2;
  826. b += g;
  827. r += g;
  828. src[x + stride*y]= b + (g<<8) + (r<<16) + (0xFF<<24);
  829. }
  830. }
  831. }
  832. static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){
  833. int v;
  834. int i=0;
  835. uint8_t state[CONTEXT_SIZE];
  836. memset(state, 128, sizeof(state));
  837. for(v=0; i<128 ; v++){
  838. int len= get_symbol(c, state, 0) + 1;
  839. if(len + i > 128) return -1;
  840. while(len--){
  841. quant_table[i] = scale*v;
  842. i++;
  843. //printf("%2d ",v);
  844. //if(i%16==0) printf("\n");
  845. }
  846. }
  847. for(i=1; i<128; i++){
  848. quant_table[256-i]= -quant_table[i];
  849. }
  850. quant_table[128]= -quant_table[127];
  851. return 2*v - 1;
  852. }
  853. static int read_quant_tables(RangeCoder *c, int16_t quant_table[5][256]){
  854. int i;
  855. int context_count=1;
  856. for(i=0; i<5; i++){
  857. context_count*= read_quant_table(c, quant_table[i], context_count);
  858. if(context_count > 32768U){
  859. return -1;
  860. }
  861. }
  862. return (context_count+1)/2;
  863. }
  864. static int read_header(FFV1Context *f){
  865. uint8_t state[CONTEXT_SIZE];
  866. int i, context_count;
  867. RangeCoder * const c= &f->c;
  868. memset(state, 128, sizeof(state));
  869. f->version= get_symbol(c, state, 0);
  870. f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
  871. if(f->ac>1){
  872. for(i=1; i<256; i++){
  873. f->state_transition[i]= get_symbol(c, state, 1) + c->one_state[i];
  874. }
  875. }
  876. f->colorspace= get_symbol(c, state, 0); //YUV cs type
  877. if(f->version>0)
  878. f->avctx->bits_per_raw_sample= get_symbol(c, state, 0);
  879. get_rac(c, state); //no chroma = false
  880. f->chroma_h_shift= get_symbol(c, state, 0);
  881. f->chroma_v_shift= get_symbol(c, state, 0);
  882. get_rac(c, state); //transparency plane
  883. f->plane_count= 2;
  884. if(f->colorspace==0){
  885. if(f->avctx->bits_per_raw_sample<=8){
  886. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  887. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
  888. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
  889. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
  890. case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
  891. case 0x22: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
  892. default:
  893. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  894. return -1;
  895. }
  896. }else{
  897. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  898. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P16; break;
  899. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P16; break;
  900. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P16; break;
  901. default:
  902. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  903. return -1;
  904. }
  905. }
  906. }else if(f->colorspace==1){
  907. if(f->chroma_h_shift || f->chroma_v_shift){
  908. av_log(f->avctx, AV_LOG_ERROR, "chroma subsampling not supported in this colorspace\n");
  909. return -1;
  910. }
  911. f->avctx->pix_fmt= PIX_FMT_RGB32;
  912. }else{
  913. av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
  914. return -1;
  915. }
  916. //printf("%d %d %d\n", f->chroma_h_shift, f->chroma_v_shift,f->avctx->pix_fmt);
  917. context_count= read_quant_tables(c, f->quant_table);
  918. if(context_count < 0){
  919. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  920. return -1;
  921. }
  922. for(i=0; i<f->plane_count; i++){
  923. PlaneContext * const p= &f->plane[i];
  924. p->context_count= context_count;
  925. if(f->ac){
  926. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  927. }else{
  928. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  929. }
  930. }
  931. return 0;
  932. }
  933. static av_cold int decode_init(AVCodecContext *avctx)
  934. {
  935. // FFV1Context *s = avctx->priv_data;
  936. common_init(avctx);
  937. return 0;
  938. }
  939. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt){
  940. const uint8_t *buf = avpkt->data;
  941. int buf_size = avpkt->size;
  942. FFV1Context *f = avctx->priv_data;
  943. RangeCoder * const c= &f->c;
  944. const int width= f->width;
  945. const int height= f->height;
  946. AVFrame * const p= &f->picture;
  947. int bytes_read;
  948. uint8_t keystate= 128;
  949. AVFrame *picture = data;
  950. ff_init_range_decoder(c, buf, buf_size);
  951. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  952. p->pict_type= FF_I_TYPE; //FIXME I vs. P
  953. if(get_rac(c, &keystate)){
  954. p->key_frame= 1;
  955. if(read_header(f) < 0)
  956. return -1;
  957. clear_state(f);
  958. }else{
  959. p->key_frame= 0;
  960. }
  961. if(f->ac>1){
  962. int i;
  963. for(i=1; i<256; i++){
  964. c->one_state[i]= f->state_transition[i];
  965. c->zero_state[256-i]= 256-c->one_state[i];
  966. }
  967. }
  968. if(!f->plane[0].state && !f->plane[0].vlc_state)
  969. return -1;
  970. p->reference= 0;
  971. if(avctx->get_buffer(avctx, p) < 0){
  972. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  973. return -1;
  974. }
  975. if(avctx->debug&FF_DEBUG_PICT_INFO)
  976. av_log(avctx, AV_LOG_ERROR, "keyframe:%d coder:%d\n", p->key_frame, f->ac);
  977. if(!f->ac){
  978. bytes_read = c->bytestream - c->bytestream_start - 1;
  979. if(bytes_read ==0) av_log(avctx, AV_LOG_ERROR, "error at end of AC stream\n"); //FIXME
  980. //printf("pos=%d\n", bytes_read);
  981. init_get_bits(&f->gb, buf + bytes_read, buf_size - bytes_read);
  982. } else {
  983. bytes_read = 0; /* avoid warning */
  984. }
  985. if(f->colorspace==0){
  986. const int chroma_width = -((-width )>>f->chroma_h_shift);
  987. const int chroma_height= -((-height)>>f->chroma_v_shift);
  988. decode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  989. decode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  990. decode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
  991. }else{
  992. decode_rgb_frame(f, (uint32_t*)p->data[0], width, height, p->linesize[0]/4);
  993. }
  994. emms_c();
  995. f->picture_number++;
  996. *picture= *p;
  997. avctx->release_buffer(avctx, p); //FIXME
  998. *data_size = sizeof(AVFrame);
  999. if(f->ac){
  1000. bytes_read= c->bytestream - c->bytestream_start - 1;
  1001. if(bytes_read ==0) av_log(f->avctx, AV_LOG_ERROR, "error at end of frame\n");
  1002. }else{
  1003. bytes_read+= (get_bits_count(&f->gb)+7)/8;
  1004. }
  1005. return bytes_read;
  1006. }
  1007. AVCodec ffv1_decoder = {
  1008. "ffv1",
  1009. AVMEDIA_TYPE_VIDEO,
  1010. CODEC_ID_FFV1,
  1011. sizeof(FFV1Context),
  1012. decode_init,
  1013. NULL,
  1014. common_end,
  1015. decode_frame,
  1016. CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  1017. NULL,
  1018. .long_name= NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  1019. };
  1020. #if CONFIG_FFV1_ENCODER
  1021. AVCodec ffv1_encoder = {
  1022. "ffv1",
  1023. AVMEDIA_TYPE_VIDEO,
  1024. CODEC_ID_FFV1,
  1025. sizeof(FFV1Context),
  1026. encode_init,
  1027. encode_frame,
  1028. common_end,
  1029. .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},
  1030. .long_name= NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  1031. };
  1032. #endif