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.

919 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){
  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. put_cabac_u(c, state+1, e, 7, 6, 1); //1..7
  205. if(e<7){
  206. for(i=e-1; i>=0; i--){
  207. static const int offset[7]= {15+0, 15+0, 15+1, 15+3, 15+6, 15+10, 15+11};
  208. put_cabac(c, state+offset[e]+i, (a>>i)&1); //15..31
  209. }
  210. if(is_signed)
  211. put_cabac(c, state+8 + e, v < 0); //8..14
  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){
  218. int i;
  219. if(get_cabac(c, state+0))
  220. return 0;
  221. else{
  222. const int e= get_cabac_u(c, state+1, 7, 6, 1); //1..7
  223. if(e<7){
  224. int a= 1<<e;
  225. for(i=e-1; i>=0; i--){
  226. static const int offset[7]= {15+0, 15+0, 15+1, 15+3, 15+6, 15+10, 15+11};
  227. a += get_cabac(c, state+offset[e]+i)<<i; //14..31
  228. }
  229. if(is_signed && get_cabac(c, state+8 + e)) //8..14
  230. return -a;
  231. else
  232. return a;
  233. }else
  234. return -128;
  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. #if 0 // JPEG LS
  273. if(k==0 && 2*state->drift <= - state->count) code= v ^ (-1);
  274. else code= v;
  275. #else
  276. code= v ^ ((2*state->drift + state->count)>>31);
  277. #endif
  278. code = -2*code-1;
  279. code^= (code>>31);
  280. //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);
  281. set_ur_golomb(pb, code, k, 8, 8);
  282. update_vlc_state(state, v);
  283. }
  284. static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state){
  285. int k, i, v, ret;
  286. i= state->count;
  287. k=0;
  288. while(i < state->error_sum){ //FIXME optimize
  289. k++;
  290. i += i;
  291. }
  292. v= get_ur_golomb(gb, k, 8, 8);
  293. //printf("v:%d bias:%d error:%d drift:%d count:%d k:%d", v, state->bias, state->error_sum, state->drift, state->count, k);
  294. v++;
  295. if(v&1) v= (v>>1);
  296. else v= -(v>>1);
  297. #if 0 // JPEG LS
  298. if(k==0 && 2*state->drift <= - state->count) v ^= (-1);
  299. #else
  300. v ^= ((2*state->drift + state->count)>>31);
  301. #endif
  302. ret= (int8_t)(v + state->bias);
  303. update_vlc_state(state, v);
  304. //printf("final: %d\n", ret);
  305. return ret;
  306. }
  307. static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  308. PlaneContext * const p= &s->plane[plane_index];
  309. CABACContext * const c= &s->c;
  310. int x,y;
  311. uint8_t pred_diff_buffer[4][w+6]; //FIXME rema,e
  312. 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};
  313. int run_index=0;
  314. memset(pred_diff_buffer, 0, sizeof(pred_diff_buffer));
  315. for(y=0; y<h; y++){
  316. uint8_t *temp= pred_diff[0]; //FIXME try a normal buffer
  317. int run_count=0;
  318. int run_mode=0;
  319. pred_diff[0]= pred_diff[1];
  320. pred_diff[1]= pred_diff[2];
  321. pred_diff[2]= pred_diff[3];
  322. pred_diff[3]= temp;
  323. pred_diff[3][-1]= pred_diff[2][0 ];
  324. pred_diff[2][ w]= pred_diff[2][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, pred_diff[3]+x, pred_diff[2]+x, pred_diff[1]+x);
  329. diff= temp_src[0] - predict(pred_diff[3]+x, pred_diff[2]+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);
  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. pred_diff[3][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);
  379. last= i;
  380. }
  381. }
  382. put_symbol(c, state, i-last-1, 0);
  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);
  389. put_symbol(c, state, f->avctx->coder_type, 0);
  390. put_symbol(c, state, 0, 0); //YUV cs type
  391. put_cabac(c, state, 1); //chroma planes
  392. put_symbol(c, state, f->chroma_h_shift, 0);
  393. put_symbol(c, state, f->chroma_v_shift, 0);
  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=3;
  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], 2);
  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 pred_diff_buffer[4][w+6];
  543. 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};
  544. int run_index=0;
  545. memset(pred_diff_buffer, 0, sizeof(pred_diff_buffer));
  546. for(y=0; y<h; y++){
  547. uint8_t *temp= pred_diff[0]; //FIXME try a normal buffer
  548. int run_count=0;
  549. int run_mode=0;
  550. pred_diff[0]= pred_diff[1];
  551. pred_diff[1]= pred_diff[2];
  552. pred_diff[2]= pred_diff[3];
  553. pred_diff[3]= temp;
  554. pred_diff[3][-1]= pred_diff[2][0 ];
  555. pred_diff[2][ w]= pred_diff[2][w-1];
  556. for(x=0; x<w; x++){
  557. uint8_t *temp_src= src + x + stride*y;
  558. int diff, context, sign;
  559. context= get_context(s, pred_diff[3] + x, pred_diff[2] + x, pred_diff[1] + x);
  560. if(context < 0){
  561. context= -context;
  562. sign=1;
  563. }else
  564. sign=0;
  565. if(s->ac)
  566. diff= get_symbol(c, p->state[context], 1);
  567. else{
  568. if(context == 0 && run_mode==0) run_mode=1;
  569. if(run_mode){
  570. if(run_count==0 && run_mode==1){
  571. if(get_bits1(&s->gb)){
  572. run_count = 1<<log2_run[run_index];
  573. if(x + run_count <= w) run_index++;
  574. }else{
  575. if(log2_run[run_index]) run_count = get_bits(&s->gb, log2_run[run_index]);
  576. else run_count=0;
  577. if(run_index) run_index--;
  578. run_mode=2;
  579. }
  580. }
  581. run_count--;
  582. if(run_count < 0){
  583. run_mode=0;
  584. run_count=0;
  585. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context]);
  586. if(diff>=0) diff++;
  587. }else
  588. diff=0;
  589. }else
  590. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context]);
  591. // 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));
  592. }
  593. if(sign) diff= (int8_t)(-diff); //FIXME remove cast
  594. pred_diff[3][x]=
  595. temp_src[0] = predict(pred_diff[3] + x, pred_diff[2] + x) + diff;
  596. assert(diff>= -128 && diff <= 127);
  597. }
  598. }
  599. }
  600. static int read_quant_table(CABACContext *c, int16_t *quant_table, int scale){
  601. int v;
  602. int i=0;
  603. uint8_t state[CONTEXT_SIZE]={0};
  604. for(v=0; i<128 ; v++){
  605. int len= get_symbol(c, state, 0) + 1;
  606. if(len + i > 128) return -1;
  607. while(len--){
  608. quant_table[i] = scale*v;
  609. i++;
  610. //printf("%2d ",v);
  611. //if(i%16==0) printf("\n");
  612. }
  613. }
  614. for(i=1; i<128; i++){
  615. quant_table[256-i]= -quant_table[i];
  616. }
  617. quant_table[128]= -quant_table[127];
  618. return 2*v - 1;
  619. }
  620. static int read_header(FFV1Context *f){
  621. uint8_t state[CONTEXT_SIZE]={0};
  622. int i, context_count;
  623. CABACContext * const c= &f->c;
  624. f->version= get_symbol(c, state, 0);
  625. f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
  626. get_symbol(c, state, 0); //YUV cs type
  627. get_cabac(c, state); //no chroma = false
  628. f->chroma_h_shift= get_symbol(c, state, 0);
  629. f->chroma_v_shift= get_symbol(c, state, 0);
  630. get_cabac(c, state); //transparency plane
  631. f->plane_count= 3;
  632. context_count=1;
  633. for(i=0; i<5; i++){
  634. context_count*= read_quant_table(c, f->quant_table[i], context_count);
  635. }
  636. context_count= (context_count+1)/2;
  637. for(i=0; i<f->plane_count; i++){
  638. PlaneContext * const p= &f->plane[i];
  639. p->context_count= context_count;
  640. if(f->ac){
  641. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  642. }else{
  643. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  644. }
  645. }
  646. return 0;
  647. }
  648. static int decode_init(AVCodecContext *avctx)
  649. {
  650. // FFV1Context *s = avctx->priv_data;
  651. common_init(avctx);
  652. #if 0
  653. switch(s->bitstream_bpp){
  654. case 12:
  655. avctx->pix_fmt = PIX_FMT_YUV420P;
  656. break;
  657. case 16:
  658. avctx->pix_fmt = PIX_FMT_YUV422P;
  659. break;
  660. case 24:
  661. case 32:
  662. if(s->bgr32){
  663. avctx->pix_fmt = PIX_FMT_RGBA32;
  664. }else{
  665. avctx->pix_fmt = PIX_FMT_BGR24;
  666. }
  667. break;
  668. default:
  669. assert(0);
  670. }
  671. #endif
  672. return 0;
  673. }
  674. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){
  675. FFV1Context *f = avctx->priv_data;
  676. CABACContext * const c= &f->c;
  677. const int width= f->width;
  678. const int height= f->height;
  679. AVFrame * const p= &f->picture;
  680. int bytes_read;
  681. AVFrame *picture = data;
  682. *data_size = 0;
  683. /* no supplementary picture */
  684. if (buf_size == 0)
  685. return 0;
  686. ff_init_cabac_decoder(c, buf, buf_size);
  687. ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  688. p->reference= 0;
  689. if(avctx->get_buffer(avctx, p) < 0){
  690. fprintf(stderr, "get_buffer() failed\n");
  691. return -1;
  692. }
  693. p->pict_type= FF_I_TYPE; //FIXME I vs. P
  694. if(get_cabac_bypass(c)){
  695. p->key_frame= 1;
  696. read_header(f);
  697. clear_state(f);
  698. }else{
  699. p->key_frame= 0;
  700. }
  701. if(avctx->debug&FF_DEBUG_PICT_INFO)
  702. printf("keyframe:%d\n", p->key_frame);
  703. if(!f->ac){
  704. bytes_read = get_cabac_terminate(c);
  705. if(bytes_read ==0) printf("error at end of AC stream\n");
  706. //printf("pos=%d\n", bytes_read);
  707. init_get_bits(&f->gb, buf + bytes_read, buf_size - bytes_read);
  708. }
  709. if(1){
  710. const int chroma_width = -((-width )>>f->chroma_h_shift);
  711. const int chroma_height= -((-height)>>f->chroma_v_shift);
  712. decode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  713. decode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  714. decode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 2);
  715. }
  716. emms_c();
  717. f->picture_number++;
  718. *picture= *p;
  719. avctx->release_buffer(avctx, p); //FIXME
  720. *data_size = sizeof(AVFrame);
  721. if(f->ac){
  722. bytes_read= get_cabac_terminate(c);
  723. if(bytes_read ==0) printf("error at end of frame\n");
  724. }else{
  725. bytes_read+= (get_bits_count(&f->gb)+7)/8;
  726. }
  727. return bytes_read;
  728. }
  729. static int decode_end(AVCodecContext *avctx)
  730. {
  731. FFV1Context *s = avctx->priv_data;
  732. int i;
  733. if(avctx->get_buffer == avcodec_default_get_buffer){
  734. for(i=0; i<4; i++){
  735. av_freep(&s->picture.base[i]);
  736. s->picture.data[i]= NULL;
  737. }
  738. av_freep(&s->picture.opaque);
  739. }
  740. return 0;
  741. }
  742. AVCodec ffv1_decoder = {
  743. "ffv1",
  744. CODEC_TYPE_VIDEO,
  745. CODEC_ID_FFV1,
  746. sizeof(FFV1Context),
  747. decode_init,
  748. NULL,
  749. decode_end,
  750. decode_frame,
  751. CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  752. NULL
  753. };
  754. #ifdef CONFIG_ENCODERS
  755. AVCodec ffv1_encoder = {
  756. "ffv1",
  757. CODEC_TYPE_VIDEO,
  758. CODEC_ID_FFV1,
  759. sizeof(FFV1Context),
  760. encode_init,
  761. encode_frame,
  762. encode_end,
  763. };
  764. #endif