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.

1040 lines
30KB

  1. /*
  2. * FFV1 codec for libavcodec
  3. *
  4. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file ffv1.c
  24. * FF Video Codec 1 (an experimental lossless codec)
  25. */
  26. #include "avcodec.h"
  27. #include "bitstream.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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-2,-2,
  84. -2,-2,-2,-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, 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
  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,-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 av_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= FFABS(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 += FFABS(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. #ifdef CONFIG_ENCODERS
  314. static inline int encode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
  315. PlaneContext * const p= &s->plane[plane_index];
  316. RangeCoder * const c= &s->c;
  317. int x;
  318. int run_index= s->run_index;
  319. int run_count=0;
  320. int run_mode=0;
  321. if(s->ac){
  322. if(c->bytestream_end - c->bytestream < w*20){
  323. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  324. return -1;
  325. }
  326. }else{
  327. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < w*4){
  328. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  329. return -1;
  330. }
  331. }
  332. for(x=0; x<w; x++){
  333. int diff, context;
  334. context= get_context(s, sample[0]+x, sample[1]+x, sample[2]+x);
  335. diff= sample[0][x] - predict(sample[0]+x, sample[1]+x);
  336. if(context < 0){
  337. context = -context;
  338. diff= -diff;
  339. }
  340. diff= fold(diff, bits);
  341. if(s->ac){
  342. put_symbol(c, p->state[context], diff, 1);
  343. }else{
  344. if(context == 0) run_mode=1;
  345. if(run_mode){
  346. if(diff){
  347. while(run_count >= 1<<log2_run[run_index]){
  348. run_count -= 1<<log2_run[run_index];
  349. run_index++;
  350. put_bits(&s->pb, 1, 1);
  351. }
  352. put_bits(&s->pb, 1 + log2_run[run_index], run_count);
  353. if(run_index) run_index--;
  354. run_count=0;
  355. run_mode=0;
  356. if(diff>0) diff--;
  357. }else{
  358. run_count++;
  359. }
  360. }
  361. // 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));
  362. if(run_mode == 0)
  363. put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
  364. }
  365. }
  366. if(run_mode){
  367. while(run_count >= 1<<log2_run[run_index]){
  368. run_count -= 1<<log2_run[run_index];
  369. run_index++;
  370. put_bits(&s->pb, 1, 1);
  371. }
  372. if(run_count)
  373. put_bits(&s->pb, 1, 1);
  374. }
  375. s->run_index= run_index;
  376. return 0;
  377. }
  378. static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  379. int x,y,i;
  380. const int ring_size= s->avctx->context_model ? 3 : 2;
  381. int_fast16_t sample_buffer[ring_size][w+6], *sample[ring_size];
  382. s->run_index=0;
  383. memset(sample_buffer, 0, sizeof(sample_buffer));
  384. for(y=0; y<h; y++){
  385. for(i=0; i<ring_size; i++)
  386. sample[i]= sample_buffer[(h+i-y)%ring_size]+3;
  387. sample[0][-1]= sample[1][0 ];
  388. sample[1][ w]= sample[1][w-1];
  389. //{START_TIMER
  390. for(x=0; x<w; x++){
  391. sample[0][x]= src[x + stride*y];
  392. }
  393. encode_line(s, w, sample, plane_index, 8);
  394. //STOP_TIMER("encode line")}
  395. }
  396. }
  397. static void encode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  398. int x, y, p, i;
  399. const int ring_size= s->avctx->context_model ? 3 : 2;
  400. int_fast16_t sample_buffer[3][ring_size][w+6], *sample[3][ring_size];
  401. s->run_index=0;
  402. memset(sample_buffer, 0, sizeof(sample_buffer));
  403. for(y=0; y<h; y++){
  404. for(i=0; i<ring_size; i++)
  405. for(p=0; p<3; p++)
  406. sample[p][i]= sample_buffer[p][(h+i-y)%ring_size]+3;
  407. for(x=0; x<w; x++){
  408. int v= src[x + stride*y];
  409. int b= v&0xFF;
  410. int g= (v>>8)&0xFF;
  411. int r= (v>>16)&0xFF;
  412. b -= g;
  413. r -= g;
  414. g += (b + r)>>2;
  415. b += 0x100;
  416. r += 0x100;
  417. // assert(g>=0 && b>=0 && r>=0);
  418. // assert(g<256 && b<512 && r<512);
  419. sample[0][0][x]= g;
  420. sample[1][0][x]= b;
  421. sample[2][0][x]= r;
  422. }
  423. for(p=0; p<3; p++){
  424. sample[p][0][-1]= sample[p][1][0 ];
  425. sample[p][1][ w]= sample[p][1][w-1];
  426. encode_line(s, w, sample[p], FFMIN(p, 1), 9);
  427. }
  428. }
  429. }
  430. static void write_quant_table(RangeCoder *c, int16_t *quant_table){
  431. int last=0;
  432. int i;
  433. uint8_t state[CONTEXT_SIZE];
  434. memset(state, 128, sizeof(state));
  435. for(i=1; i<128 ; i++){
  436. if(quant_table[i] != quant_table[i-1]){
  437. put_symbol(c, state, i-last-1, 0);
  438. last= i;
  439. }
  440. }
  441. put_symbol(c, state, i-last-1, 0);
  442. }
  443. static void write_header(FFV1Context *f){
  444. uint8_t state[CONTEXT_SIZE];
  445. int i;
  446. RangeCoder * const c= &f->c;
  447. memset(state, 128, sizeof(state));
  448. put_symbol(c, state, f->version, 0);
  449. put_symbol(c, state, f->avctx->coder_type, 0);
  450. put_symbol(c, state, f->colorspace, 0); //YUV cs type
  451. put_rac(c, state, 1); //chroma planes
  452. put_symbol(c, state, f->chroma_h_shift, 0);
  453. put_symbol(c, state, f->chroma_v_shift, 0);
  454. put_rac(c, state, 0); //no transparency plane
  455. for(i=0; i<5; i++)
  456. write_quant_table(c, f->quant_table[i]);
  457. }
  458. #endif /* CONFIG_ENCODERS */
  459. static int common_init(AVCodecContext *avctx){
  460. FFV1Context *s = avctx->priv_data;
  461. int width, height;
  462. s->avctx= avctx;
  463. s->flags= avctx->flags;
  464. dsputil_init(&s->dsp, avctx);
  465. width= s->width= avctx->width;
  466. height= s->height= avctx->height;
  467. assert(width && height);
  468. return 0;
  469. }
  470. #ifdef CONFIG_ENCODERS
  471. static int encode_init(AVCodecContext *avctx)
  472. {
  473. FFV1Context *s = avctx->priv_data;
  474. int i;
  475. common_init(avctx);
  476. s->version=0;
  477. s->ac= avctx->coder_type;
  478. s->plane_count=2;
  479. for(i=0; i<256; i++){
  480. s->quant_table[0][i]= quant11[i];
  481. s->quant_table[1][i]= 11*quant11[i];
  482. if(avctx->context_model==0){
  483. s->quant_table[2][i]= 11*11*quant11[i];
  484. s->quant_table[3][i]=
  485. s->quant_table[4][i]=0;
  486. }else{
  487. s->quant_table[2][i]= 11*11*quant5 [i];
  488. s->quant_table[3][i]= 5*11*11*quant5 [i];
  489. s->quant_table[4][i]= 5*5*11*11*quant5 [i];
  490. }
  491. }
  492. for(i=0; i<s->plane_count; i++){
  493. PlaneContext * const p= &s->plane[i];
  494. if(avctx->context_model==0){
  495. p->context_count= (11*11*11+1)/2;
  496. }else{
  497. p->context_count= (11*11*5*5*5+1)/2;
  498. }
  499. if(s->ac){
  500. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  501. }else{
  502. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  503. }
  504. }
  505. avctx->coded_frame= &s->picture;
  506. switch(avctx->pix_fmt){
  507. case PIX_FMT_YUV444P:
  508. case PIX_FMT_YUV422P:
  509. case PIX_FMT_YUV420P:
  510. case PIX_FMT_YUV411P:
  511. case PIX_FMT_YUV410P:
  512. s->colorspace= 0;
  513. break;
  514. case PIX_FMT_RGB32:
  515. s->colorspace= 1;
  516. break;
  517. default:
  518. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  519. return -1;
  520. }
  521. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  522. s->picture_number=0;
  523. return 0;
  524. }
  525. #endif /* CONFIG_ENCODERS */
  526. static void clear_state(FFV1Context *f){
  527. int i, j;
  528. for(i=0; i<f->plane_count; i++){
  529. PlaneContext *p= &f->plane[i];
  530. p->interlace_bit_state[0]= 128;
  531. p->interlace_bit_state[1]= 128;
  532. for(j=0; j<p->context_count; j++){
  533. if(f->ac){
  534. memset(p->state[j], 128, sizeof(uint8_t)*CONTEXT_SIZE);
  535. }else{
  536. p->vlc_state[j].drift= 0;
  537. p->vlc_state[j].error_sum= 4; //FFMAX((RANGE + 32)/64, 2);
  538. p->vlc_state[j].bias= 0;
  539. p->vlc_state[j].count= 1;
  540. }
  541. }
  542. }
  543. }
  544. #ifdef CONFIG_ENCODERS
  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. #endif /* CONFIG_ENCODERS */
  592. static int common_end(AVCodecContext *avctx){
  593. FFV1Context *s = avctx->priv_data;
  594. int i;
  595. for(i=0; i<s->plane_count; i++){
  596. PlaneContext *p= &s->plane[i];
  597. av_freep(&p->state);
  598. av_freep(&p->vlc_state);
  599. }
  600. return 0;
  601. }
  602. static inline void decode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
  603. PlaneContext * const p= &s->plane[plane_index];
  604. RangeCoder * const c= &s->c;
  605. int x;
  606. int run_count=0;
  607. int run_mode=0;
  608. int run_index= s->run_index;
  609. for(x=0; x<w; x++){
  610. int diff, context, sign;
  611. context= get_context(s, sample[1] + x, sample[0] + x, sample[1] + x);
  612. if(context < 0){
  613. context= -context;
  614. sign=1;
  615. }else
  616. sign=0;
  617. if(s->ac){
  618. diff= get_symbol(c, p->state[context], 1);
  619. }else{
  620. if(context == 0 && run_mode==0) run_mode=1;
  621. if(run_mode){
  622. if(run_count==0 && run_mode==1){
  623. if(get_bits1(&s->gb)){
  624. run_count = 1<<log2_run[run_index];
  625. if(x + run_count <= w) run_index++;
  626. }else{
  627. if(log2_run[run_index]) run_count = get_bits(&s->gb, log2_run[run_index]);
  628. else run_count=0;
  629. if(run_index) run_index--;
  630. run_mode=2;
  631. }
  632. }
  633. run_count--;
  634. if(run_count < 0){
  635. run_mode=0;
  636. run_count=0;
  637. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  638. if(diff>=0) diff++;
  639. }else
  640. diff=0;
  641. }else
  642. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  643. // 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));
  644. }
  645. if(sign) diff= -diff;
  646. sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
  647. }
  648. s->run_index= run_index;
  649. }
  650. static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  651. int x, y;
  652. int_fast16_t sample_buffer[2][w+6];
  653. int_fast16_t *sample[2]= {sample_buffer[0]+3, sample_buffer[1]+3};
  654. s->run_index=0;
  655. memset(sample_buffer, 0, sizeof(sample_buffer));
  656. for(y=0; y<h; y++){
  657. int_fast16_t *temp= sample[0]; //FIXME try a normal buffer
  658. sample[0]= sample[1];
  659. sample[1]= temp;
  660. sample[1][-1]= sample[0][0 ];
  661. sample[0][ w]= sample[0][w-1];
  662. //{START_TIMER
  663. decode_line(s, w, sample, plane_index, 8);
  664. for(x=0; x<w; x++){
  665. src[x + stride*y]= sample[1][x];
  666. }
  667. //STOP_TIMER("decode-line")}
  668. }
  669. }
  670. static void decode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  671. int x, y, p;
  672. int_fast16_t sample_buffer[3][2][w+6];
  673. int_fast16_t *sample[3][2]= {
  674. {sample_buffer[0][0]+3, sample_buffer[0][1]+3},
  675. {sample_buffer[1][0]+3, sample_buffer[1][1]+3},
  676. {sample_buffer[2][0]+3, sample_buffer[2][1]+3}};
  677. s->run_index=0;
  678. memset(sample_buffer, 0, sizeof(sample_buffer));
  679. for(y=0; y<h; y++){
  680. for(p=0; p<3; p++){
  681. int_fast16_t *temp= sample[p][0]; //FIXME try a normal buffer
  682. sample[p][0]= sample[p][1];
  683. sample[p][1]= temp;
  684. sample[p][1][-1]= sample[p][0][0 ];
  685. sample[p][0][ w]= sample[p][0][w-1];
  686. decode_line(s, w, sample[p], FFMIN(p, 1), 9);
  687. }
  688. for(x=0; x<w; x++){
  689. int g= sample[0][1][x];
  690. int b= sample[1][1][x];
  691. int r= sample[2][1][x];
  692. // assert(g>=0 && b>=0 && r>=0);
  693. // assert(g<256 && b<512 && r<512);
  694. b -= 0x100;
  695. r -= 0x100;
  696. g -= (b + r)>>2;
  697. b += g;
  698. r += g;
  699. src[x + stride*y]= b + (g<<8) + (r<<16);
  700. }
  701. }
  702. }
  703. static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){
  704. int v;
  705. int i=0;
  706. uint8_t state[CONTEXT_SIZE];
  707. memset(state, 128, sizeof(state));
  708. for(v=0; i<128 ; v++){
  709. int len= get_symbol(c, state, 0) + 1;
  710. if(len + i > 128) return -1;
  711. while(len--){
  712. quant_table[i] = scale*v;
  713. i++;
  714. //printf("%2d ",v);
  715. //if(i%16==0) printf("\n");
  716. }
  717. }
  718. for(i=1; i<128; i++){
  719. quant_table[256-i]= -quant_table[i];
  720. }
  721. quant_table[128]= -quant_table[127];
  722. return 2*v - 1;
  723. }
  724. static int read_header(FFV1Context *f){
  725. uint8_t state[CONTEXT_SIZE];
  726. int i, context_count;
  727. RangeCoder * const c= &f->c;
  728. memset(state, 128, sizeof(state));
  729. f->version= get_symbol(c, state, 0);
  730. f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
  731. f->colorspace= get_symbol(c, state, 0); //YUV cs type
  732. get_rac(c, state); //no chroma = false
  733. f->chroma_h_shift= get_symbol(c, state, 0);
  734. f->chroma_v_shift= get_symbol(c, state, 0);
  735. get_rac(c, state); //transparency plane
  736. f->plane_count= 2;
  737. if(f->colorspace==0){
  738. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  739. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
  740. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
  741. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
  742. case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
  743. case 0x22: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
  744. default:
  745. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  746. return -1;
  747. }
  748. }else if(f->colorspace==1){
  749. if(f->chroma_h_shift || f->chroma_v_shift){
  750. av_log(f->avctx, AV_LOG_ERROR, "chroma subsampling not supported in this colorspace\n");
  751. return -1;
  752. }
  753. f->avctx->pix_fmt= PIX_FMT_RGB32;
  754. }else{
  755. av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
  756. return -1;
  757. }
  758. //printf("%d %d %d\n", f->chroma_h_shift, f->chroma_v_shift,f->avctx->pix_fmt);
  759. context_count=1;
  760. for(i=0; i<5; i++){
  761. context_count*= read_quant_table(c, f->quant_table[i], context_count);
  762. if(context_count < 0 || context_count > 32768){
  763. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  764. return -1;
  765. }
  766. }
  767. context_count= (context_count+1)/2;
  768. for(i=0; i<f->plane_count; i++){
  769. PlaneContext * const p= &f->plane[i];
  770. p->context_count= context_count;
  771. if(f->ac){
  772. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  773. }else{
  774. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  775. }
  776. }
  777. return 0;
  778. }
  779. static int decode_init(AVCodecContext *avctx)
  780. {
  781. // FFV1Context *s = avctx->priv_data;
  782. common_init(avctx);
  783. return 0;
  784. }
  785. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){
  786. FFV1Context *f = avctx->priv_data;
  787. RangeCoder * const c= &f->c;
  788. const int width= f->width;
  789. const int height= f->height;
  790. AVFrame * const p= &f->picture;
  791. int bytes_read;
  792. uint8_t keystate= 128;
  793. AVFrame *picture = data;
  794. ff_init_range_decoder(c, buf, buf_size);
  795. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  796. p->pict_type= FF_I_TYPE; //FIXME I vs. P
  797. if(get_rac(c, &keystate)){
  798. p->key_frame= 1;
  799. if(read_header(f) < 0)
  800. return -1;
  801. clear_state(f);
  802. }else{
  803. p->key_frame= 0;
  804. }
  805. if(!f->plane[0].state && !f->plane[0].vlc_state)
  806. return -1;
  807. p->reference= 0;
  808. if(avctx->get_buffer(avctx, p) < 0){
  809. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  810. return -1;
  811. }
  812. if(avctx->debug&FF_DEBUG_PICT_INFO)
  813. av_log(avctx, AV_LOG_ERROR, "keyframe:%d coder:%d\n", p->key_frame, f->ac);
  814. if(!f->ac){
  815. bytes_read = c->bytestream - c->bytestream_start - 1;
  816. if(bytes_read ==0) av_log(avctx, AV_LOG_ERROR, "error at end of AC stream\n"); //FIXME
  817. //printf("pos=%d\n", bytes_read);
  818. init_get_bits(&f->gb, buf + bytes_read, buf_size - bytes_read);
  819. } else {
  820. bytes_read = 0; /* avoid warning */
  821. }
  822. if(f->colorspace==0){
  823. const int chroma_width = -((-width )>>f->chroma_h_shift);
  824. const int chroma_height= -((-height)>>f->chroma_v_shift);
  825. decode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  826. decode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  827. decode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
  828. }else{
  829. decode_rgb_frame(f, (uint32_t*)p->data[0], width, height, p->linesize[0]/4);
  830. }
  831. emms_c();
  832. f->picture_number++;
  833. *picture= *p;
  834. avctx->release_buffer(avctx, p); //FIXME
  835. *data_size = sizeof(AVFrame);
  836. if(f->ac){
  837. bytes_read= c->bytestream - c->bytestream_start - 1;
  838. if(bytes_read ==0) av_log(f->avctx, AV_LOG_ERROR, "error at end of frame\n");
  839. }else{
  840. bytes_read+= (get_bits_count(&f->gb)+7)/8;
  841. }
  842. return bytes_read;
  843. }
  844. AVCodec ffv1_decoder = {
  845. "ffv1",
  846. CODEC_TYPE_VIDEO,
  847. CODEC_ID_FFV1,
  848. sizeof(FFV1Context),
  849. decode_init,
  850. NULL,
  851. common_end,
  852. decode_frame,
  853. CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  854. NULL
  855. };
  856. #ifdef CONFIG_ENCODERS
  857. AVCodec ffv1_encoder = {
  858. "ffv1",
  859. CODEC_TYPE_VIDEO,
  860. CODEC_ID_FFV1,
  861. sizeof(FFV1Context),
  862. encode_init,
  863. encode_frame,
  864. common_end,
  865. .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV411P, PIX_FMT_YUV410P, PIX_FMT_RGB32, -1},
  866. };
  867. #endif