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.

1041 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 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. if(avctx->strict_std_compliance >= 0){
  473. av_log(avctx, AV_LOG_ERROR, "this codec is under development, files encoded with it wont be decodeable with future versions!!!\n"
  474. "use vstrict=-1 / -strict -1 to use it anyway\n");
  475. return -1;
  476. }
  477. common_init(avctx);
  478. s->version=0;
  479. s->ac= avctx->coder_type;
  480. s->plane_count=2;
  481. for(i=0; i<256; i++){
  482. s->quant_table[0][i]= quant11[i];
  483. s->quant_table[1][i]= 11*quant11[i];
  484. if(avctx->context_model==0){
  485. s->quant_table[2][i]= 11*11*quant11[i];
  486. s->quant_table[3][i]=
  487. s->quant_table[4][i]=0;
  488. }else{
  489. s->quant_table[2][i]= 11*11*quant5 [i];
  490. s->quant_table[3][i]= 5*11*11*quant5 [i];
  491. s->quant_table[4][i]= 5*5*11*11*quant5 [i];
  492. }
  493. }
  494. for(i=0; i<s->plane_count; i++){
  495. PlaneContext * const p= &s->plane[i];
  496. if(avctx->context_model==0){
  497. p->context_count= (11*11*11+1)/2;
  498. }else{
  499. p->context_count= (11*11*5*5*5+1)/2;
  500. }
  501. if(s->ac){
  502. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  503. }else{
  504. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  505. }
  506. }
  507. avctx->coded_frame= &s->picture;
  508. switch(avctx->pix_fmt){
  509. case PIX_FMT_YUV444P:
  510. case PIX_FMT_YUV422P:
  511. case PIX_FMT_YUV420P:
  512. case PIX_FMT_YUV411P:
  513. case PIX_FMT_YUV410P:
  514. s->colorspace= 0;
  515. break;
  516. case PIX_FMT_RGBA32:
  517. s->colorspace= 1;
  518. break;
  519. default:
  520. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  521. return -1;
  522. }
  523. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  524. s->picture_number=0;
  525. return 0;
  526. }
  527. static void clear_state(FFV1Context *f){
  528. int i, j;
  529. for(i=0; i<f->plane_count; i++){
  530. PlaneContext *p= &f->plane[i];
  531. p->interlace_bit_state[0]= 128;
  532. p->interlace_bit_state[1]= 128;
  533. for(j=0; j<p->context_count; j++){
  534. if(f->ac){
  535. memset(p->state[j], 128, sizeof(uint8_t)*CONTEXT_SIZE);
  536. }else{
  537. p->vlc_state[j].drift= 0;
  538. p->vlc_state[j].error_sum= 4; //FFMAX((RANGE + 32)/64, 2);
  539. p->vlc_state[j].bias= 0;
  540. p->vlc_state[j].count= 1;
  541. }
  542. }
  543. }
  544. }
  545. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  546. FFV1Context *f = avctx->priv_data;
  547. RangeCoder * const c= &f->c;
  548. AVFrame *pict = data;
  549. const int width= f->width;
  550. const int height= f->height;
  551. AVFrame * const p= &f->picture;
  552. int used_count= 0;
  553. uint8_t keystate=128;
  554. ff_init_range_encoder(c, buf, buf_size);
  555. // ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  556. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  557. *p = *pict;
  558. p->pict_type= FF_I_TYPE;
  559. if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){
  560. put_rac(c, &keystate, 1);
  561. p->key_frame= 1;
  562. write_header(f);
  563. clear_state(f);
  564. }else{
  565. put_rac(c, &keystate, 0);
  566. p->key_frame= 0;
  567. }
  568. if(!f->ac){
  569. used_count += ff_rac_terminate(c);
  570. //printf("pos=%d\n", used_count);
  571. init_put_bits(&f->pb, buf + used_count, buf_size - used_count);
  572. }
  573. if(f->colorspace==0){
  574. const int chroma_width = -((-width )>>f->chroma_h_shift);
  575. const int chroma_height= -((-height)>>f->chroma_v_shift);
  576. encode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  577. encode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  578. encode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
  579. }else{
  580. encode_rgb_frame(f, (uint32_t*)(p->data[0]), width, height, p->linesize[0]/4);
  581. }
  582. emms_c();
  583. f->picture_number++;
  584. if(f->ac){
  585. return ff_rac_terminate(c);
  586. }else{
  587. flush_put_bits(&f->pb); //nicer padding FIXME
  588. return used_count + (put_bits_count(&f->pb)+7)/8;
  589. }
  590. }
  591. static void common_end(FFV1Context *s){
  592. int i;
  593. for(i=0; i<s->plane_count; i++){
  594. PlaneContext *p= &s->plane[i];
  595. av_freep(&p->state);
  596. }
  597. }
  598. static int encode_end(AVCodecContext *avctx)
  599. {
  600. FFV1Context *s = avctx->priv_data;
  601. common_end(s);
  602. return 0;
  603. }
  604. static inline void decode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
  605. PlaneContext * const p= &s->plane[plane_index];
  606. RangeCoder * const c= &s->c;
  607. int x;
  608. int run_count=0;
  609. int run_mode=0;
  610. int run_index= s->run_index;
  611. for(x=0; x<w; x++){
  612. int diff, context, sign;
  613. context= get_context(s, sample[1] + x, sample[0] + x, sample[1] + x);
  614. if(context < 0){
  615. context= -context;
  616. sign=1;
  617. }else
  618. sign=0;
  619. if(s->ac){
  620. diff= get_symbol(c, p->state[context], 1);
  621. }else{
  622. if(context == 0 && run_mode==0) run_mode=1;
  623. if(run_mode){
  624. if(run_count==0 && run_mode==1){
  625. if(get_bits1(&s->gb)){
  626. run_count = 1<<log2_run[run_index];
  627. if(x + run_count <= w) run_index++;
  628. }else{
  629. if(log2_run[run_index]) run_count = get_bits(&s->gb, log2_run[run_index]);
  630. else run_count=0;
  631. if(run_index) run_index--;
  632. run_mode=2;
  633. }
  634. }
  635. run_count--;
  636. if(run_count < 0){
  637. run_mode=0;
  638. run_count=0;
  639. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  640. if(diff>=0) diff++;
  641. }else
  642. diff=0;
  643. }else
  644. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  645. // 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));
  646. }
  647. if(sign) diff= -diff;
  648. sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
  649. }
  650. s->run_index= run_index;
  651. }
  652. static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  653. int x, y;
  654. int_fast16_t sample_buffer[2][w+6];
  655. int_fast16_t *sample[2]= {sample_buffer[0]+3, sample_buffer[1]+3};
  656. s->run_index=0;
  657. memset(sample_buffer, 0, sizeof(sample_buffer));
  658. for(y=0; y<h; y++){
  659. int_fast16_t *temp= sample[0]; //FIXME try a normal buffer
  660. sample[0]= sample[1];
  661. sample[1]= temp;
  662. sample[1][-1]= sample[0][0 ];
  663. sample[0][ w]= sample[0][w-1];
  664. //{START_TIMER
  665. decode_line(s, w, sample, plane_index, 8);
  666. for(x=0; x<w; x++){
  667. src[x + stride*y]= sample[1][x];
  668. }
  669. //STOP_TIMER("decode-line")}
  670. }
  671. }
  672. static void decode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  673. int x, y, p;
  674. int_fast16_t sample_buffer[3][2][w+6];
  675. int_fast16_t *sample[3][2]= {
  676. {sample_buffer[0][0]+3, sample_buffer[0][1]+3},
  677. {sample_buffer[1][0]+3, sample_buffer[1][1]+3},
  678. {sample_buffer[2][0]+3, sample_buffer[2][1]+3}};
  679. s->run_index=0;
  680. memset(sample_buffer, 0, sizeof(sample_buffer));
  681. for(y=0; y<h; y++){
  682. for(p=0; p<3; p++){
  683. int_fast16_t *temp= sample[p][0]; //FIXME try a normal buffer
  684. sample[p][0]= sample[p][1];
  685. sample[p][1]= temp;
  686. sample[p][1][-1]= sample[p][0][0 ];
  687. sample[p][0][ w]= sample[p][0][w-1];
  688. decode_line(s, w, sample[p], FFMIN(p, 1), 9);
  689. }
  690. for(x=0; x<w; x++){
  691. int g= sample[0][1][x];
  692. int b= sample[1][1][x];
  693. int r= sample[2][1][x];
  694. // assert(g>=0 && b>=0 && r>=0);
  695. // assert(g<256 && b<512 && r<512);
  696. b -= 0x100;
  697. r -= 0x100;
  698. g -= (b + r)>>2;
  699. b += g;
  700. r += g;
  701. src[x + stride*y]= b + (g<<8) + (r<<16);
  702. }
  703. }
  704. }
  705. static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){
  706. int v;
  707. int i=0;
  708. uint8_t state[CONTEXT_SIZE];
  709. memset(state, 128, sizeof(state));
  710. for(v=0; i<128 ; v++){
  711. int len= get_symbol(c, state, 0) + 1;
  712. if(len + i > 128) return -1;
  713. while(len--){
  714. quant_table[i] = scale*v;
  715. i++;
  716. //printf("%2d ",v);
  717. //if(i%16==0) printf("\n");
  718. }
  719. }
  720. for(i=1; i<128; i++){
  721. quant_table[256-i]= -quant_table[i];
  722. }
  723. quant_table[128]= -quant_table[127];
  724. return 2*v - 1;
  725. }
  726. static int read_header(FFV1Context *f){
  727. uint8_t state[CONTEXT_SIZE];
  728. int i, context_count;
  729. RangeCoder * const c= &f->c;
  730. memset(state, 128, sizeof(state));
  731. f->version= get_symbol(c, state, 0);
  732. f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
  733. f->colorspace= get_symbol(c, state, 0); //YUV cs type
  734. get_rac(c, state); //no chroma = false
  735. f->chroma_h_shift= get_symbol(c, state, 0);
  736. f->chroma_v_shift= get_symbol(c, state, 0);
  737. get_rac(c, state); //transparency plane
  738. f->plane_count= 2;
  739. if(f->colorspace==0){
  740. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  741. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
  742. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
  743. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
  744. case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
  745. case 0x22: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
  746. default:
  747. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  748. return -1;
  749. }
  750. }else if(f->colorspace==1){
  751. if(f->chroma_h_shift || f->chroma_v_shift){
  752. av_log(f->avctx, AV_LOG_ERROR, "chroma subsampling not supported in this colorspace\n");
  753. return -1;
  754. }
  755. f->avctx->pix_fmt= PIX_FMT_RGBA32;
  756. }else{
  757. av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
  758. return -1;
  759. }
  760. //printf("%d %d %d\n", f->chroma_h_shift, f->chroma_v_shift,f->avctx->pix_fmt);
  761. context_count=1;
  762. for(i=0; i<5; i++){
  763. context_count*= read_quant_table(c, f->quant_table[i], context_count);
  764. if(context_count < 0 || context_count > 32768){
  765. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  766. return -1;
  767. }
  768. }
  769. context_count= (context_count+1)/2;
  770. for(i=0; i<f->plane_count; i++){
  771. PlaneContext * const p= &f->plane[i];
  772. p->context_count= context_count;
  773. if(f->ac){
  774. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  775. }else{
  776. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  777. }
  778. }
  779. return 0;
  780. }
  781. static int decode_init(AVCodecContext *avctx)
  782. {
  783. // FFV1Context *s = avctx->priv_data;
  784. common_init(avctx);
  785. return 0;
  786. }
  787. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){
  788. FFV1Context *f = avctx->priv_data;
  789. RangeCoder * const c= &f->c;
  790. const int width= f->width;
  791. const int height= f->height;
  792. AVFrame * const p= &f->picture;
  793. int bytes_read;
  794. uint8_t keystate= 128;
  795. AVFrame *picture = data;
  796. ff_init_range_decoder(c, buf, buf_size);
  797. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  798. p->pict_type= FF_I_TYPE; //FIXME I vs. P
  799. if(get_rac(c, &keystate)){
  800. p->key_frame= 1;
  801. read_header(f);
  802. clear_state(f);
  803. }else{
  804. p->key_frame= 0;
  805. }
  806. p->reference= 0;
  807. if(avctx->get_buffer(avctx, p) < 0){
  808. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  809. return -1;
  810. }
  811. if(avctx->debug&FF_DEBUG_PICT_INFO)
  812. av_log(avctx, AV_LOG_ERROR, "keyframe:%d coder:%d\n", p->key_frame, f->ac);
  813. if(!f->ac){
  814. bytes_read = c->bytestream - c->bytestream_start - 1;
  815. if(bytes_read ==0) av_log(avctx, AV_LOG_ERROR, "error at end of AC stream\n"); //FIXME
  816. //printf("pos=%d\n", bytes_read);
  817. init_get_bits(&f->gb, buf + bytes_read, buf_size - bytes_read);
  818. } else {
  819. bytes_read = 0; /* avoid warning */
  820. }
  821. if(f->colorspace==0){
  822. const int chroma_width = -((-width )>>f->chroma_h_shift);
  823. const int chroma_height= -((-height)>>f->chroma_v_shift);
  824. decode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  825. decode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  826. decode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
  827. }else{
  828. decode_rgb_frame(f, (uint32_t*)p->data[0], width, height, p->linesize[0]/4);
  829. }
  830. emms_c();
  831. f->picture_number++;
  832. *picture= *p;
  833. avctx->release_buffer(avctx, p); //FIXME
  834. *data_size = sizeof(AVFrame);
  835. if(f->ac){
  836. bytes_read= c->bytestream - c->bytestream_start - 1;
  837. if(bytes_read ==0) av_log(f->avctx, AV_LOG_ERROR, "error at end of frame\n");
  838. }else{
  839. bytes_read+= (get_bits_count(&f->gb)+7)/8;
  840. }
  841. return bytes_read;
  842. }
  843. AVCodec ffv1_decoder = {
  844. "ffv1",
  845. CODEC_TYPE_VIDEO,
  846. CODEC_ID_FFV1,
  847. sizeof(FFV1Context),
  848. decode_init,
  849. NULL,
  850. NULL,
  851. decode_frame,
  852. CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  853. NULL
  854. };
  855. #ifdef CONFIG_ENCODERS
  856. AVCodec ffv1_encoder = {
  857. "ffv1",
  858. CODEC_TYPE_VIDEO,
  859. CODEC_ID_FFV1,
  860. sizeof(FFV1Context),
  861. encode_init,
  862. encode_frame,
  863. encode_end,
  864. };
  865. #endif