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.

1032 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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, 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2,-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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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,-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 int 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. if(s->ac){
  321. if(c->bytestream_end - c->bytestream < w*20){
  322. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  323. return -1;
  324. }
  325. }else{
  326. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < w*4){
  327. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  328. return -1;
  329. }
  330. }
  331. for(x=0; x<w; x++){
  332. int diff, context;
  333. context= get_context(s, sample[0]+x, sample[1]+x, sample[2]+x);
  334. diff= sample[0][x] - predict(sample[0]+x, sample[1]+x);
  335. if(context < 0){
  336. context = -context;
  337. diff= -diff;
  338. }
  339. diff= fold(diff, bits);
  340. if(s->ac){
  341. put_symbol(c, p->state[context], diff, 1);
  342. }else{
  343. if(context == 0) run_mode=1;
  344. if(run_mode){
  345. if(diff){
  346. while(run_count >= 1<<log2_run[run_index]){
  347. run_count -= 1<<log2_run[run_index];
  348. run_index++;
  349. put_bits(&s->pb, 1, 1);
  350. }
  351. put_bits(&s->pb, 1 + log2_run[run_index], run_count);
  352. if(run_index) run_index--;
  353. run_count=0;
  354. run_mode=0;
  355. if(diff>0) diff--;
  356. }else{
  357. run_count++;
  358. }
  359. }
  360. // 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));
  361. if(run_mode == 0)
  362. put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
  363. }
  364. }
  365. if(run_mode){
  366. while(run_count >= 1<<log2_run[run_index]){
  367. run_count -= 1<<log2_run[run_index];
  368. run_index++;
  369. put_bits(&s->pb, 1, 1);
  370. }
  371. if(run_count)
  372. put_bits(&s->pb, 1, 1);
  373. }
  374. s->run_index= run_index;
  375. return 0;
  376. }
  377. static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  378. int x,y,i;
  379. const int ring_size= s->avctx->context_model ? 3 : 2;
  380. int_fast16_t sample_buffer[ring_size][w+6], *sample[ring_size];
  381. s->run_index=0;
  382. memset(sample_buffer, 0, sizeof(sample_buffer));
  383. for(y=0; y<h; y++){
  384. for(i=0; i<ring_size; i++)
  385. sample[i]= sample_buffer[(h+i-y)%ring_size]+3;
  386. sample[0][-1]= sample[1][0 ];
  387. sample[1][ w]= sample[1][w-1];
  388. //{START_TIMER
  389. for(x=0; x<w; x++){
  390. sample[0][x]= src[x + stride*y];
  391. }
  392. encode_line(s, w, sample, plane_index, 8);
  393. //STOP_TIMER("encode line")}
  394. }
  395. }
  396. static void encode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  397. int x, y, p, i;
  398. const int ring_size= s->avctx->context_model ? 3 : 2;
  399. int_fast16_t sample_buffer[3][ring_size][w+6], *sample[3][ring_size];
  400. s->run_index=0;
  401. memset(sample_buffer, 0, sizeof(sample_buffer));
  402. for(y=0; y<h; y++){
  403. for(i=0; i<ring_size; i++)
  404. for(p=0; p<3; p++)
  405. sample[p][i]= sample_buffer[p][(h+i-y)%ring_size]+3;
  406. for(x=0; x<w; x++){
  407. int v= src[x + stride*y];
  408. int b= v&0xFF;
  409. int g= (v>>8)&0xFF;
  410. int r= (v>>16)&0xFF;
  411. b -= g;
  412. r -= g;
  413. g += (b + r)>>2;
  414. b += 0x100;
  415. r += 0x100;
  416. // assert(g>=0 && b>=0 && r>=0);
  417. // assert(g<256 && b<512 && r<512);
  418. sample[0][0][x]= g;
  419. sample[1][0][x]= b;
  420. sample[2][0][x]= r;
  421. }
  422. for(p=0; p<3; p++){
  423. sample[p][0][-1]= sample[p][1][0 ];
  424. sample[p][1][ w]= sample[p][1][w-1];
  425. encode_line(s, w, sample[p], FFMIN(p, 1), 9);
  426. }
  427. }
  428. }
  429. static void write_quant_table(RangeCoder *c, int16_t *quant_table){
  430. int last=0;
  431. int i;
  432. uint8_t state[CONTEXT_SIZE];
  433. memset(state, 128, sizeof(state));
  434. for(i=1; i<128 ; i++){
  435. if(quant_table[i] != quant_table[i-1]){
  436. put_symbol(c, state, i-last-1, 0);
  437. last= i;
  438. }
  439. }
  440. put_symbol(c, state, i-last-1, 0);
  441. }
  442. static void write_header(FFV1Context *f){
  443. uint8_t state[CONTEXT_SIZE];
  444. int i;
  445. RangeCoder * const c= &f->c;
  446. memset(state, 128, sizeof(state));
  447. put_symbol(c, state, f->version, 0);
  448. put_symbol(c, state, f->avctx->coder_type, 0);
  449. put_symbol(c, state, f->colorspace, 0); //YUV cs type
  450. put_rac(c, state, 1); //chroma planes
  451. put_symbol(c, state, f->chroma_h_shift, 0);
  452. put_symbol(c, state, f->chroma_v_shift, 0);
  453. put_rac(c, state, 0); //no transparency plane
  454. for(i=0; i<5; i++)
  455. write_quant_table(c, f->quant_table[i]);
  456. }
  457. static int common_init(AVCodecContext *avctx){
  458. FFV1Context *s = avctx->priv_data;
  459. int width, height;
  460. s->avctx= avctx;
  461. s->flags= avctx->flags;
  462. dsputil_init(&s->dsp, avctx);
  463. width= s->width= avctx->width;
  464. height= s->height= avctx->height;
  465. assert(width && height);
  466. return 0;
  467. }
  468. static int encode_init(AVCodecContext *avctx)
  469. {
  470. FFV1Context *s = avctx->priv_data;
  471. int i;
  472. common_init(avctx);
  473. s->version=0;
  474. s->ac= avctx->coder_type;
  475. s->plane_count=2;
  476. for(i=0; i<256; i++){
  477. s->quant_table[0][i]= quant11[i];
  478. s->quant_table[1][i]= 11*quant11[i];
  479. if(avctx->context_model==0){
  480. s->quant_table[2][i]= 11*11*quant11[i];
  481. s->quant_table[3][i]=
  482. s->quant_table[4][i]=0;
  483. }else{
  484. s->quant_table[2][i]= 11*11*quant5 [i];
  485. s->quant_table[3][i]= 5*11*11*quant5 [i];
  486. s->quant_table[4][i]= 5*5*11*11*quant5 [i];
  487. }
  488. }
  489. for(i=0; i<s->plane_count; i++){
  490. PlaneContext * const p= &s->plane[i];
  491. if(avctx->context_model==0){
  492. p->context_count= (11*11*11+1)/2;
  493. }else{
  494. p->context_count= (11*11*5*5*5+1)/2;
  495. }
  496. if(s->ac){
  497. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  498. }else{
  499. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  500. }
  501. }
  502. avctx->coded_frame= &s->picture;
  503. switch(avctx->pix_fmt){
  504. case PIX_FMT_YUV444P:
  505. case PIX_FMT_YUV422P:
  506. case PIX_FMT_YUV420P:
  507. case PIX_FMT_YUV411P:
  508. case PIX_FMT_YUV410P:
  509. s->colorspace= 0;
  510. break;
  511. case PIX_FMT_RGBA32:
  512. s->colorspace= 1;
  513. break;
  514. default:
  515. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  516. return -1;
  517. }
  518. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  519. s->picture_number=0;
  520. return 0;
  521. }
  522. static void clear_state(FFV1Context *f){
  523. int i, j;
  524. for(i=0; i<f->plane_count; i++){
  525. PlaneContext *p= &f->plane[i];
  526. p->interlace_bit_state[0]= 128;
  527. p->interlace_bit_state[1]= 128;
  528. for(j=0; j<p->context_count; j++){
  529. if(f->ac){
  530. memset(p->state[j], 128, sizeof(uint8_t)*CONTEXT_SIZE);
  531. }else{
  532. p->vlc_state[j].drift= 0;
  533. p->vlc_state[j].error_sum= 4; //FFMAX((RANGE + 32)/64, 2);
  534. p->vlc_state[j].bias= 0;
  535. p->vlc_state[j].count= 1;
  536. }
  537. }
  538. }
  539. }
  540. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  541. FFV1Context *f = avctx->priv_data;
  542. RangeCoder * const c= &f->c;
  543. AVFrame *pict = data;
  544. const int width= f->width;
  545. const int height= f->height;
  546. AVFrame * const p= &f->picture;
  547. int used_count= 0;
  548. uint8_t keystate=128;
  549. ff_init_range_encoder(c, buf, buf_size);
  550. // ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  551. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  552. *p = *pict;
  553. p->pict_type= FF_I_TYPE;
  554. if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){
  555. put_rac(c, &keystate, 1);
  556. p->key_frame= 1;
  557. write_header(f);
  558. clear_state(f);
  559. }else{
  560. put_rac(c, &keystate, 0);
  561. p->key_frame= 0;
  562. }
  563. if(!f->ac){
  564. used_count += ff_rac_terminate(c);
  565. //printf("pos=%d\n", used_count);
  566. init_put_bits(&f->pb, buf + used_count, buf_size - used_count);
  567. }
  568. if(f->colorspace==0){
  569. const int chroma_width = -((-width )>>f->chroma_h_shift);
  570. const int chroma_height= -((-height)>>f->chroma_v_shift);
  571. encode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  572. encode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  573. encode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
  574. }else{
  575. encode_rgb_frame(f, (uint32_t*)(p->data[0]), width, height, p->linesize[0]/4);
  576. }
  577. emms_c();
  578. f->picture_number++;
  579. if(f->ac){
  580. return ff_rac_terminate(c);
  581. }else{
  582. flush_put_bits(&f->pb); //nicer padding FIXME
  583. return used_count + (put_bits_count(&f->pb)+7)/8;
  584. }
  585. }
  586. static int common_end(AVCodecContext *avctx){
  587. FFV1Context *s = avctx->priv_data;
  588. int i;
  589. for(i=0; i<s->plane_count; i++){
  590. PlaneContext *p= &s->plane[i];
  591. av_freep(&p->state);
  592. }
  593. return 0;
  594. }
  595. static inline void decode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
  596. PlaneContext * const p= &s->plane[plane_index];
  597. RangeCoder * const c= &s->c;
  598. int x;
  599. int run_count=0;
  600. int run_mode=0;
  601. int run_index= s->run_index;
  602. for(x=0; x<w; x++){
  603. int diff, context, sign;
  604. context= get_context(s, sample[1] + x, sample[0] + x, sample[1] + x);
  605. if(context < 0){
  606. context= -context;
  607. sign=1;
  608. }else
  609. sign=0;
  610. if(s->ac){
  611. diff= get_symbol(c, p->state[context], 1);
  612. }else{
  613. if(context == 0 && run_mode==0) run_mode=1;
  614. if(run_mode){
  615. if(run_count==0 && run_mode==1){
  616. if(get_bits1(&s->gb)){
  617. run_count = 1<<log2_run[run_index];
  618. if(x + run_count <= w) run_index++;
  619. }else{
  620. if(log2_run[run_index]) run_count = get_bits(&s->gb, log2_run[run_index]);
  621. else run_count=0;
  622. if(run_index) run_index--;
  623. run_mode=2;
  624. }
  625. }
  626. run_count--;
  627. if(run_count < 0){
  628. run_mode=0;
  629. run_count=0;
  630. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  631. if(diff>=0) diff++;
  632. }else
  633. diff=0;
  634. }else
  635. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  636. // 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));
  637. }
  638. if(sign) diff= -diff;
  639. sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
  640. }
  641. s->run_index= run_index;
  642. }
  643. static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  644. int x, y;
  645. int_fast16_t sample_buffer[2][w+6];
  646. int_fast16_t *sample[2]= {sample_buffer[0]+3, sample_buffer[1]+3};
  647. s->run_index=0;
  648. memset(sample_buffer, 0, sizeof(sample_buffer));
  649. for(y=0; y<h; y++){
  650. int_fast16_t *temp= sample[0]; //FIXME try a normal buffer
  651. sample[0]= sample[1];
  652. sample[1]= temp;
  653. sample[1][-1]= sample[0][0 ];
  654. sample[0][ w]= sample[0][w-1];
  655. //{START_TIMER
  656. decode_line(s, w, sample, plane_index, 8);
  657. for(x=0; x<w; x++){
  658. src[x + stride*y]= sample[1][x];
  659. }
  660. //STOP_TIMER("decode-line")}
  661. }
  662. }
  663. static void decode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  664. int x, y, p;
  665. int_fast16_t sample_buffer[3][2][w+6];
  666. int_fast16_t *sample[3][2]= {
  667. {sample_buffer[0][0]+3, sample_buffer[0][1]+3},
  668. {sample_buffer[1][0]+3, sample_buffer[1][1]+3},
  669. {sample_buffer[2][0]+3, sample_buffer[2][1]+3}};
  670. s->run_index=0;
  671. memset(sample_buffer, 0, sizeof(sample_buffer));
  672. for(y=0; y<h; y++){
  673. for(p=0; p<3; p++){
  674. int_fast16_t *temp= sample[p][0]; //FIXME try a normal buffer
  675. sample[p][0]= sample[p][1];
  676. sample[p][1]= temp;
  677. sample[p][1][-1]= sample[p][0][0 ];
  678. sample[p][0][ w]= sample[p][0][w-1];
  679. decode_line(s, w, sample[p], FFMIN(p, 1), 9);
  680. }
  681. for(x=0; x<w; x++){
  682. int g= sample[0][1][x];
  683. int b= sample[1][1][x];
  684. int r= sample[2][1][x];
  685. // assert(g>=0 && b>=0 && r>=0);
  686. // assert(g<256 && b<512 && r<512);
  687. b -= 0x100;
  688. r -= 0x100;
  689. g -= (b + r)>>2;
  690. b += g;
  691. r += g;
  692. src[x + stride*y]= b + (g<<8) + (r<<16);
  693. }
  694. }
  695. }
  696. static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){
  697. int v;
  698. int i=0;
  699. uint8_t state[CONTEXT_SIZE];
  700. memset(state, 128, sizeof(state));
  701. for(v=0; i<128 ; v++){
  702. int len= get_symbol(c, state, 0) + 1;
  703. if(len + i > 128) return -1;
  704. while(len--){
  705. quant_table[i] = scale*v;
  706. i++;
  707. //printf("%2d ",v);
  708. //if(i%16==0) printf("\n");
  709. }
  710. }
  711. for(i=1; i<128; i++){
  712. quant_table[256-i]= -quant_table[i];
  713. }
  714. quant_table[128]= -quant_table[127];
  715. return 2*v - 1;
  716. }
  717. static int read_header(FFV1Context *f){
  718. uint8_t state[CONTEXT_SIZE];
  719. int i, context_count;
  720. RangeCoder * const c= &f->c;
  721. memset(state, 128, sizeof(state));
  722. f->version= get_symbol(c, state, 0);
  723. f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
  724. f->colorspace= get_symbol(c, state, 0); //YUV cs type
  725. get_rac(c, state); //no chroma = false
  726. f->chroma_h_shift= get_symbol(c, state, 0);
  727. f->chroma_v_shift= get_symbol(c, state, 0);
  728. get_rac(c, state); //transparency plane
  729. f->plane_count= 2;
  730. if(f->colorspace==0){
  731. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  732. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
  733. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
  734. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
  735. case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
  736. case 0x22: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
  737. default:
  738. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  739. return -1;
  740. }
  741. }else if(f->colorspace==1){
  742. if(f->chroma_h_shift || f->chroma_v_shift){
  743. av_log(f->avctx, AV_LOG_ERROR, "chroma subsampling not supported in this colorspace\n");
  744. return -1;
  745. }
  746. f->avctx->pix_fmt= PIX_FMT_RGBA32;
  747. }else{
  748. av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
  749. return -1;
  750. }
  751. //printf("%d %d %d\n", f->chroma_h_shift, f->chroma_v_shift,f->avctx->pix_fmt);
  752. context_count=1;
  753. for(i=0; i<5; i++){
  754. context_count*= read_quant_table(c, f->quant_table[i], context_count);
  755. if(context_count < 0 || context_count > 32768){
  756. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  757. return -1;
  758. }
  759. }
  760. context_count= (context_count+1)/2;
  761. for(i=0; i<f->plane_count; i++){
  762. PlaneContext * const p= &f->plane[i];
  763. p->context_count= context_count;
  764. if(f->ac){
  765. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  766. }else{
  767. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  768. }
  769. }
  770. return 0;
  771. }
  772. static int decode_init(AVCodecContext *avctx)
  773. {
  774. // FFV1Context *s = avctx->priv_data;
  775. common_init(avctx);
  776. return 0;
  777. }
  778. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){
  779. FFV1Context *f = avctx->priv_data;
  780. RangeCoder * const c= &f->c;
  781. const int width= f->width;
  782. const int height= f->height;
  783. AVFrame * const p= &f->picture;
  784. int bytes_read;
  785. uint8_t keystate= 128;
  786. AVFrame *picture = data;
  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. if(read_header(f) < 0)
  793. return -1;
  794. clear_state(f);
  795. }else{
  796. p->key_frame= 0;
  797. }
  798. if(!f->plane[0].state && !f->plane[0].vlc_state)
  799. return -1;
  800. p->reference= 0;
  801. if(avctx->get_buffer(avctx, p) < 0){
  802. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  803. return -1;
  804. }
  805. if(avctx->debug&FF_DEBUG_PICT_INFO)
  806. av_log(avctx, AV_LOG_ERROR, "keyframe:%d coder:%d\n", p->key_frame, f->ac);
  807. if(!f->ac){
  808. bytes_read = c->bytestream - c->bytestream_start - 1;
  809. if(bytes_read ==0) av_log(avctx, AV_LOG_ERROR, "error at end of AC stream\n"); //FIXME
  810. //printf("pos=%d\n", bytes_read);
  811. init_get_bits(&f->gb, buf + bytes_read, buf_size - bytes_read);
  812. } else {
  813. bytes_read = 0; /* avoid warning */
  814. }
  815. if(f->colorspace==0){
  816. const int chroma_width = -((-width )>>f->chroma_h_shift);
  817. const int chroma_height= -((-height)>>f->chroma_v_shift);
  818. decode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  819. decode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  820. decode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
  821. }else{
  822. decode_rgb_frame(f, (uint32_t*)p->data[0], width, height, p->linesize[0]/4);
  823. }
  824. emms_c();
  825. f->picture_number++;
  826. *picture= *p;
  827. avctx->release_buffer(avctx, p); //FIXME
  828. *data_size = sizeof(AVFrame);
  829. if(f->ac){
  830. bytes_read= c->bytestream - c->bytestream_start - 1;
  831. if(bytes_read ==0) av_log(f->avctx, AV_LOG_ERROR, "error at end of frame\n");
  832. }else{
  833. bytes_read+= (get_bits_count(&f->gb)+7)/8;
  834. }
  835. return bytes_read;
  836. }
  837. AVCodec ffv1_decoder = {
  838. "ffv1",
  839. CODEC_TYPE_VIDEO,
  840. CODEC_ID_FFV1,
  841. sizeof(FFV1Context),
  842. decode_init,
  843. NULL,
  844. common_end,
  845. decode_frame,
  846. CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  847. NULL
  848. };
  849. #ifdef CONFIG_ENCODERS
  850. AVCodec ffv1_encoder = {
  851. "ffv1",
  852. CODEC_TYPE_VIDEO,
  853. CODEC_ID_FFV1,
  854. sizeof(FFV1Context),
  855. encode_init,
  856. encode_frame,
  857. common_end,
  858. };
  859. #endif