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.

1198 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. DSPContext dsp;
  229. }FFV1Context;
  230. static av_always_inline int fold(int diff, int bits){
  231. if(bits==8)
  232. diff= (int8_t)diff;
  233. else{
  234. diff+= 1<<(bits-1);
  235. diff&=(1<<bits)-1;
  236. diff-= 1<<(bits-1);
  237. }
  238. return diff;
  239. }
  240. static inline int predict(int_fast16_t *src, int_fast16_t *last){
  241. const int LT= last[-1];
  242. const int T= last[ 0];
  243. const int L = src[-1];
  244. return mid_pred(L, L + T - LT, T);
  245. }
  246. static inline int get_context(FFV1Context *f, int_fast16_t *src, int_fast16_t *last, int_fast16_t *last2){
  247. const int LT= last[-1];
  248. const int T= last[ 0];
  249. const int RT= last[ 1];
  250. const int L = src[-1];
  251. if(f->quant_table[3][127]){
  252. const int TT= last2[0];
  253. const int LL= src[-2];
  254. return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF]
  255. +f->quant_table[3][(LL-L) & 0xFF] + f->quant_table[4][(TT-T) & 0xFF];
  256. }else
  257. return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF];
  258. }
  259. static inline void put_symbol_inline(RangeCoder *c, uint8_t *state, int v, int is_signed){
  260. int i;
  261. if(v){
  262. const int a= FFABS(v);
  263. const int e= av_log2(a);
  264. put_rac(c, state+0, 0);
  265. if(e<=9){
  266. for(i=0; i<e; i++){
  267. put_rac(c, state+1+i, 1); //1..10
  268. }
  269. put_rac(c, state+1+i, 0);
  270. for(i=e-1; i>=0; i--){
  271. put_rac(c, state+22+i, (a>>i)&1); //22..31
  272. }
  273. if(is_signed)
  274. put_rac(c, state+11 + e, v < 0); //11..21
  275. }else{
  276. for(i=0; i<e; i++){
  277. put_rac(c, state+1+FFMIN(i,9), 1); //1..10
  278. }
  279. put_rac(c, state+1+9, 0);
  280. for(i=e-1; i>=0; i--){
  281. put_rac(c, state+22+FFMIN(i,9), (a>>i)&1); //22..31
  282. }
  283. if(is_signed)
  284. put_rac(c, state+11 + 10, v < 0); //11..21
  285. }
  286. }else{
  287. put_rac(c, state+0, 1);
  288. }
  289. }
  290. static void av_noinline put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
  291. put_symbol_inline(c, state, v, is_signed);
  292. }
  293. static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state, int is_signed){
  294. if(get_rac(c, state+0))
  295. return 0;
  296. else{
  297. int i, e, a;
  298. e= 0;
  299. while(get_rac(c, state+1 + FFMIN(e,9))){ //1..10
  300. e++;
  301. }
  302. a= 1;
  303. for(i=e-1; i>=0; i--){
  304. a += a + get_rac(c, state+22 + FFMIN(i,9)); //22..31
  305. }
  306. e= -(is_signed && get_rac(c, state+11 + FFMIN(e, 10))); //11..21
  307. return (a^e)-e;
  308. }
  309. }
  310. static int av_noinline get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
  311. return get_symbol_inline(c, state, is_signed);
  312. }
  313. static inline void update_vlc_state(VlcState * const state, const int v){
  314. int drift= state->drift;
  315. int count= state->count;
  316. state->error_sum += FFABS(v);
  317. drift += v;
  318. if(count == 128){ //FIXME variable
  319. count >>= 1;
  320. drift >>= 1;
  321. state->error_sum >>= 1;
  322. }
  323. count++;
  324. if(drift <= -count){
  325. if(state->bias > -128) state->bias--;
  326. drift += count;
  327. if(drift <= -count)
  328. drift= -count + 1;
  329. }else if(drift > 0){
  330. if(state->bias < 127) state->bias++;
  331. drift -= count;
  332. if(drift > 0)
  333. drift= 0;
  334. }
  335. state->drift= drift;
  336. state->count= count;
  337. }
  338. static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int v, int bits){
  339. int i, k, code;
  340. //printf("final: %d ", v);
  341. v = fold(v - state->bias, bits);
  342. i= state->count;
  343. k=0;
  344. while(i < state->error_sum){ //FIXME optimize
  345. k++;
  346. i += i;
  347. }
  348. assert(k<=8);
  349. #if 0 // JPEG LS
  350. if(k==0 && 2*state->drift <= - state->count) code= v ^ (-1);
  351. else code= v;
  352. #else
  353. code= v ^ ((2*state->drift + state->count)>>31);
  354. #endif
  355. //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);
  356. set_sr_golomb(pb, code, k, 12, bits);
  357. update_vlc_state(state, v);
  358. }
  359. static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state, int bits){
  360. int k, i, v, ret;
  361. i= state->count;
  362. k=0;
  363. while(i < state->error_sum){ //FIXME optimize
  364. k++;
  365. i += i;
  366. }
  367. assert(k<=8);
  368. v= get_sr_golomb(gb, k, 12, bits);
  369. //printf("v:%d bias:%d error:%d drift:%d count:%d k:%d", v, state->bias, state->error_sum, state->drift, state->count, k);
  370. #if 0 // JPEG LS
  371. if(k==0 && 2*state->drift <= - state->count) v ^= (-1);
  372. #else
  373. v ^= ((2*state->drift + state->count)>>31);
  374. #endif
  375. ret= fold(v + state->bias, bits);
  376. update_vlc_state(state, v);
  377. //printf("final: %d\n", ret);
  378. return ret;
  379. }
  380. #if CONFIG_FFV1_ENCODER
  381. static inline int encode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
  382. PlaneContext * const p= &s->plane[plane_index];
  383. RangeCoder * const c= &s->c;
  384. int x;
  385. int run_index= s->run_index;
  386. int run_count=0;
  387. int run_mode=0;
  388. if(s->ac){
  389. if(c->bytestream_end - c->bytestream < w*20){
  390. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  391. return -1;
  392. }
  393. }else{
  394. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < w*4){
  395. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  396. return -1;
  397. }
  398. }
  399. for(x=0; x<w; x++){
  400. int diff, context;
  401. context= get_context(s, sample[0]+x, sample[1]+x, sample[2]+x);
  402. diff= sample[0][x] - predict(sample[0]+x, sample[1]+x);
  403. if(context < 0){
  404. context = -context;
  405. diff= -diff;
  406. }
  407. diff= fold(diff, bits);
  408. if(s->ac){
  409. put_symbol_inline(c, p->state[context], diff, 1);
  410. }else{
  411. if(context == 0) run_mode=1;
  412. if(run_mode){
  413. if(diff){
  414. while(run_count >= 1<<ff_log2_run[run_index]){
  415. run_count -= 1<<ff_log2_run[run_index];
  416. run_index++;
  417. put_bits(&s->pb, 1, 1);
  418. }
  419. put_bits(&s->pb, 1 + ff_log2_run[run_index], run_count);
  420. if(run_index) run_index--;
  421. run_count=0;
  422. run_mode=0;
  423. if(diff>0) diff--;
  424. }else{
  425. run_count++;
  426. }
  427. }
  428. // 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));
  429. if(run_mode == 0)
  430. put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
  431. }
  432. }
  433. if(run_mode){
  434. while(run_count >= 1<<ff_log2_run[run_index]){
  435. run_count -= 1<<ff_log2_run[run_index];
  436. run_index++;
  437. put_bits(&s->pb, 1, 1);
  438. }
  439. if(run_count)
  440. put_bits(&s->pb, 1, 1);
  441. }
  442. s->run_index= run_index;
  443. return 0;
  444. }
  445. static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  446. int x,y,i;
  447. const int ring_size= s->avctx->context_model ? 3 : 2;
  448. int_fast16_t sample_buffer[ring_size][w+6], *sample[ring_size];
  449. s->run_index=0;
  450. memset(sample_buffer, 0, sizeof(sample_buffer));
  451. for(y=0; y<h; y++){
  452. for(i=0; i<ring_size; i++)
  453. sample[i]= sample_buffer[(h+i-y)%ring_size]+3;
  454. sample[0][-1]= sample[1][0 ];
  455. sample[1][ w]= sample[1][w-1];
  456. //{START_TIMER
  457. if(s->avctx->bits_per_raw_sample<=8){
  458. for(x=0; x<w; x++){
  459. sample[0][x]= src[x + stride*y];
  460. }
  461. encode_line(s, w, sample, plane_index, 8);
  462. }else{
  463. for(x=0; x<w; x++){
  464. sample[0][x]= ((uint16_t*)(src + stride*y))[x] >> (16 - s->avctx->bits_per_raw_sample);
  465. }
  466. encode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
  467. }
  468. //STOP_TIMER("encode line")}
  469. }
  470. }
  471. static void encode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  472. int x, y, p, i;
  473. const int ring_size= s->avctx->context_model ? 3 : 2;
  474. int_fast16_t sample_buffer[3][ring_size][w+6], *sample[3][ring_size];
  475. s->run_index=0;
  476. memset(sample_buffer, 0, sizeof(sample_buffer));
  477. for(y=0; y<h; y++){
  478. for(i=0; i<ring_size; i++)
  479. for(p=0; p<3; p++)
  480. sample[p][i]= sample_buffer[p][(h+i-y)%ring_size]+3;
  481. for(x=0; x<w; x++){
  482. int v= src[x + stride*y];
  483. int b= v&0xFF;
  484. int g= (v>>8)&0xFF;
  485. int r= (v>>16)&0xFF;
  486. b -= g;
  487. r -= g;
  488. g += (b + r)>>2;
  489. b += 0x100;
  490. r += 0x100;
  491. // assert(g>=0 && b>=0 && r>=0);
  492. // assert(g<256 && b<512 && r<512);
  493. sample[0][0][x]= g;
  494. sample[1][0][x]= b;
  495. sample[2][0][x]= r;
  496. }
  497. for(p=0; p<3; p++){
  498. sample[p][0][-1]= sample[p][1][0 ];
  499. sample[p][1][ w]= sample[p][1][w-1];
  500. encode_line(s, w, sample[p], FFMIN(p, 1), 9);
  501. }
  502. }
  503. }
  504. static void write_quant_table(RangeCoder *c, int16_t *quant_table){
  505. int last=0;
  506. int i;
  507. uint8_t state[CONTEXT_SIZE];
  508. memset(state, 128, sizeof(state));
  509. for(i=1; i<128 ; i++){
  510. if(quant_table[i] != quant_table[i-1]){
  511. put_symbol(c, state, i-last-1, 0);
  512. last= i;
  513. }
  514. }
  515. put_symbol(c, state, i-last-1, 0);
  516. }
  517. static void write_header(FFV1Context *f){
  518. uint8_t state[CONTEXT_SIZE];
  519. int i;
  520. RangeCoder * const c= &f->c;
  521. memset(state, 128, sizeof(state));
  522. put_symbol(c, state, f->version, 0);
  523. put_symbol(c, state, f->ac, 0);
  524. if(f->ac>1){
  525. for(i=1; i<256; i++){
  526. f->state_transition[i]=ver2_state[i];
  527. put_symbol(c, state, ver2_state[i] - c->one_state[i], 1);
  528. }
  529. }
  530. put_symbol(c, state, f->colorspace, 0); //YUV cs type
  531. if(f->version>0)
  532. put_symbol(c, state, f->avctx->bits_per_raw_sample, 0);
  533. put_rac(c, state, 1); //chroma planes
  534. put_symbol(c, state, f->chroma_h_shift, 0);
  535. put_symbol(c, state, f->chroma_v_shift, 0);
  536. put_rac(c, state, 0); //no transparency plane
  537. for(i=0; i<5; i++)
  538. write_quant_table(c, f->quant_table[i]);
  539. }
  540. #endif /* CONFIG_FFV1_ENCODER */
  541. static av_cold int common_init(AVCodecContext *avctx){
  542. FFV1Context *s = avctx->priv_data;
  543. s->avctx= avctx;
  544. s->flags= avctx->flags;
  545. dsputil_init(&s->dsp, avctx);
  546. s->width = avctx->width;
  547. s->height= avctx->height;
  548. assert(s->width && s->height);
  549. return 0;
  550. }
  551. #if CONFIG_FFV1_ENCODER
  552. static av_cold int encode_init(AVCodecContext *avctx)
  553. {
  554. FFV1Context *s = avctx->priv_data;
  555. int i;
  556. common_init(avctx);
  557. s->version=0;
  558. s->ac= avctx->coder_type ? 2:0;
  559. s->plane_count=2;
  560. for(i=0; i<256; i++){
  561. if(avctx->bits_per_raw_sample <=8){
  562. s->quant_table[0][i]= quant11[i];
  563. s->quant_table[1][i]= 11*quant11[i];
  564. if(avctx->context_model==0){
  565. s->quant_table[2][i]= 11*11*quant11[i];
  566. s->quant_table[3][i]=
  567. s->quant_table[4][i]=0;
  568. }else{
  569. s->quant_table[2][i]= 11*11*quant5 [i];
  570. s->quant_table[3][i]= 5*11*11*quant5 [i];
  571. s->quant_table[4][i]= 5*5*11*11*quant5 [i];
  572. }
  573. }else{
  574. s->quant_table[0][i]= quant9_10bit[i];
  575. s->quant_table[1][i]= 11*quant9_10bit[i];
  576. if(avctx->context_model==0){
  577. s->quant_table[2][i]= 11*11*quant9_10bit[i];
  578. s->quant_table[3][i]=
  579. s->quant_table[4][i]=0;
  580. }else{
  581. s->quant_table[2][i]= 11*11*quant5_10bit[i];
  582. s->quant_table[3][i]= 5*11*11*quant5_10bit[i];
  583. s->quant_table[4][i]= 5*5*11*11*quant5_10bit[i];
  584. }
  585. }
  586. }
  587. for(i=0; i<s->plane_count; i++){
  588. PlaneContext * const p= &s->plane[i];
  589. if(avctx->context_model==0){
  590. p->context_count= (11*11*11+1)/2;
  591. }else{
  592. p->context_count= (11*11*5*5*5+1)/2;
  593. }
  594. if(s->ac){
  595. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  596. }else{
  597. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  598. }
  599. }
  600. avctx->coded_frame= &s->picture;
  601. switch(avctx->pix_fmt){
  602. case PIX_FMT_YUV444P16:
  603. case PIX_FMT_YUV422P16:
  604. case PIX_FMT_YUV420P16:
  605. if(avctx->bits_per_raw_sample <=8){
  606. av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
  607. return -1;
  608. }
  609. if(!s->ac){
  610. av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample of more than 8 needs -coder 1 currently\n");
  611. return -1;
  612. }
  613. s->version= 1;
  614. case PIX_FMT_YUV444P:
  615. case PIX_FMT_YUV422P:
  616. case PIX_FMT_YUV420P:
  617. case PIX_FMT_YUV411P:
  618. case PIX_FMT_YUV410P:
  619. s->colorspace= 0;
  620. break;
  621. case PIX_FMT_RGB32:
  622. s->colorspace= 1;
  623. break;
  624. default:
  625. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  626. return -1;
  627. }
  628. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  629. s->picture_number=0;
  630. return 0;
  631. }
  632. #endif /* CONFIG_FFV1_ENCODER */
  633. static void clear_state(FFV1Context *f){
  634. int i, j;
  635. for(i=0; i<f->plane_count; i++){
  636. PlaneContext *p= &f->plane[i];
  637. p->interlace_bit_state[0]= 128;
  638. p->interlace_bit_state[1]= 128;
  639. for(j=0; j<p->context_count; j++){
  640. if(f->ac){
  641. memset(p->state[j], 128, sizeof(uint8_t)*CONTEXT_SIZE);
  642. }else{
  643. p->vlc_state[j].drift= 0;
  644. p->vlc_state[j].error_sum= 4; //FFMAX((RANGE + 32)/64, 2);
  645. p->vlc_state[j].bias= 0;
  646. p->vlc_state[j].count= 1;
  647. }
  648. }
  649. }
  650. }
  651. #if CONFIG_FFV1_ENCODER
  652. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  653. FFV1Context *f = avctx->priv_data;
  654. RangeCoder * const c= &f->c;
  655. AVFrame *pict = data;
  656. const int width= f->width;
  657. const int height= f->height;
  658. AVFrame * const p= &f->picture;
  659. int used_count= 0;
  660. uint8_t keystate=128;
  661. ff_init_range_encoder(c, buf, buf_size);
  662. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  663. *p = *pict;
  664. p->pict_type= FF_I_TYPE;
  665. if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){
  666. put_rac(c, &keystate, 1);
  667. p->key_frame= 1;
  668. write_header(f);
  669. clear_state(f);
  670. }else{
  671. put_rac(c, &keystate, 0);
  672. p->key_frame= 0;
  673. }
  674. if(!f->ac){
  675. used_count += ff_rac_terminate(c);
  676. //printf("pos=%d\n", used_count);
  677. init_put_bits(&f->pb, buf + used_count, buf_size - used_count);
  678. }else if (f->ac>1){
  679. int i;
  680. for(i=1; i<256; i++){
  681. c->one_state[i]= f->state_transition[i];
  682. c->zero_state[256-i]= 256-c->one_state[i];
  683. }
  684. }
  685. if(f->colorspace==0){
  686. const int chroma_width = -((-width )>>f->chroma_h_shift);
  687. const int chroma_height= -((-height)>>f->chroma_v_shift);
  688. encode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  689. encode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  690. encode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
  691. }else{
  692. encode_rgb_frame(f, (uint32_t*)(p->data[0]), width, height, p->linesize[0]/4);
  693. }
  694. emms_c();
  695. f->picture_number++;
  696. if(f->ac){
  697. return ff_rac_terminate(c);
  698. }else{
  699. flush_put_bits(&f->pb); //nicer padding FIXME
  700. return used_count + (put_bits_count(&f->pb)+7)/8;
  701. }
  702. }
  703. #endif /* CONFIG_FFV1_ENCODER */
  704. static av_cold int common_end(AVCodecContext *avctx){
  705. FFV1Context *s = avctx->priv_data;
  706. int i;
  707. for(i=0; i<s->plane_count; i++){
  708. PlaneContext *p= &s->plane[i];
  709. av_freep(&p->state);
  710. av_freep(&p->vlc_state);
  711. }
  712. return 0;
  713. }
  714. static av_always_inline void decode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
  715. PlaneContext * const p= &s->plane[plane_index];
  716. RangeCoder * const c= &s->c;
  717. int x;
  718. int run_count=0;
  719. int run_mode=0;
  720. int run_index= s->run_index;
  721. for(x=0; x<w; x++){
  722. int diff, context, sign;
  723. context= get_context(s, sample[1] + x, sample[0] + x, sample[1] + x);
  724. if(context < 0){
  725. context= -context;
  726. sign=1;
  727. }else
  728. sign=0;
  729. if(s->ac){
  730. diff= get_symbol_inline(c, p->state[context], 1);
  731. }else{
  732. if(context == 0 && run_mode==0) run_mode=1;
  733. if(run_mode){
  734. if(run_count==0 && run_mode==1){
  735. if(get_bits1(&s->gb)){
  736. run_count = 1<<ff_log2_run[run_index];
  737. if(x + run_count <= w) run_index++;
  738. }else{
  739. if(ff_log2_run[run_index]) run_count = get_bits(&s->gb, ff_log2_run[run_index]);
  740. else run_count=0;
  741. if(run_index) run_index--;
  742. run_mode=2;
  743. }
  744. }
  745. run_count--;
  746. if(run_count < 0){
  747. run_mode=0;
  748. run_count=0;
  749. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  750. if(diff>=0) diff++;
  751. }else
  752. diff=0;
  753. }else
  754. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  755. // 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));
  756. }
  757. if(sign) diff= -diff;
  758. sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
  759. }
  760. s->run_index= run_index;
  761. }
  762. static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  763. int x, y;
  764. int_fast16_t sample_buffer[2][w+6];
  765. int_fast16_t *sample[2];
  766. sample[0]=sample_buffer[0]+3;
  767. sample[1]=sample_buffer[1]+3;
  768. s->run_index=0;
  769. memset(sample_buffer, 0, sizeof(sample_buffer));
  770. for(y=0; y<h; y++){
  771. int_fast16_t *temp= sample[0]; //FIXME try a normal buffer
  772. sample[0]= sample[1];
  773. sample[1]= temp;
  774. sample[1][-1]= sample[0][0 ];
  775. sample[0][ w]= sample[0][w-1];
  776. //{START_TIMER
  777. if(s->avctx->bits_per_raw_sample <= 8){
  778. decode_line(s, w, sample, plane_index, 8);
  779. for(x=0; x<w; x++){
  780. src[x + stride*y]= sample[1][x];
  781. }
  782. }else{
  783. decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
  784. for(x=0; x<w; x++){
  785. ((uint16_t*)(src + stride*y))[x]= sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
  786. }
  787. }
  788. //STOP_TIMER("decode-line")}
  789. }
  790. }
  791. static void decode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  792. int x, y, p;
  793. int_fast16_t sample_buffer[3][2][w+6];
  794. int_fast16_t *sample[3][2];
  795. for(x=0; x<3; x++){
  796. sample[x][0] = sample_buffer[x][0]+3;
  797. sample[x][1] = sample_buffer[x][1]+3;
  798. }
  799. s->run_index=0;
  800. memset(sample_buffer, 0, sizeof(sample_buffer));
  801. for(y=0; y<h; y++){
  802. for(p=0; p<3; p++){
  803. int_fast16_t *temp= sample[p][0]; //FIXME try a normal buffer
  804. sample[p][0]= sample[p][1];
  805. sample[p][1]= temp;
  806. sample[p][1][-1]= sample[p][0][0 ];
  807. sample[p][0][ w]= sample[p][0][w-1];
  808. decode_line(s, w, sample[p], FFMIN(p, 1), 9);
  809. }
  810. for(x=0; x<w; x++){
  811. int g= sample[0][1][x];
  812. int b= sample[1][1][x];
  813. int r= sample[2][1][x];
  814. // assert(g>=0 && b>=0 && r>=0);
  815. // assert(g<256 && b<512 && r<512);
  816. b -= 0x100;
  817. r -= 0x100;
  818. g -= (b + r)>>2;
  819. b += g;
  820. r += g;
  821. src[x + stride*y]= b + (g<<8) + (r<<16);
  822. }
  823. }
  824. }
  825. static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){
  826. int v;
  827. int i=0;
  828. uint8_t state[CONTEXT_SIZE];
  829. memset(state, 128, sizeof(state));
  830. for(v=0; i<128 ; v++){
  831. int len= get_symbol(c, state, 0) + 1;
  832. if(len + i > 128) return -1;
  833. while(len--){
  834. quant_table[i] = scale*v;
  835. i++;
  836. //printf("%2d ",v);
  837. //if(i%16==0) printf("\n");
  838. }
  839. }
  840. for(i=1; i<128; i++){
  841. quant_table[256-i]= -quant_table[i];
  842. }
  843. quant_table[128]= -quant_table[127];
  844. return 2*v - 1;
  845. }
  846. static int read_header(FFV1Context *f){
  847. uint8_t state[CONTEXT_SIZE];
  848. int i, context_count;
  849. RangeCoder * const c= &f->c;
  850. memset(state, 128, sizeof(state));
  851. f->version= get_symbol(c, state, 0);
  852. f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
  853. if(f->ac>1){
  854. for(i=1; i<256; i++){
  855. f->state_transition[i]= get_symbol(c, state, 1) + c->one_state[i];
  856. }
  857. }
  858. f->colorspace= get_symbol(c, state, 0); //YUV cs type
  859. if(f->version>0)
  860. f->avctx->bits_per_raw_sample= get_symbol(c, state, 0);
  861. get_rac(c, state); //no chroma = false
  862. f->chroma_h_shift= get_symbol(c, state, 0);
  863. f->chroma_v_shift= get_symbol(c, state, 0);
  864. get_rac(c, state); //transparency plane
  865. f->plane_count= 2;
  866. if(f->colorspace==0){
  867. if(f->avctx->bits_per_raw_sample<=8){
  868. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  869. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
  870. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
  871. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
  872. case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
  873. case 0x22: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
  874. default:
  875. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  876. return -1;
  877. }
  878. }else{
  879. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  880. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P16; break;
  881. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P16; break;
  882. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P16; break;
  883. default:
  884. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  885. return -1;
  886. }
  887. }
  888. }else if(f->colorspace==1){
  889. if(f->chroma_h_shift || f->chroma_v_shift){
  890. av_log(f->avctx, AV_LOG_ERROR, "chroma subsampling not supported in this colorspace\n");
  891. return -1;
  892. }
  893. f->avctx->pix_fmt= PIX_FMT_RGB32;
  894. }else{
  895. av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
  896. return -1;
  897. }
  898. //printf("%d %d %d\n", f->chroma_h_shift, f->chroma_v_shift,f->avctx->pix_fmt);
  899. context_count=1;
  900. for(i=0; i<5; i++){
  901. context_count*= read_quant_table(c, f->quant_table[i], context_count);
  902. if(context_count < 0 || context_count > 32768){
  903. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  904. return -1;
  905. }
  906. }
  907. context_count= (context_count+1)/2;
  908. for(i=0; i<f->plane_count; i++){
  909. PlaneContext * const p= &f->plane[i];
  910. p->context_count= context_count;
  911. if(f->ac){
  912. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  913. }else{
  914. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  915. }
  916. }
  917. return 0;
  918. }
  919. static av_cold int decode_init(AVCodecContext *avctx)
  920. {
  921. // FFV1Context *s = avctx->priv_data;
  922. common_init(avctx);
  923. return 0;
  924. }
  925. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt){
  926. const uint8_t *buf = avpkt->data;
  927. int buf_size = avpkt->size;
  928. FFV1Context *f = avctx->priv_data;
  929. RangeCoder * const c= &f->c;
  930. const int width= f->width;
  931. const int height= f->height;
  932. AVFrame * const p= &f->picture;
  933. int bytes_read;
  934. uint8_t keystate= 128;
  935. AVFrame *picture = data;
  936. ff_init_range_decoder(c, buf, buf_size);
  937. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  938. p->pict_type= FF_I_TYPE; //FIXME I vs. P
  939. if(get_rac(c, &keystate)){
  940. p->key_frame= 1;
  941. if(read_header(f) < 0)
  942. return -1;
  943. clear_state(f);
  944. }else{
  945. p->key_frame= 0;
  946. }
  947. if(f->ac>1){
  948. int i;
  949. for(i=1; i<256; i++){
  950. c->one_state[i]= f->state_transition[i];
  951. c->zero_state[256-i]= 256-c->one_state[i];
  952. }
  953. }
  954. if(!f->plane[0].state && !f->plane[0].vlc_state)
  955. return -1;
  956. p->reference= 0;
  957. if(avctx->get_buffer(avctx, p) < 0){
  958. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  959. return -1;
  960. }
  961. if(avctx->debug&FF_DEBUG_PICT_INFO)
  962. av_log(avctx, AV_LOG_ERROR, "keyframe:%d coder:%d\n", p->key_frame, f->ac);
  963. if(!f->ac){
  964. bytes_read = c->bytestream - c->bytestream_start - 1;
  965. if(bytes_read ==0) av_log(avctx, AV_LOG_ERROR, "error at end of AC stream\n"); //FIXME
  966. //printf("pos=%d\n", bytes_read);
  967. init_get_bits(&f->gb, buf + bytes_read, buf_size - bytes_read);
  968. } else {
  969. bytes_read = 0; /* avoid warning */
  970. }
  971. if(f->colorspace==0){
  972. const int chroma_width = -((-width )>>f->chroma_h_shift);
  973. const int chroma_height= -((-height)>>f->chroma_v_shift);
  974. decode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  975. decode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  976. decode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
  977. }else{
  978. decode_rgb_frame(f, (uint32_t*)p->data[0], width, height, p->linesize[0]/4);
  979. }
  980. emms_c();
  981. f->picture_number++;
  982. *picture= *p;
  983. avctx->release_buffer(avctx, p); //FIXME
  984. *data_size = sizeof(AVFrame);
  985. if(f->ac){
  986. bytes_read= c->bytestream - c->bytestream_start - 1;
  987. if(bytes_read ==0) av_log(f->avctx, AV_LOG_ERROR, "error at end of frame\n");
  988. }else{
  989. bytes_read+= (get_bits_count(&f->gb)+7)/8;
  990. }
  991. return bytes_read;
  992. }
  993. AVCodec ffv1_decoder = {
  994. "ffv1",
  995. AVMEDIA_TYPE_VIDEO,
  996. CODEC_ID_FFV1,
  997. sizeof(FFV1Context),
  998. decode_init,
  999. NULL,
  1000. common_end,
  1001. decode_frame,
  1002. CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  1003. NULL,
  1004. .long_name= NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  1005. };
  1006. #if CONFIG_FFV1_ENCODER
  1007. AVCodec ffv1_encoder = {
  1008. "ffv1",
  1009. AVMEDIA_TYPE_VIDEO,
  1010. CODEC_ID_FFV1,
  1011. sizeof(FFV1Context),
  1012. encode_init,
  1013. encode_frame,
  1014. common_end,
  1015. .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},
  1016. .long_name= NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  1017. };
  1018. #endif