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