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.

1031 lines
30KB

  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 (an experimental lossless codec)
  24. */
  25. #include "common.h"
  26. #include "bitstream.h"
  27. #include "avcodec.h"
  28. #include "dsputil.h"
  29. #include "rangecoder.h"
  30. #include "golomb.h"
  31. #define MAX_PLANES 4
  32. #define CONTEXT_SIZE 32
  33. static const int8_t quant3[256]={
  34. 0, 0, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  85. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,-1,
  86. };
  87. static const int8_t quant9[256]={
  88. 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  89. 3, 3, 3, 3, 3, 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,-4,-4,-4,-4,
  102. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,
  103. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-1,-1,
  104. };
  105. static const int8_t quant11[256]={
  106. 0, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
  107. 4, 4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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,-6,
  136. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-5,
  137. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  138. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  139. -4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,-2,-2,-1,
  140. };
  141. static const uint8_t log2_run[32]={
  142. 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
  143. 4, 4, 5, 5, 6, 6, 7, 7,
  144. 8, 9,10,11,12,13,14,15,
  145. };
  146. typedef struct VlcState{
  147. int16_t drift;
  148. uint16_t error_sum;
  149. int8_t bias;
  150. uint8_t count;
  151. } VlcState;
  152. typedef struct PlaneContext{
  153. int context_count;
  154. uint8_t (*state)[CONTEXT_SIZE];
  155. VlcState *vlc_state;
  156. uint8_t interlace_bit_state[2];
  157. } PlaneContext;
  158. typedef struct FFV1Context{
  159. AVCodecContext *avctx;
  160. RangeCoder c;
  161. GetBitContext gb;
  162. PutBitContext pb;
  163. int version;
  164. int width, height;
  165. int chroma_h_shift, chroma_v_shift;
  166. int flags;
  167. int picture_number;
  168. AVFrame picture;
  169. int plane_count;
  170. int ac; ///< 1-> CABAC 0-> golomb rice
  171. PlaneContext plane[MAX_PLANES];
  172. int16_t quant_table[5][256];
  173. int run_index;
  174. int colorspace;
  175. DSPContext dsp;
  176. }FFV1Context;
  177. static always_inline int fold(int diff, int bits){
  178. if(bits==8)
  179. diff= (int8_t)diff;
  180. else{
  181. diff+= 1<<(bits-1);
  182. diff&=(1<<bits)-1;
  183. diff-= 1<<(bits-1);
  184. }
  185. return diff;
  186. }
  187. static inline int predict(int_fast16_t *src, int_fast16_t *last){
  188. const int LT= last[-1];
  189. const int T= last[ 0];
  190. const int L = src[-1];
  191. return mid_pred(L, L + T - LT, T);
  192. }
  193. static inline int get_context(FFV1Context *f, int_fast16_t *src, int_fast16_t *last, int_fast16_t *last2){
  194. const int LT= last[-1];
  195. const int T= last[ 0];
  196. const int RT= last[ 1];
  197. const int L = src[-1];
  198. if(f->quant_table[3][127]){
  199. const int TT= last2[0];
  200. const int LL= src[-2];
  201. return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF]
  202. +f->quant_table[3][(LL-L) & 0xFF] + f->quant_table[4][(TT-T) & 0xFF];
  203. }else
  204. return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF];
  205. }
  206. static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
  207. int i;
  208. if(v){
  209. const int a= ABS(v);
  210. const int e= av_log2(a);
  211. put_rac(c, state+0, 0);
  212. assert(e<=9);
  213. for(i=0; i<e; i++){
  214. put_rac(c, state+1+i, 1); //1..10
  215. }
  216. put_rac(c, state+1+i, 0);
  217. for(i=e-1; i>=0; i--){
  218. put_rac(c, state+22+i, (a>>i)&1); //22..31
  219. }
  220. if(is_signed)
  221. put_rac(c, state+11 + e, v < 0); //11..21
  222. }else{
  223. put_rac(c, state+0, 1);
  224. }
  225. }
  226. static inline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
  227. if(get_rac(c, state+0))
  228. return 0;
  229. else{
  230. int i, e, a;
  231. e= 0;
  232. while(get_rac(c, state+1 + e)){ //1..10
  233. e++;
  234. }
  235. assert(e<=9);
  236. a= 1;
  237. for(i=e-1; i>=0; i--){
  238. a += a + get_rac(c, state+22 + i); //22..31
  239. }
  240. if(is_signed && get_rac(c, state+11 + e)) //11..21
  241. return -a;
  242. else
  243. return a;
  244. }
  245. }
  246. static inline void update_vlc_state(VlcState * const state, const int v){
  247. int drift= state->drift;
  248. int count= state->count;
  249. state->error_sum += ABS(v);
  250. drift += v;
  251. if(count == 128){ //FIXME variable
  252. count >>= 1;
  253. drift >>= 1;
  254. state->error_sum >>= 1;
  255. }
  256. count++;
  257. if(drift <= -count){
  258. if(state->bias > -128) state->bias--;
  259. drift += count;
  260. if(drift <= -count)
  261. drift= -count + 1;
  262. }else if(drift > 0){
  263. if(state->bias < 127) state->bias++;
  264. drift -= count;
  265. if(drift > 0)
  266. drift= 0;
  267. }
  268. state->drift= drift;
  269. state->count= count;
  270. }
  271. static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int v, int bits){
  272. int i, k, code;
  273. //printf("final: %d ", v);
  274. v = fold(v - state->bias, bits);
  275. i= state->count;
  276. k=0;
  277. while(i < state->error_sum){ //FIXME optimize
  278. k++;
  279. i += i;
  280. }
  281. assert(k<=8);
  282. #if 0 // JPEG LS
  283. if(k==0 && 2*state->drift <= - state->count) code= v ^ (-1);
  284. else code= v;
  285. #else
  286. code= v ^ ((2*state->drift + state->count)>>31);
  287. #endif
  288. //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);
  289. set_sr_golomb(pb, code, k, 12, bits);
  290. update_vlc_state(state, v);
  291. }
  292. static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state, int bits){
  293. int k, i, v, ret;
  294. i= state->count;
  295. k=0;
  296. while(i < state->error_sum){ //FIXME optimize
  297. k++;
  298. i += i;
  299. }
  300. assert(k<=8);
  301. v= get_sr_golomb(gb, k, 12, bits);
  302. //printf("v:%d bias:%d error:%d drift:%d count:%d k:%d", v, state->bias, state->error_sum, state->drift, state->count, k);
  303. #if 0 // JPEG LS
  304. if(k==0 && 2*state->drift <= - state->count) v ^= (-1);
  305. #else
  306. v ^= ((2*state->drift + state->count)>>31);
  307. #endif
  308. ret= fold(v + state->bias, bits);
  309. update_vlc_state(state, v);
  310. //printf("final: %d\n", ret);
  311. return ret;
  312. }
  313. static inline void encode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
  314. PlaneContext * const p= &s->plane[plane_index];
  315. RangeCoder * const c= &s->c;
  316. int x;
  317. int run_index= s->run_index;
  318. int run_count=0;
  319. int run_mode=0;
  320. for(x=0; x<w; x++){
  321. int diff, context;
  322. context= get_context(s, sample[0]+x, sample[1]+x, sample[2]+x);
  323. diff= sample[0][x] - predict(sample[0]+x, sample[1]+x);
  324. if(context < 0){
  325. context = -context;
  326. diff= -diff;
  327. }
  328. diff= fold(diff, bits);
  329. if(s->ac){
  330. put_symbol(c, p->state[context], diff, 1);
  331. }else{
  332. if(context == 0) run_mode=1;
  333. if(run_mode){
  334. if(diff){
  335. while(run_count >= 1<<log2_run[run_index]){
  336. run_count -= 1<<log2_run[run_index];
  337. run_index++;
  338. put_bits(&s->pb, 1, 1);
  339. }
  340. put_bits(&s->pb, 1 + log2_run[run_index], run_count);
  341. if(run_index) run_index--;
  342. run_count=0;
  343. run_mode=0;
  344. if(diff>0) diff--;
  345. }else{
  346. run_count++;
  347. }
  348. }
  349. // 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));
  350. if(run_mode == 0)
  351. put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
  352. }
  353. }
  354. if(run_mode){
  355. while(run_count >= 1<<log2_run[run_index]){
  356. run_count -= 1<<log2_run[run_index];
  357. run_index++;
  358. put_bits(&s->pb, 1, 1);
  359. }
  360. if(run_count)
  361. put_bits(&s->pb, 1, 1);
  362. }
  363. s->run_index= run_index;
  364. }
  365. static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  366. int x,y,i;
  367. const int ring_size= s->avctx->context_model ? 3 : 2;
  368. int_fast16_t sample_buffer[ring_size][w+6], *sample[ring_size];
  369. s->run_index=0;
  370. memset(sample_buffer, 0, sizeof(sample_buffer));
  371. for(y=0; y<h; y++){
  372. for(i=0; i<ring_size; i++)
  373. sample[i]= sample_buffer[(h+i-y)%ring_size]+3;
  374. sample[0][-1]= sample[1][0 ];
  375. sample[1][ w]= sample[1][w-1];
  376. //{START_TIMER
  377. for(x=0; x<w; x++){
  378. sample[0][x]= src[x + stride*y];
  379. }
  380. encode_line(s, w, sample, plane_index, 8);
  381. //STOP_TIMER("encode line")}
  382. }
  383. }
  384. static void encode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  385. int x, y, p, i;
  386. const int ring_size= s->avctx->context_model ? 3 : 2;
  387. int_fast16_t sample_buffer[3][ring_size][w+6], *sample[3][ring_size];
  388. s->run_index=0;
  389. memset(sample_buffer, 0, sizeof(sample_buffer));
  390. for(y=0; y<h; y++){
  391. for(i=0; i<ring_size; i++)
  392. for(p=0; p<3; p++)
  393. sample[p][i]= sample_buffer[p][(h+i-y)%ring_size]+3;
  394. for(x=0; x<w; x++){
  395. int v= src[x + stride*y];
  396. int b= v&0xFF;
  397. int g= (v>>8)&0xFF;
  398. int r= (v>>16)&0xFF;
  399. b -= g;
  400. r -= g;
  401. g += (b + r)>>2;
  402. b += 0x100;
  403. r += 0x100;
  404. // assert(g>=0 && b>=0 && r>=0);
  405. // assert(g<256 && b<512 && r<512);
  406. sample[0][0][x]= g;
  407. sample[1][0][x]= b;
  408. sample[2][0][x]= r;
  409. }
  410. for(p=0; p<3; p++){
  411. sample[p][0][-1]= sample[p][1][0 ];
  412. sample[p][1][ w]= sample[p][1][w-1];
  413. encode_line(s, w, sample[p], FFMIN(p, 1), 9);
  414. }
  415. }
  416. }
  417. static void write_quant_table(RangeCoder *c, int16_t *quant_table){
  418. int last=0;
  419. int i;
  420. uint8_t state[CONTEXT_SIZE];
  421. memset(state, 128, sizeof(state));
  422. for(i=1; i<128 ; i++){
  423. if(quant_table[i] != quant_table[i-1]){
  424. put_symbol(c, state, i-last-1, 0);
  425. last= i;
  426. }
  427. }
  428. put_symbol(c, state, i-last-1, 0);
  429. }
  430. static void write_header(FFV1Context *f){
  431. uint8_t state[CONTEXT_SIZE];
  432. int i;
  433. RangeCoder * const c= &f->c;
  434. memset(state, 128, sizeof(state));
  435. put_symbol(c, state, f->version, 0);
  436. put_symbol(c, state, f->avctx->coder_type, 0);
  437. put_symbol(c, state, f->colorspace, 0); //YUV cs type
  438. put_rac(c, state, 1); //chroma planes
  439. put_symbol(c, state, f->chroma_h_shift, 0);
  440. put_symbol(c, state, f->chroma_v_shift, 0);
  441. put_rac(c, state, 0); //no transparency plane
  442. for(i=0; i<5; i++)
  443. write_quant_table(c, f->quant_table[i]);
  444. }
  445. static int common_init(AVCodecContext *avctx){
  446. FFV1Context *s = avctx->priv_data;
  447. int width, height;
  448. s->avctx= avctx;
  449. s->flags= avctx->flags;
  450. dsputil_init(&s->dsp, avctx);
  451. width= s->width= avctx->width;
  452. height= s->height= avctx->height;
  453. assert(width && height);
  454. return 0;
  455. }
  456. static int encode_init(AVCodecContext *avctx)
  457. {
  458. FFV1Context *s = avctx->priv_data;
  459. int i;
  460. if(avctx->strict_std_compliance >= 0){
  461. av_log(avctx, AV_LOG_ERROR, "this codec is under development, files encoded with it wont be decodeable with future versions!!!\n"
  462. "use vstrict=-1 to use it anyway\n");
  463. return -1;
  464. }
  465. common_init(avctx);
  466. s->version=0;
  467. s->ac= avctx->coder_type;
  468. s->plane_count=2;
  469. for(i=0; i<256; i++){
  470. s->quant_table[0][i]= quant11[i];
  471. s->quant_table[1][i]= 11*quant11[i];
  472. if(avctx->context_model==0){
  473. s->quant_table[2][i]= 11*11*quant11[i];
  474. s->quant_table[3][i]=
  475. s->quant_table[4][i]=0;
  476. }else{
  477. s->quant_table[2][i]= 11*11*quant5 [i];
  478. s->quant_table[3][i]= 5*11*11*quant5 [i];
  479. s->quant_table[4][i]= 5*5*11*11*quant5 [i];
  480. }
  481. }
  482. for(i=0; i<s->plane_count; i++){
  483. PlaneContext * const p= &s->plane[i];
  484. if(avctx->context_model==0){
  485. p->context_count= (11*11*11+1)/2;
  486. }else{
  487. p->context_count= (11*11*5*5*5+1)/2;
  488. }
  489. if(s->ac){
  490. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  491. }else{
  492. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  493. }
  494. }
  495. avctx->coded_frame= &s->picture;
  496. switch(avctx->pix_fmt){
  497. case PIX_FMT_YUV444P:
  498. case PIX_FMT_YUV422P:
  499. case PIX_FMT_YUV420P:
  500. case PIX_FMT_YUV411P:
  501. case PIX_FMT_YUV410P:
  502. s->colorspace= 0;
  503. break;
  504. case PIX_FMT_RGBA32:
  505. s->colorspace= 1;
  506. break;
  507. default:
  508. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  509. return -1;
  510. }
  511. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  512. s->picture_number=0;
  513. return 0;
  514. }
  515. static void clear_state(FFV1Context *f){
  516. int i, j;
  517. for(i=0; i<f->plane_count; i++){
  518. PlaneContext *p= &f->plane[i];
  519. p->interlace_bit_state[0]= 128;
  520. p->interlace_bit_state[1]= 128;
  521. for(j=0; j<p->context_count; j++){
  522. if(f->ac){
  523. memset(p->state[j], 128, sizeof(uint8_t)*CONTEXT_SIZE);
  524. }else{
  525. p->vlc_state[j].drift= 0;
  526. p->vlc_state[j].error_sum= 4; //FFMAX((RANGE + 32)/64, 2);
  527. p->vlc_state[j].bias= 0;
  528. p->vlc_state[j].count= 1;
  529. }
  530. }
  531. }
  532. }
  533. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  534. FFV1Context *f = avctx->priv_data;
  535. RangeCoder * const c= &f->c;
  536. AVFrame *pict = data;
  537. const int width= f->width;
  538. const int height= f->height;
  539. AVFrame * const p= &f->picture;
  540. int used_count= 0;
  541. uint8_t keystate=128;
  542. ff_init_range_encoder(c, buf, buf_size);
  543. // ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  544. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  545. *p = *pict;
  546. p->pict_type= FF_I_TYPE;
  547. if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){
  548. put_rac(c, &keystate, 1);
  549. p->key_frame= 1;
  550. write_header(f);
  551. clear_state(f);
  552. }else{
  553. put_rac(c, &keystate, 0);
  554. p->key_frame= 0;
  555. }
  556. if(!f->ac){
  557. used_count += ff_rac_terminate(c);
  558. //printf("pos=%d\n", used_count);
  559. init_put_bits(&f->pb, buf + used_count, buf_size - used_count);
  560. }
  561. if(f->colorspace==0){
  562. const int chroma_width = -((-width )>>f->chroma_h_shift);
  563. const int chroma_height= -((-height)>>f->chroma_v_shift);
  564. encode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  565. encode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  566. encode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
  567. }else{
  568. encode_rgb_frame(f, (uint32_t*)(p->data[0]), width, height, p->linesize[0]/4);
  569. }
  570. emms_c();
  571. f->picture_number++;
  572. if(f->ac){
  573. return ff_rac_terminate(c);
  574. }else{
  575. flush_put_bits(&f->pb); //nicer padding FIXME
  576. return used_count + (put_bits_count(&f->pb)+7)/8;
  577. }
  578. }
  579. static void common_end(FFV1Context *s){
  580. int i;
  581. for(i=0; i<s->plane_count; i++){
  582. PlaneContext *p= &s->plane[i];
  583. av_freep(&p->state);
  584. }
  585. }
  586. static int encode_end(AVCodecContext *avctx)
  587. {
  588. FFV1Context *s = avctx->priv_data;
  589. common_end(s);
  590. return 0;
  591. }
  592. static inline void decode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
  593. PlaneContext * const p= &s->plane[plane_index];
  594. RangeCoder * const c= &s->c;
  595. int x;
  596. int run_count=0;
  597. int run_mode=0;
  598. int run_index= s->run_index;
  599. for(x=0; x<w; x++){
  600. int diff, context, sign;
  601. context= get_context(s, sample[1] + x, sample[0] + x, sample[1] + x);
  602. if(context < 0){
  603. context= -context;
  604. sign=1;
  605. }else
  606. sign=0;
  607. if(s->ac){
  608. diff= get_symbol(c, p->state[context], 1);
  609. }else{
  610. if(context == 0 && run_mode==0) run_mode=1;
  611. if(run_mode){
  612. if(run_count==0 && run_mode==1){
  613. if(get_bits1(&s->gb)){
  614. run_count = 1<<log2_run[run_index];
  615. if(x + run_count <= w) run_index++;
  616. }else{
  617. if(log2_run[run_index]) run_count = get_bits(&s->gb, log2_run[run_index]);
  618. else run_count=0;
  619. if(run_index) run_index--;
  620. run_mode=2;
  621. }
  622. }
  623. run_count--;
  624. if(run_count < 0){
  625. run_mode=0;
  626. run_count=0;
  627. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  628. if(diff>=0) diff++;
  629. }else
  630. diff=0;
  631. }else
  632. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  633. // 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));
  634. }
  635. if(sign) diff= -diff;
  636. sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
  637. }
  638. s->run_index= run_index;
  639. }
  640. static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  641. int x, y;
  642. int_fast16_t sample_buffer[2][w+6];
  643. int_fast16_t *sample[2]= {sample_buffer[0]+3, sample_buffer[1]+3};
  644. s->run_index=0;
  645. memset(sample_buffer, 0, sizeof(sample_buffer));
  646. for(y=0; y<h; y++){
  647. int_fast16_t *temp= sample[0]; //FIXME try a normal buffer
  648. sample[0]= sample[1];
  649. sample[1]= temp;
  650. sample[1][-1]= sample[0][0 ];
  651. sample[0][ w]= sample[0][w-1];
  652. //{START_TIMER
  653. decode_line(s, w, sample, plane_index, 8);
  654. for(x=0; x<w; x++){
  655. src[x + stride*y]= sample[1][x];
  656. }
  657. //STOP_TIMER("decode-line")}
  658. }
  659. }
  660. static void decode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  661. int x, y, p;
  662. int_fast16_t sample_buffer[3][2][w+6];
  663. int_fast16_t *sample[3][2]= {
  664. {sample_buffer[0][0]+3, sample_buffer[0][1]+3},
  665. {sample_buffer[1][0]+3, sample_buffer[1][1]+3},
  666. {sample_buffer[2][0]+3, sample_buffer[2][1]+3}};
  667. s->run_index=0;
  668. memset(sample_buffer, 0, sizeof(sample_buffer));
  669. for(y=0; y<h; y++){
  670. for(p=0; p<3; p++){
  671. int_fast16_t *temp= sample[p][0]; //FIXME try a normal buffer
  672. sample[p][0]= sample[p][1];
  673. sample[p][1]= temp;
  674. sample[p][1][-1]= sample[p][0][0 ];
  675. sample[p][0][ w]= sample[p][0][w-1];
  676. decode_line(s, w, sample[p], FFMIN(p, 1), 9);
  677. }
  678. for(x=0; x<w; x++){
  679. int g= sample[0][1][x];
  680. int b= sample[1][1][x];
  681. int r= sample[2][1][x];
  682. // assert(g>=0 && b>=0 && r>=0);
  683. // assert(g<256 && b<512 && r<512);
  684. b -= 0x100;
  685. r -= 0x100;
  686. g -= (b + r)>>2;
  687. b += g;
  688. r += g;
  689. src[x + stride*y]= b + (g<<8) + (r<<16);
  690. }
  691. }
  692. }
  693. static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){
  694. int v;
  695. int i=0;
  696. uint8_t state[CONTEXT_SIZE];
  697. memset(state, 128, sizeof(state));
  698. for(v=0; i<128 ; v++){
  699. int len= get_symbol(c, state, 0) + 1;
  700. if(len + i > 128) return -1;
  701. while(len--){
  702. quant_table[i] = scale*v;
  703. i++;
  704. //printf("%2d ",v);
  705. //if(i%16==0) printf("\n");
  706. }
  707. }
  708. for(i=1; i<128; i++){
  709. quant_table[256-i]= -quant_table[i];
  710. }
  711. quant_table[128]= -quant_table[127];
  712. return 2*v - 1;
  713. }
  714. static int read_header(FFV1Context *f){
  715. uint8_t state[CONTEXT_SIZE];
  716. int i, context_count;
  717. RangeCoder * const c= &f->c;
  718. memset(state, 128, sizeof(state));
  719. f->version= get_symbol(c, state, 0);
  720. f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
  721. f->colorspace= get_symbol(c, state, 0); //YUV cs type
  722. get_rac(c, state); //no chroma = false
  723. f->chroma_h_shift= get_symbol(c, state, 0);
  724. f->chroma_v_shift= get_symbol(c, state, 0);
  725. get_rac(c, state); //transparency plane
  726. f->plane_count= 2;
  727. if(f->colorspace==0){
  728. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  729. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
  730. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
  731. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
  732. case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
  733. case 0x33: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
  734. default:
  735. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  736. return -1;
  737. }
  738. }else if(f->colorspace==1){
  739. if(f->chroma_h_shift || f->chroma_v_shift){
  740. av_log(f->avctx, AV_LOG_ERROR, "chroma subsampling not supported in this colorspace\n");
  741. return -1;
  742. }
  743. f->avctx->pix_fmt= PIX_FMT_RGBA32;
  744. }else{
  745. av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
  746. return -1;
  747. }
  748. //printf("%d %d %d\n", f->chroma_h_shift, f->chroma_v_shift,f->avctx->pix_fmt);
  749. context_count=1;
  750. for(i=0; i<5; i++){
  751. context_count*= read_quant_table(c, f->quant_table[i], context_count);
  752. if(context_count < 0){
  753. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  754. return -1;
  755. }
  756. }
  757. context_count= (context_count+1)/2;
  758. for(i=0; i<f->plane_count; i++){
  759. PlaneContext * const p= &f->plane[i];
  760. p->context_count= context_count;
  761. if(f->ac){
  762. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  763. }else{
  764. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  765. }
  766. }
  767. return 0;
  768. }
  769. static int decode_init(AVCodecContext *avctx)
  770. {
  771. // FFV1Context *s = avctx->priv_data;
  772. common_init(avctx);
  773. return 0;
  774. }
  775. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){
  776. FFV1Context *f = avctx->priv_data;
  777. RangeCoder * const c= &f->c;
  778. const int width= f->width;
  779. const int height= f->height;
  780. AVFrame * const p= &f->picture;
  781. int bytes_read;
  782. uint8_t keystate= 128;
  783. AVFrame *picture = data;
  784. /* no supplementary picture */
  785. if (buf_size == 0)
  786. return 0;
  787. ff_init_range_decoder(c, buf, buf_size);
  788. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  789. p->pict_type= FF_I_TYPE; //FIXME I vs. P
  790. if(get_rac(c, &keystate)){
  791. p->key_frame= 1;
  792. read_header(f);
  793. clear_state(f);
  794. }else{
  795. p->key_frame= 0;
  796. }
  797. p->reference= 0;
  798. if(avctx->get_buffer(avctx, p) < 0){
  799. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  800. return -1;
  801. }
  802. if(avctx->debug&FF_DEBUG_PICT_INFO)
  803. av_log(avctx, AV_LOG_ERROR, "keyframe:%d coder:%d\n", p->key_frame, f->ac);
  804. if(!f->ac){
  805. bytes_read = c->bytestream - c->bytestream_start - 1;
  806. if(bytes_read ==0) av_log(avctx, AV_LOG_ERROR, "error at end of AC stream\n"); //FIXME
  807. //printf("pos=%d\n", bytes_read);
  808. init_get_bits(&f->gb, buf + bytes_read, buf_size - bytes_read);
  809. } else {
  810. bytes_read = 0; /* avoid warning */
  811. }
  812. if(f->colorspace==0){
  813. const int chroma_width = -((-width )>>f->chroma_h_shift);
  814. const int chroma_height= -((-height)>>f->chroma_v_shift);
  815. decode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  816. decode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  817. decode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
  818. }else{
  819. decode_rgb_frame(f, (uint32_t*)p->data[0], width, height, p->linesize[0]/4);
  820. }
  821. emms_c();
  822. f->picture_number++;
  823. *picture= *p;
  824. avctx->release_buffer(avctx, p); //FIXME
  825. *data_size = sizeof(AVFrame);
  826. if(f->ac){
  827. bytes_read= c->bytestream - c->bytestream_start - 1;
  828. if(bytes_read ==0) av_log(f->avctx, AV_LOG_ERROR, "error at end of frame\n");
  829. }else{
  830. bytes_read+= (get_bits_count(&f->gb)+7)/8;
  831. }
  832. return bytes_read;
  833. }
  834. AVCodec ffv1_decoder = {
  835. "ffv1",
  836. CODEC_TYPE_VIDEO,
  837. CODEC_ID_FFV1,
  838. sizeof(FFV1Context),
  839. decode_init,
  840. NULL,
  841. NULL,
  842. decode_frame,
  843. CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  844. NULL
  845. };
  846. #ifdef CONFIG_ENCODERS
  847. AVCodec ffv1_encoder = {
  848. "ffv1",
  849. CODEC_TYPE_VIDEO,
  850. CODEC_ID_FFV1,
  851. sizeof(FFV1Context),
  852. encode_init,
  853. encode_frame,
  854. encode_end,
  855. };
  856. #endif