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.

941 lines
27KB

  1. /*
  2. * FFV1 codec for libavcodec
  3. *
  4. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. /**
  22. * @file ffv1.c
  23. * FF Video Codec 1 (a experimental lossless codec)
  24. */
  25. #include "common.h"
  26. #include "avcodec.h"
  27. #include "dsputil.h"
  28. #include "cabac.h"
  29. #include "golomb.h"
  30. #define MAX_PLANES 4
  31. #define CONTEXT_SIZE 32
  32. static const int8_t quant3[256]={
  33. 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  34. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  35. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  36. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  37. 1, 1, 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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-2,-2,-2,-2,-2,-2,
  84. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,-1,
  85. };
  86. static const int8_t quant9[256]={
  87. 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  88. 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  89. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  90. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  91. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  92. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  93. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  94. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  95. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  96. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  97. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  98. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  99. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  100. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  101. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,
  102. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-1,-1,
  103. };
  104. static const int8_t quant11[256]={
  105. 0, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
  106. 4, 4, 4, 4, 4, 4, 4, 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 6, 6, 6, 6,
  128. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  129. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  130. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  131. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  132. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  133. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  134. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  135. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-5,
  136. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  137. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  138. -4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,-2,-2,-1,
  139. };
  140. static const uint8_t log2_run[32]={
  141. 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
  142. 4, 4, 5, 5, 6, 6, 7, 7,
  143. 8, 9,10,11,12,13,14,15,
  144. };
  145. typedef struct VlcState{
  146. int16_t drift;
  147. uint16_t error_sum;
  148. int8_t bias;
  149. uint8_t count;
  150. } VlcState;
  151. typedef struct PlaneContext{
  152. int context_count;
  153. uint8_t (*state)[CONTEXT_SIZE];
  154. VlcState *vlc_state;
  155. uint8_t interlace_bit_state[2];
  156. } PlaneContext;
  157. typedef struct FFV1Context{
  158. AVCodecContext *avctx;
  159. CABACContext c;
  160. GetBitContext gb;
  161. PutBitContext pb;
  162. int version;
  163. int width, height;
  164. int chroma_h_shift, chroma_v_shift;
  165. int flags;
  166. int picture_number;
  167. AVFrame picture;
  168. int plane_count;
  169. int ac; ///< 1-> CABAC 0-> golomb rice
  170. PlaneContext plane[MAX_PLANES];
  171. int16_t quant_table[5][256];
  172. DSPContext dsp;
  173. }FFV1Context;
  174. static inline int predict(uint8_t *src, uint8_t *last){
  175. const int LT= last[-1];
  176. const int T= last[ 0];
  177. const int L = src[-1];
  178. return mid_pred(L, L + T - LT, T);
  179. }
  180. static inline int get_context(FFV1Context *f, uint8_t *src, uint8_t *last, uint8_t *last2){
  181. const int LT= last[-1];
  182. const int T= last[ 0];
  183. const int RT= last[ 1];
  184. const int L = src[-1];
  185. if(f->quant_table[3][127]){
  186. const int TT= last2[0];
  187. const int LL= src[-2];
  188. return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF]
  189. +f->quant_table[3][(LL-L) & 0xFF] + f->quant_table[4][(TT-T) & 0xFF];
  190. }else
  191. return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF];
  192. }
  193. /**
  194. * put
  195. */
  196. static inline void put_symbol(CABACContext *c, uint8_t *state, int v, int is_signed, int max_exp){
  197. int i;
  198. if(v){
  199. const int a= ABS(v);
  200. const int e= av_log2(a);
  201. put_cabac(c, state+0, 0);
  202. for(i=0; i<e; i++){
  203. put_cabac(c, state+1+i, 1); //1..8
  204. }
  205. if(e<max_exp){
  206. put_cabac(c, state+1+i, 0); //1..8
  207. for(i=e-1; i>=0; i--){
  208. put_cabac(c, state+16+e+i, (a>>i)&1); //17..29
  209. }
  210. if(is_signed)
  211. put_cabac(c, state+9 + e, v < 0); //9..16
  212. }
  213. }else{
  214. put_cabac(c, state+0, 1);
  215. }
  216. }
  217. static inline int get_symbol(CABACContext *c, uint8_t *state, int is_signed, int max_exp){
  218. if(get_cabac(c, state+0))
  219. return 0;
  220. else{
  221. int i, e;
  222. for(e=0; e<max_exp; e++){
  223. int a= 1<<e;
  224. if(get_cabac(c, state + 1 + e)==0){ // 1..8
  225. for(i=e-1; i>=0; i--){
  226. a += get_cabac(c, state+16+e+i)<<i; //17..29
  227. }
  228. if(is_signed && get_cabac(c, state+9 + e)) //9..16
  229. return -a;
  230. else
  231. return a;
  232. }
  233. }
  234. return -(1<<e);
  235. }
  236. }
  237. static inline void update_vlc_state(VlcState * const state, const int v){
  238. int drift= state->drift;
  239. int count= state->count;
  240. state->error_sum += ABS(v);
  241. drift += v;
  242. if(count == 128){ //FIXME variable
  243. count >>= 1;
  244. drift >>= 1;
  245. state->error_sum >>= 1;
  246. }
  247. count++;
  248. if(drift <= -count){
  249. if(state->bias > -128) state->bias--;
  250. drift += count;
  251. if(drift <= -count)
  252. drift= -count + 1;
  253. }else if(drift > 0){
  254. if(state->bias < 127) state->bias++;
  255. drift -= count;
  256. if(drift > 0)
  257. drift= 0;
  258. }
  259. state->drift= drift;
  260. state->count= count;
  261. }
  262. static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int v){
  263. int i, k, code;
  264. //printf("final: %d ", v);
  265. v = (int8_t)(v - state->bias);
  266. i= state->count;
  267. k=0;
  268. while(i < state->error_sum){ //FIXME optimize
  269. k++;
  270. i += i;
  271. }
  272. assert(k<=8);
  273. #if 0 // JPEG LS
  274. if(k==0 && 2*state->drift <= - state->count) code= v ^ (-1);
  275. else code= v;
  276. #else
  277. code= v ^ ((2*state->drift + state->count)>>31);
  278. #endif
  279. code = -2*code-1;
  280. code^= (code>>31);
  281. //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);
  282. set_ur_golomb(pb, code, k, 12, 8);
  283. update_vlc_state(state, v);
  284. }
  285. static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state){
  286. int k, i, v, ret;
  287. i= state->count;
  288. k=0;
  289. while(i < state->error_sum){ //FIXME optimize
  290. k++;
  291. i += i;
  292. }
  293. assert(k<=8);
  294. v= get_ur_golomb(gb, k, 12, 8);
  295. //printf("v:%d bias:%d error:%d drift:%d count:%d k:%d", v, state->bias, state->error_sum, state->drift, state->count, k);
  296. v++;
  297. if(v&1) v= (v>>1);
  298. else v= -(v>>1);
  299. #if 0 // JPEG LS
  300. if(k==0 && 2*state->drift <= - state->count) v ^= (-1);
  301. #else
  302. v ^= ((2*state->drift + state->count)>>31);
  303. #endif
  304. ret= (int8_t)(v + state->bias);
  305. update_vlc_state(state, v);
  306. //printf("final: %d\n", ret);
  307. return ret;
  308. }
  309. static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  310. PlaneContext * const p= &s->plane[plane_index];
  311. CABACContext * const c= &s->c;
  312. int x,y;
  313. uint8_t sample_buffer[2][w+6];
  314. uint8_t *sample[2]= {sample_buffer[0]+3, sample_buffer[1]+3};
  315. int run_index=0;
  316. memset(sample_buffer, 0, sizeof(sample_buffer));
  317. for(y=0; y<h; y++){
  318. uint8_t *temp= sample[0]; //FIXME try a normal buffer
  319. int run_count=0;
  320. int run_mode=0;
  321. sample[0]= sample[1];
  322. sample[1]= temp;
  323. sample[1][-1]= sample[0][0 ];
  324. sample[0][ w]= sample[0][w-1];
  325. for(x=0; x<w; x++){
  326. uint8_t *temp_src= src + x + stride*y;
  327. int diff, context;
  328. context= get_context(s, sample[1]+x, sample[0]+x, sample[1]+x);
  329. diff= temp_src[0] - predict(sample[1]+x, sample[0]+x);
  330. if(context < 0){
  331. context = -context;
  332. diff= -diff;
  333. }
  334. diff= (int8_t)diff;
  335. if(s->ac){
  336. put_symbol(c, p->state[context], diff, 1, 7);
  337. }else{
  338. if(context == 0) run_mode=1;
  339. if(run_mode){
  340. if(diff){
  341. while(run_count >= 1<<log2_run[run_index]){
  342. run_count -= 1<<log2_run[run_index];
  343. run_index++;
  344. put_bits(&s->pb, 1, 1);
  345. }
  346. put_bits(&s->pb, 1 + log2_run[run_index], run_count);
  347. if(run_index) run_index--;
  348. run_count=0;
  349. run_mode=0;
  350. if(diff>0) diff--;
  351. }else{
  352. run_count++;
  353. }
  354. }
  355. // printf("count:%d index:%d, mode:%d, x:%d y:%d pos:%d\n", run_count, run_index, run_mode, x, y, (int)get_bit_count(&s->pb));
  356. if(run_mode == 0)
  357. put_vlc_symbol(&s->pb, &p->vlc_state[context], diff);
  358. }
  359. sample[1][x]= temp_src[0];
  360. }
  361. if(run_mode){
  362. while(run_count >= 1<<log2_run[run_index]){
  363. run_count -= 1<<log2_run[run_index];
  364. run_index++;
  365. put_bits(&s->pb, 1, 1);
  366. }
  367. if(run_count)
  368. put_bits(&s->pb, 1, 1);
  369. }
  370. }
  371. }
  372. static void write_quant_table(CABACContext *c, int16_t *quant_table){
  373. int last=0;
  374. int i;
  375. uint8_t state[CONTEXT_SIZE]={0};
  376. for(i=1; i<128 ; i++){
  377. if(quant_table[i] != quant_table[i-1]){
  378. put_symbol(c, state, i-last-1, 0, 7);
  379. last= i;
  380. }
  381. }
  382. put_symbol(c, state, i-last-1, 0, 7);
  383. }
  384. static void write_header(FFV1Context *f){
  385. uint8_t state[CONTEXT_SIZE]={0};
  386. int i;
  387. CABACContext * const c= &f->c;
  388. put_symbol(c, state, f->version, 0, 7);
  389. put_symbol(c, state, f->avctx->coder_type, 0, 7);
  390. put_symbol(c, state, 0, 0, 7); //YUV cs type
  391. put_cabac(c, state, 1); //chroma planes
  392. put_symbol(c, state, f->chroma_h_shift, 0, 7);
  393. put_symbol(c, state, f->chroma_v_shift, 0, 7);
  394. put_cabac(c, state, 0); //no transparency plane
  395. for(i=0; i<5; i++)
  396. write_quant_table(c, f->quant_table[i]);
  397. }
  398. static int common_init(AVCodecContext *avctx){
  399. FFV1Context *s = avctx->priv_data;
  400. int width, height;
  401. s->avctx= avctx;
  402. s->flags= avctx->flags;
  403. dsputil_init(&s->dsp, avctx);
  404. width= s->width= avctx->width;
  405. height= s->height= avctx->height;
  406. assert(width && height);
  407. return 0;
  408. }
  409. static int encode_init(AVCodecContext *avctx)
  410. {
  411. FFV1Context *s = avctx->priv_data;
  412. int i;
  413. common_init(avctx);
  414. s->version=0;
  415. s->ac= avctx->coder_type;
  416. s->plane_count=2;
  417. for(i=0; i<256; i++){
  418. s->quant_table[0][i]= quant11[i];
  419. s->quant_table[1][i]= 11*quant11[i];
  420. if(avctx->context_model==0){
  421. s->quant_table[2][i]= 11*11*quant11[i];
  422. s->quant_table[3][i]=
  423. s->quant_table[4][i]=0;
  424. }else{
  425. s->quant_table[2][i]= 11*11*quant5 [i];
  426. s->quant_table[3][i]= 5*11*11*quant5 [i];
  427. s->quant_table[4][i]= 5*5*11*11*quant5 [i];
  428. }
  429. }
  430. for(i=0; i<s->plane_count; i++){
  431. PlaneContext * const p= &s->plane[i];
  432. if(avctx->context_model==0){
  433. p->context_count= (11*11*11+1)/2;
  434. }else{
  435. p->context_count= (11*11*5*5*5+1)/2;
  436. }
  437. if(s->ac){
  438. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  439. }else{
  440. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  441. }
  442. }
  443. avctx->coded_frame= &s->picture;
  444. switch(avctx->pix_fmt){
  445. case PIX_FMT_YUV444P:
  446. case PIX_FMT_YUV422P:
  447. case PIX_FMT_YUV420P:
  448. case PIX_FMT_YUV411P:
  449. case PIX_FMT_YUV410P:
  450. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  451. break;
  452. default:
  453. fprintf(stderr, "format not supported\n");
  454. return -1;
  455. }
  456. s->picture_number=0;
  457. return 0;
  458. }
  459. static void clear_state(FFV1Context *f){
  460. int i, j;
  461. for(i=0; i<f->plane_count; i++){
  462. PlaneContext *p= &f->plane[i];
  463. p->interlace_bit_state[0]= 0;
  464. p->interlace_bit_state[1]= 0;
  465. for(j=0; j<p->context_count; j++){
  466. if(f->ac){
  467. memset(p->state[j], 0, sizeof(uint8_t)*CONTEXT_SIZE);
  468. p->state[j][7] = 2*62;
  469. }else{
  470. p->vlc_state[j].drift= 0;
  471. p->vlc_state[j].error_sum= 4; //FFMAX((RANGE + 32)/64, 2);
  472. p->vlc_state[j].bias= 0;
  473. p->vlc_state[j].count= 1;
  474. }
  475. }
  476. }
  477. }
  478. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  479. FFV1Context *f = avctx->priv_data;
  480. CABACContext * const c= &f->c;
  481. AVFrame *pict = data;
  482. const int width= f->width;
  483. const int height= f->height;
  484. AVFrame * const p= &f->picture;
  485. int used_count= 0;
  486. if(avctx->strict_std_compliance >= 0){
  487. printf("this codec is under development, files encoded with it wont be decodeable with future versions!!!\n"
  488. "use vstrict=-1 to use it anyway\n");
  489. return -1;
  490. }
  491. ff_init_cabac_encoder(c, buf, buf_size);
  492. ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  493. *p = *pict;
  494. p->pict_type= FF_I_TYPE;
  495. if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){
  496. put_cabac_bypass(c, 1);
  497. p->key_frame= 1;
  498. write_header(f);
  499. clear_state(f);
  500. }else{
  501. put_cabac_bypass(c, 0);
  502. p->key_frame= 0;
  503. }
  504. if(!f->ac){
  505. used_count += put_cabac_terminate(c, 1);
  506. //printf("pos=%d\n", used_count);
  507. init_put_bits(&f->pb, buf + used_count, buf_size - used_count, NULL, NULL);
  508. }
  509. if(1){
  510. const int chroma_width = -((-width )>>f->chroma_h_shift);
  511. const int chroma_height= -((-height)>>f->chroma_v_shift);
  512. encode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  513. encode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  514. encode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
  515. }
  516. emms_c();
  517. f->picture_number++;
  518. if(f->ac){
  519. return put_cabac_terminate(c, 1);
  520. }else{
  521. flush_put_bits(&f->pb); //nicer padding FIXME
  522. return used_count + (get_bit_count(&f->pb)+7)/8;
  523. }
  524. }
  525. static void common_end(FFV1Context *s){
  526. int i;
  527. for(i=0; i<s->plane_count; i++){
  528. PlaneContext *p= &s->plane[i];
  529. av_freep(&p->state);
  530. }
  531. }
  532. static int encode_end(AVCodecContext *avctx)
  533. {
  534. FFV1Context *s = avctx->priv_data;
  535. common_end(s);
  536. return 0;
  537. }
  538. static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  539. PlaneContext * const p= &s->plane[plane_index];
  540. CABACContext * const c= &s->c;
  541. int x,y;
  542. uint8_t sample_buffer[2][w+6];
  543. uint8_t *sample[2]= {sample_buffer[0]+3, sample_buffer[1]+3};
  544. int run_index=0;
  545. memset(sample_buffer, 0, sizeof(sample_buffer));
  546. for(y=0; y<h; y++){
  547. uint8_t *temp= sample[0]; //FIXME try a normal buffer
  548. int run_count=0;
  549. int run_mode=0;
  550. sample[0]= sample[1];
  551. sample[1]= temp;
  552. sample[1][-1]= sample[0][0 ];
  553. sample[0][ w]= sample[0][w-1];
  554. for(x=0; x<w; x++){
  555. uint8_t *temp_src= src + x + stride*y;
  556. int diff, context, sign;
  557. context= get_context(s, sample[1] + x, sample[0] + x, sample[1] + x);
  558. if(context < 0){
  559. context= -context;
  560. sign=1;
  561. }else
  562. sign=0;
  563. if(s->ac)
  564. diff= get_symbol(c, p->state[context], 1, 7);
  565. else{
  566. if(context == 0 && run_mode==0) run_mode=1;
  567. if(run_mode){
  568. if(run_count==0 && run_mode==1){
  569. if(get_bits1(&s->gb)){
  570. run_count = 1<<log2_run[run_index];
  571. if(x + run_count <= w) run_index++;
  572. }else{
  573. if(log2_run[run_index]) run_count = get_bits(&s->gb, log2_run[run_index]);
  574. else run_count=0;
  575. if(run_index) run_index--;
  576. run_mode=2;
  577. }
  578. }
  579. run_count--;
  580. if(run_count < 0){
  581. run_mode=0;
  582. run_count=0;
  583. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context]);
  584. if(diff>=0) diff++;
  585. }else
  586. diff=0;
  587. }else
  588. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context]);
  589. // 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));
  590. }
  591. if(sign) diff= (int8_t)(-diff); //FIXME remove cast
  592. sample[1][x]=
  593. temp_src[0] = predict(sample[1] + x, sample[0] + x) + diff;
  594. assert(diff>= -128 && diff <= 127);
  595. }
  596. }
  597. }
  598. static int read_quant_table(CABACContext *c, int16_t *quant_table, int scale){
  599. int v;
  600. int i=0;
  601. uint8_t state[CONTEXT_SIZE]={0};
  602. for(v=0; i<128 ; v++){
  603. int len= get_symbol(c, state, 0, 7) + 1;
  604. if(len + i > 128) return -1;
  605. while(len--){
  606. quant_table[i] = scale*v;
  607. i++;
  608. //printf("%2d ",v);
  609. //if(i%16==0) printf("\n");
  610. }
  611. }
  612. for(i=1; i<128; i++){
  613. quant_table[256-i]= -quant_table[i];
  614. }
  615. quant_table[128]= -quant_table[127];
  616. return 2*v - 1;
  617. }
  618. static int read_header(FFV1Context *f){
  619. uint8_t state[CONTEXT_SIZE]={0};
  620. int i, context_count;
  621. CABACContext * const c= &f->c;
  622. f->version= get_symbol(c, state, 0, 7);
  623. f->ac= f->avctx->coder_type= get_symbol(c, state, 0, 7);
  624. get_symbol(c, state, 0, 7); //YUV cs type
  625. get_cabac(c, state); //no chroma = false
  626. f->chroma_h_shift= get_symbol(c, state, 0, 7);
  627. f->chroma_v_shift= get_symbol(c, state, 0, 7);
  628. get_cabac(c, state); //transparency plane
  629. f->plane_count= 2;
  630. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  631. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
  632. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
  633. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
  634. case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
  635. case 0x33: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
  636. default:
  637. fprintf(stderr, "format not supported\n");
  638. return -1;
  639. }
  640. //printf("%d %d %d\n", f->chroma_h_shift, f->chroma_v_shift,f->avctx->pix_fmt);
  641. context_count=1;
  642. for(i=0; i<5; i++){
  643. context_count*= read_quant_table(c, f->quant_table[i], context_count);
  644. if(context_count < 0){
  645. printf("read_quant_table error\n");
  646. return -1;
  647. }
  648. }
  649. context_count= (context_count+1)/2;
  650. for(i=0; i<f->plane_count; i++){
  651. PlaneContext * const p= &f->plane[i];
  652. p->context_count= context_count;
  653. if(f->ac){
  654. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  655. }else{
  656. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  657. }
  658. }
  659. return 0;
  660. }
  661. static int decode_init(AVCodecContext *avctx)
  662. {
  663. // FFV1Context *s = avctx->priv_data;
  664. common_init(avctx);
  665. #if 0
  666. switch(s->bitstream_bpp){
  667. case 12:
  668. avctx->pix_fmt = PIX_FMT_YUV420P;
  669. break;
  670. case 16:
  671. avctx->pix_fmt = PIX_FMT_YUV422P;
  672. break;
  673. case 24:
  674. case 32:
  675. if(s->bgr32){
  676. avctx->pix_fmt = PIX_FMT_RGBA32;
  677. }else{
  678. avctx->pix_fmt = PIX_FMT_BGR24;
  679. }
  680. break;
  681. default:
  682. assert(0);
  683. }
  684. #endif
  685. return 0;
  686. }
  687. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){
  688. FFV1Context *f = avctx->priv_data;
  689. CABACContext * const c= &f->c;
  690. const int width= f->width;
  691. const int height= f->height;
  692. AVFrame * const p= &f->picture;
  693. int bytes_read;
  694. AVFrame *picture = data;
  695. *data_size = 0;
  696. /* no supplementary picture */
  697. if (buf_size == 0)
  698. return 0;
  699. ff_init_cabac_decoder(c, buf, buf_size);
  700. ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  701. p->pict_type= FF_I_TYPE; //FIXME I vs. P
  702. if(get_cabac_bypass(c)){
  703. p->key_frame= 1;
  704. read_header(f);
  705. clear_state(f);
  706. }else{
  707. p->key_frame= 0;
  708. }
  709. p->reference= 0;
  710. if(avctx->get_buffer(avctx, p) < 0){
  711. fprintf(stderr, "get_buffer() failed\n");
  712. return -1;
  713. }
  714. if(avctx->debug&FF_DEBUG_PICT_INFO)
  715. printf("keyframe:%d coder:%d\n", p->key_frame, f->ac);
  716. if(!f->ac){
  717. bytes_read = get_cabac_terminate(c);
  718. if(bytes_read ==0) printf("error at end of AC stream\n");
  719. //printf("pos=%d\n", bytes_read);
  720. init_get_bits(&f->gb, buf + bytes_read, buf_size - bytes_read);
  721. } else {
  722. bytes_read = 0; /* avoid warning */
  723. }
  724. if(1){
  725. const int chroma_width = -((-width )>>f->chroma_h_shift);
  726. const int chroma_height= -((-height)>>f->chroma_v_shift);
  727. decode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  728. decode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  729. decode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
  730. }
  731. emms_c();
  732. f->picture_number++;
  733. *picture= *p;
  734. avctx->release_buffer(avctx, p); //FIXME
  735. *data_size = sizeof(AVFrame);
  736. if(f->ac){
  737. bytes_read= get_cabac_terminate(c);
  738. if(bytes_read ==0) printf("error at end of frame\n");
  739. }else{
  740. bytes_read+= (get_bits_count(&f->gb)+7)/8;
  741. }
  742. return bytes_read;
  743. }
  744. static int decode_end(AVCodecContext *avctx)
  745. {
  746. FFV1Context *s = avctx->priv_data;
  747. int i;
  748. if(avctx->get_buffer == avcodec_default_get_buffer){
  749. for(i=0; i<4; i++){
  750. av_freep(&s->picture.base[i]);
  751. s->picture.data[i]= NULL;
  752. }
  753. av_freep(&s->picture.opaque);
  754. }
  755. return 0;
  756. }
  757. AVCodec ffv1_decoder = {
  758. "ffv1",
  759. CODEC_TYPE_VIDEO,
  760. CODEC_ID_FFV1,
  761. sizeof(FFV1Context),
  762. decode_init,
  763. NULL,
  764. decode_end,
  765. decode_frame,
  766. CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  767. NULL
  768. };
  769. #ifdef CONFIG_ENCODERS
  770. AVCodec ffv1_encoder = {
  771. "ffv1",
  772. CODEC_TYPE_VIDEO,
  773. CODEC_ID_FFV1,
  774. sizeof(FFV1Context),
  775. encode_init,
  776. encode_frame,
  777. encode_end,
  778. };
  779. #endif