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.

940 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. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  179. const int gradient= cm[L + T - LT];
  180. return mid_pred(L, gradient, T);
  181. }
  182. static inline int get_context(FFV1Context *f, uint8_t *src, uint8_t *last, uint8_t *last2){
  183. const int LT= last[-1];
  184. const int T= last[ 0];
  185. const int RT= last[ 1];
  186. const int L = src[-1];
  187. if(f->quant_table[3][127]){
  188. const int TT= last2[0];
  189. const int LL= src[-2];
  190. return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF]
  191. +f->quant_table[3][(LL-L) & 0xFF] + f->quant_table[4][(TT-T) & 0xFF];
  192. }else
  193. return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF];
  194. }
  195. /**
  196. * put
  197. */
  198. static inline void put_symbol(CABACContext *c, uint8_t *state, int v, int is_signed, int max_exp){
  199. int i;
  200. if(v){
  201. const int a= ABS(v);
  202. const int e= av_log2(a);
  203. put_cabac(c, state+0, 0);
  204. for(i=0; i<e; i++){
  205. put_cabac(c, state+1+i, 1); //1..8
  206. }
  207. if(e<max_exp){
  208. put_cabac(c, state+1+i, 0); //1..8
  209. for(i=e-1; i>=0; i--){
  210. put_cabac(c, state+16+e+i, (a>>i)&1); //17..29
  211. }
  212. if(is_signed)
  213. put_cabac(c, state+9 + e, v < 0); //9..16
  214. }
  215. }else{
  216. put_cabac(c, state+0, 1);
  217. }
  218. }
  219. static inline int get_symbol(CABACContext *c, uint8_t *state, int is_signed, int max_exp){
  220. if(get_cabac(c, state+0))
  221. return 0;
  222. else{
  223. int i, e;
  224. for(e=0; e<max_exp; e++){
  225. int a= 1<<e;
  226. if(get_cabac(c, state + 1 + e)==0){ // 1..8
  227. for(i=e-1; i>=0; i--){
  228. a += get_cabac(c, state+16+e+i)<<i; //17..29
  229. }
  230. if(is_signed && get_cabac(c, state+9 + e)) //9..16
  231. return -a;
  232. else
  233. return a;
  234. }
  235. }
  236. return -(1<<e);
  237. }
  238. }
  239. static inline void update_vlc_state(VlcState * const state, const int v){
  240. int drift= state->drift;
  241. int count= state->count;
  242. state->error_sum += ABS(v);
  243. drift += v;
  244. if(count == 128){ //FIXME variable
  245. count >>= 1;
  246. drift >>= 1;
  247. state->error_sum >>= 1;
  248. }
  249. count++;
  250. if(drift <= -count){
  251. if(state->bias > -128) state->bias--;
  252. drift += count;
  253. if(drift <= -count)
  254. drift= -count + 1;
  255. }else if(drift > 0){
  256. if(state->bias < 127) state->bias++;
  257. drift -= count;
  258. if(drift > 0)
  259. drift= 0;
  260. }
  261. state->drift= drift;
  262. state->count= count;
  263. }
  264. static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int v){
  265. int i, k, code;
  266. //printf("final: %d ", v);
  267. v = (int8_t)(v - state->bias);
  268. i= state->count;
  269. k=0;
  270. while(i < state->error_sum){ //FIXME optimize
  271. k++;
  272. i += i;
  273. }
  274. #if 0 // JPEG LS
  275. if(k==0 && 2*state->drift <= - state->count) code= v ^ (-1);
  276. else code= v;
  277. #else
  278. code= v ^ ((2*state->drift + state->count)>>31);
  279. #endif
  280. code = -2*code-1;
  281. code^= (code>>31);
  282. //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);
  283. set_ur_golomb(pb, code, k, 8, 8);
  284. update_vlc_state(state, v);
  285. }
  286. static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state){
  287. int k, i, v, ret;
  288. i= state->count;
  289. k=0;
  290. while(i < state->error_sum){ //FIXME optimize
  291. k++;
  292. i += i;
  293. }
  294. v= get_ur_golomb(gb, k, 8, 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 pred_diff_buffer[4][w+6]; //FIXME rema,e
  314. uint8_t *pred_diff[4]= {pred_diff_buffer[0]+3, pred_diff_buffer[1]+3, pred_diff_buffer[2]+3, pred_diff_buffer[3]+3};
  315. int run_index=0;
  316. memset(pred_diff_buffer, 0, sizeof(pred_diff_buffer));
  317. for(y=0; y<h; y++){
  318. uint8_t *temp= pred_diff[0]; //FIXME try a normal buffer
  319. int run_count=0;
  320. int run_mode=0;
  321. pred_diff[0]= pred_diff[1];
  322. pred_diff[1]= pred_diff[2];
  323. pred_diff[2]= pred_diff[3];
  324. pred_diff[3]= temp;
  325. pred_diff[3][-1]= pred_diff[2][0 ];
  326. pred_diff[2][ w]= pred_diff[2][w-1];
  327. for(x=0; x<w; x++){
  328. uint8_t *temp_src= src + x + stride*y;
  329. int diff, context;
  330. context= get_context(s, pred_diff[3]+x, pred_diff[2]+x, pred_diff[1]+x);
  331. diff= temp_src[0] - predict(pred_diff[3]+x, pred_diff[2]+x);
  332. if(context < 0){
  333. context = -context;
  334. diff= -diff;
  335. }
  336. diff= (int8_t)diff;
  337. if(s->ac){
  338. put_symbol(c, p->state[context], diff, 1, 7);
  339. }else{
  340. if(context == 0) run_mode=1;
  341. if(run_mode){
  342. if(diff){
  343. while(run_count >= 1<<log2_run[run_index]){
  344. run_count -= 1<<log2_run[run_index];
  345. run_index++;
  346. put_bits(&s->pb, 1, 1);
  347. }
  348. put_bits(&s->pb, 1 + log2_run[run_index], run_count);
  349. if(run_index) run_index--;
  350. run_count=0;
  351. run_mode=0;
  352. if(diff>0) diff--;
  353. }else{
  354. run_count++;
  355. }
  356. }
  357. // 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));
  358. if(run_mode == 0)
  359. put_vlc_symbol(&s->pb, &p->vlc_state[context], diff);
  360. }
  361. pred_diff[3][x]= temp_src[0];
  362. }
  363. if(run_mode){
  364. while(run_count >= 1<<log2_run[run_index]){
  365. run_count -= 1<<log2_run[run_index];
  366. run_index++;
  367. put_bits(&s->pb, 1, 1);
  368. }
  369. if(run_count)
  370. put_bits(&s->pb, 1, 1);
  371. }
  372. }
  373. }
  374. static void write_quant_table(CABACContext *c, int16_t *quant_table){
  375. int last=0;
  376. int i;
  377. uint8_t state[CONTEXT_SIZE]={0};
  378. for(i=1; i<128 ; i++){
  379. if(quant_table[i] != quant_table[i-1]){
  380. put_symbol(c, state, i-last-1, 0, 7);
  381. last= i;
  382. }
  383. }
  384. put_symbol(c, state, i-last-1, 0, 7);
  385. }
  386. static void write_header(FFV1Context *f){
  387. uint8_t state[CONTEXT_SIZE]={0};
  388. int i;
  389. CABACContext * const c= &f->c;
  390. put_symbol(c, state, f->version, 0, 7);
  391. put_symbol(c, state, f->avctx->coder_type, 0, 7);
  392. put_symbol(c, state, 0, 0, 7); //YUV cs type
  393. put_cabac(c, state, 1); //chroma planes
  394. put_symbol(c, state, f->chroma_h_shift, 0, 7);
  395. put_symbol(c, state, f->chroma_v_shift, 0, 7);
  396. put_cabac(c, state, 0); //no transparency plane
  397. for(i=0; i<5; i++)
  398. write_quant_table(c, f->quant_table[i]);
  399. }
  400. static int common_init(AVCodecContext *avctx){
  401. FFV1Context *s = avctx->priv_data;
  402. int width, height;
  403. s->avctx= avctx;
  404. s->flags= avctx->flags;
  405. dsputil_init(&s->dsp, avctx);
  406. width= s->width= avctx->width;
  407. height= s->height= avctx->height;
  408. assert(width && height);
  409. return 0;
  410. }
  411. static int encode_init(AVCodecContext *avctx)
  412. {
  413. FFV1Context *s = avctx->priv_data;
  414. int i;
  415. common_init(avctx);
  416. s->version=0;
  417. s->ac= avctx->coder_type;
  418. s->plane_count=3;
  419. for(i=0; i<256; i++){
  420. s->quant_table[0][i]= quant11[i];
  421. s->quant_table[1][i]= 11*quant11[i];
  422. if(avctx->context_model==0){
  423. s->quant_table[2][i]= 11*11*quant11[i];
  424. s->quant_table[3][i]=
  425. s->quant_table[4][i]=0;
  426. }else{
  427. s->quant_table[2][i]= 11*11*quant5 [i];
  428. s->quant_table[3][i]= 5*11*11*quant5 [i];
  429. s->quant_table[4][i]= 5*5*11*11*quant5 [i];
  430. }
  431. }
  432. for(i=0; i<s->plane_count; i++){
  433. PlaneContext * const p= &s->plane[i];
  434. if(avctx->context_model==0){
  435. p->context_count= (11*11*11+1)/2;
  436. }else{
  437. p->context_count= (11*11*5*5*5+1)/2;
  438. }
  439. if(s->ac){
  440. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  441. }else{
  442. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  443. }
  444. }
  445. avctx->coded_frame= &s->picture;
  446. switch(avctx->pix_fmt){
  447. case PIX_FMT_YUV444P:
  448. case PIX_FMT_YUV422P:
  449. case PIX_FMT_YUV420P:
  450. case PIX_FMT_YUV411P:
  451. case PIX_FMT_YUV410P:
  452. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  453. break;
  454. default:
  455. fprintf(stderr, "format not supported\n");
  456. return -1;
  457. }
  458. s->picture_number=0;
  459. return 0;
  460. }
  461. static void clear_state(FFV1Context *f){
  462. int i, j;
  463. for(i=0; i<f->plane_count; i++){
  464. PlaneContext *p= &f->plane[i];
  465. p->interlace_bit_state[0]= 0;
  466. p->interlace_bit_state[1]= 0;
  467. for(j=0; j<p->context_count; j++){
  468. if(f->ac){
  469. memset(p->state[j], 0, sizeof(uint8_t)*CONTEXT_SIZE);
  470. p->state[j][7] = 2*62;
  471. }else{
  472. p->vlc_state[j].drift= 0;
  473. p->vlc_state[j].error_sum= 4; //FFMAX((RANGE + 32)/64, 2);
  474. p->vlc_state[j].bias= 0;
  475. p->vlc_state[j].count= 1;
  476. }
  477. }
  478. }
  479. }
  480. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  481. FFV1Context *f = avctx->priv_data;
  482. CABACContext * const c= &f->c;
  483. AVFrame *pict = data;
  484. const int width= f->width;
  485. const int height= f->height;
  486. AVFrame * const p= &f->picture;
  487. int used_count= 0;
  488. if(avctx->strict_std_compliance >= 0){
  489. printf("this codec is under development, files encoded with it wont be decodeable with future versions!!!\n"
  490. "use vstrict=-1 to use it anyway\n");
  491. return -1;
  492. }
  493. ff_init_cabac_encoder(c, buf, buf_size);
  494. ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  495. *p = *pict;
  496. p->pict_type= FF_I_TYPE;
  497. if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){
  498. put_cabac_bypass(c, 1);
  499. p->key_frame= 1;
  500. write_header(f);
  501. clear_state(f);
  502. }else{
  503. put_cabac_bypass(c, 0);
  504. p->key_frame= 0;
  505. }
  506. if(!f->ac){
  507. used_count += put_cabac_terminate(c, 1);
  508. //printf("pos=%d\n", used_count);
  509. init_put_bits(&f->pb, buf + used_count, buf_size - used_count, NULL, NULL);
  510. }
  511. if(1){
  512. const int chroma_width = -((-width )>>f->chroma_h_shift);
  513. const int chroma_height= -((-height)>>f->chroma_v_shift);
  514. encode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  515. encode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  516. encode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 2);
  517. }
  518. emms_c();
  519. f->picture_number++;
  520. if(f->ac){
  521. return put_cabac_terminate(c, 1);
  522. }else{
  523. flush_put_bits(&f->pb); //nicer padding FIXME
  524. return used_count + (get_bit_count(&f->pb)+7)/8;
  525. }
  526. }
  527. static void common_end(FFV1Context *s){
  528. int i;
  529. for(i=0; i<s->plane_count; i++){
  530. PlaneContext *p= &s->plane[i];
  531. av_freep(&p->state);
  532. }
  533. }
  534. static int encode_end(AVCodecContext *avctx)
  535. {
  536. FFV1Context *s = avctx->priv_data;
  537. common_end(s);
  538. return 0;
  539. }
  540. static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  541. PlaneContext * const p= &s->plane[plane_index];
  542. CABACContext * const c= &s->c;
  543. int x,y;
  544. uint8_t pred_diff_buffer[4][w+6];
  545. uint8_t *pred_diff[4]= {pred_diff_buffer[0]+3, pred_diff_buffer[1]+3, pred_diff_buffer[2]+3, pred_diff_buffer[3]+3};
  546. int run_index=0;
  547. memset(pred_diff_buffer, 0, sizeof(pred_diff_buffer));
  548. for(y=0; y<h; y++){
  549. uint8_t *temp= pred_diff[0]; //FIXME try a normal buffer
  550. int run_count=0;
  551. int run_mode=0;
  552. pred_diff[0]= pred_diff[1];
  553. pred_diff[1]= pred_diff[2];
  554. pred_diff[2]= pred_diff[3];
  555. pred_diff[3]= temp;
  556. pred_diff[3][-1]= pred_diff[2][0 ];
  557. pred_diff[2][ w]= pred_diff[2][w-1];
  558. for(x=0; x<w; x++){
  559. uint8_t *temp_src= src + x + stride*y;
  560. int diff, context, sign;
  561. context= get_context(s, pred_diff[3] + x, pred_diff[2] + x, pred_diff[1] + x);
  562. if(context < 0){
  563. context= -context;
  564. sign=1;
  565. }else
  566. sign=0;
  567. if(s->ac)
  568. diff= get_symbol(c, p->state[context], 1, 7);
  569. else{
  570. if(context == 0 && run_mode==0) run_mode=1;
  571. if(run_mode){
  572. if(run_count==0 && run_mode==1){
  573. if(get_bits1(&s->gb)){
  574. run_count = 1<<log2_run[run_index];
  575. if(x + run_count <= w) run_index++;
  576. }else{
  577. if(log2_run[run_index]) run_count = get_bits(&s->gb, log2_run[run_index]);
  578. else run_count=0;
  579. if(run_index) run_index--;
  580. run_mode=2;
  581. }
  582. }
  583. run_count--;
  584. if(run_count < 0){
  585. run_mode=0;
  586. run_count=0;
  587. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context]);
  588. if(diff>=0) diff++;
  589. }else
  590. diff=0;
  591. }else
  592. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context]);
  593. // 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));
  594. }
  595. if(sign) diff= (int8_t)(-diff); //FIXME remove cast
  596. pred_diff[3][x]=
  597. temp_src[0] = predict(pred_diff[3] + x, pred_diff[2] + x) + diff;
  598. assert(diff>= -128 && diff <= 127);
  599. }
  600. }
  601. }
  602. static int read_quant_table(CABACContext *c, int16_t *quant_table, int scale){
  603. int v;
  604. int i=0;
  605. uint8_t state[CONTEXT_SIZE]={0};
  606. for(v=0; i<128 ; v++){
  607. int len= get_symbol(c, state, 0, 7) + 1;
  608. if(len + i > 128) return -1;
  609. while(len--){
  610. quant_table[i] = scale*v;
  611. i++;
  612. //printf("%2d ",v);
  613. //if(i%16==0) printf("\n");
  614. }
  615. }
  616. for(i=1; i<128; i++){
  617. quant_table[256-i]= -quant_table[i];
  618. }
  619. quant_table[128]= -quant_table[127];
  620. return 2*v - 1;
  621. }
  622. static int read_header(FFV1Context *f){
  623. uint8_t state[CONTEXT_SIZE]={0};
  624. int i, context_count;
  625. CABACContext * const c= &f->c;
  626. f->version= get_symbol(c, state, 0, 7);
  627. f->ac= f->avctx->coder_type= get_symbol(c, state, 0, 7);
  628. get_symbol(c, state, 0, 7); //YUV cs type
  629. get_cabac(c, state); //no chroma = false
  630. f->chroma_h_shift= get_symbol(c, state, 0, 7);
  631. f->chroma_v_shift= get_symbol(c, state, 0, 7);
  632. get_cabac(c, state); //transparency plane
  633. f->plane_count= 3;
  634. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  635. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
  636. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
  637. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
  638. case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
  639. case 0x33: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
  640. default:
  641. fprintf(stderr, "format not supported\n");
  642. return -1;
  643. }
  644. //printf("%d %d %d\n", f->chroma_h_shift, f->chroma_v_shift,f->avctx->pix_fmt);
  645. context_count=1;
  646. for(i=0; i<5; i++){
  647. context_count*= read_quant_table(c, f->quant_table[i], context_count);
  648. if(context_count < 0){
  649. printf("read_quant_table error\n");
  650. return -1;
  651. }
  652. }
  653. context_count= (context_count+1)/2;
  654. for(i=0; i<f->plane_count; i++){
  655. PlaneContext * const p= &f->plane[i];
  656. p->context_count= context_count;
  657. if(f->ac){
  658. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  659. }else{
  660. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  661. }
  662. }
  663. return 0;
  664. }
  665. static int decode_init(AVCodecContext *avctx)
  666. {
  667. // FFV1Context *s = avctx->priv_data;
  668. common_init(avctx);
  669. #if 0
  670. switch(s->bitstream_bpp){
  671. case 12:
  672. avctx->pix_fmt = PIX_FMT_YUV420P;
  673. break;
  674. case 16:
  675. avctx->pix_fmt = PIX_FMT_YUV422P;
  676. break;
  677. case 24:
  678. case 32:
  679. if(s->bgr32){
  680. avctx->pix_fmt = PIX_FMT_RGBA32;
  681. }else{
  682. avctx->pix_fmt = PIX_FMT_BGR24;
  683. }
  684. break;
  685. default:
  686. assert(0);
  687. }
  688. #endif
  689. return 0;
  690. }
  691. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){
  692. FFV1Context *f = avctx->priv_data;
  693. CABACContext * const c= &f->c;
  694. const int width= f->width;
  695. const int height= f->height;
  696. AVFrame * const p= &f->picture;
  697. int bytes_read;
  698. AVFrame *picture = data;
  699. *data_size = 0;
  700. /* no supplementary picture */
  701. if (buf_size == 0)
  702. return 0;
  703. ff_init_cabac_decoder(c, buf, buf_size);
  704. ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  705. p->pict_type= FF_I_TYPE; //FIXME I vs. P
  706. if(get_cabac_bypass(c)){
  707. p->key_frame= 1;
  708. read_header(f);
  709. clear_state(f);
  710. }else{
  711. p->key_frame= 0;
  712. }
  713. p->reference= 0;
  714. if(avctx->get_buffer(avctx, p) < 0){
  715. fprintf(stderr, "get_buffer() failed\n");
  716. return -1;
  717. }
  718. if(avctx->debug&FF_DEBUG_PICT_INFO)
  719. printf("keyframe:%d coder:%d\n", p->key_frame, f->ac);
  720. if(!f->ac){
  721. bytes_read = get_cabac_terminate(c);
  722. if(bytes_read ==0) printf("error at end of AC stream\n");
  723. //printf("pos=%d\n", bytes_read);
  724. init_get_bits(&f->gb, buf + bytes_read, buf_size - bytes_read);
  725. }
  726. if(1){
  727. const int chroma_width = -((-width )>>f->chroma_h_shift);
  728. const int chroma_height= -((-height)>>f->chroma_v_shift);
  729. decode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  730. decode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  731. decode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 2);
  732. }
  733. emms_c();
  734. f->picture_number++;
  735. *picture= *p;
  736. avctx->release_buffer(avctx, p); //FIXME
  737. *data_size = sizeof(AVFrame);
  738. if(f->ac){
  739. bytes_read= get_cabac_terminate(c);
  740. if(bytes_read ==0) printf("error at end of frame\n");
  741. }else{
  742. bytes_read+= (get_bits_count(&f->gb)+7)/8;
  743. }
  744. return bytes_read;
  745. }
  746. static int decode_end(AVCodecContext *avctx)
  747. {
  748. FFV1Context *s = avctx->priv_data;
  749. int i;
  750. if(avctx->get_buffer == avcodec_default_get_buffer){
  751. for(i=0; i<4; i++){
  752. av_freep(&s->picture.base[i]);
  753. s->picture.data[i]= NULL;
  754. }
  755. av_freep(&s->picture.opaque);
  756. }
  757. return 0;
  758. }
  759. AVCodec ffv1_decoder = {
  760. "ffv1",
  761. CODEC_TYPE_VIDEO,
  762. CODEC_ID_FFV1,
  763. sizeof(FFV1Context),
  764. decode_init,
  765. NULL,
  766. decode_end,
  767. decode_frame,
  768. CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  769. NULL
  770. };
  771. #ifdef CONFIG_ENCODERS
  772. AVCodec ffv1_encoder = {
  773. "ffv1",
  774. CODEC_TYPE_VIDEO,
  775. CODEC_ID_FFV1,
  776. sizeof(FFV1Context),
  777. encode_init,
  778. encode_frame,
  779. encode_end,
  780. };
  781. #endif