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.

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