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.

1061 lines
31KB

  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 "cabac.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,
  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, 4,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, 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. CABACContext 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. /**
  206. * put
  207. */
  208. static inline void put_symbol(CABACContext *c, uint8_t *state, int v, int is_signed, int max_exp){
  209. int i;
  210. if(v){
  211. const int a= ABS(v);
  212. const int e= av_log2(a);
  213. put_cabac(c, state+0, 0);
  214. for(i=0; i<e; i++){
  215. put_cabac(c, state+1+i, 1); //1..8
  216. }
  217. if(e<max_exp){
  218. put_cabac(c, state+1+i, 0); //1..8
  219. for(i=e-1; i>=0; i--){
  220. put_cabac(c, state+16+e+i, (a>>i)&1); //17..29
  221. }
  222. if(is_signed)
  223. put_cabac(c, state+9 + e, v < 0); //9..16
  224. }
  225. }else{
  226. put_cabac(c, state+0, 1);
  227. }
  228. }
  229. static inline int get_symbol(CABACContext *c, uint8_t *state, int is_signed, int max_exp){
  230. if(get_cabac(c, state+0))
  231. return 0;
  232. else{
  233. int i, e;
  234. for(e=0; e<max_exp; e++){
  235. int a= 1<<e;
  236. if(get_cabac(c, state + 1 + e)==0){ // 1..8
  237. for(i=e-1; i>=0; i--){
  238. a += get_cabac(c, state+16+e+i)<<i; //17..29
  239. }
  240. if(is_signed && get_cabac(c, state+9 + e)) //9..16
  241. return -a;
  242. else
  243. return a;
  244. }
  245. }
  246. return -(1<<e);
  247. }
  248. }
  249. static inline void update_vlc_state(VlcState * const state, const int v){
  250. int drift= state->drift;
  251. int count= state->count;
  252. state->error_sum += ABS(v);
  253. drift += v;
  254. if(count == 128){ //FIXME variable
  255. count >>= 1;
  256. drift >>= 1;
  257. state->error_sum >>= 1;
  258. }
  259. count++;
  260. if(drift <= -count){
  261. if(state->bias > -128) state->bias--;
  262. drift += count;
  263. if(drift <= -count)
  264. drift= -count + 1;
  265. }else if(drift > 0){
  266. if(state->bias < 127) state->bias++;
  267. drift -= count;
  268. if(drift > 0)
  269. drift= 0;
  270. }
  271. state->drift= drift;
  272. state->count= count;
  273. }
  274. static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int v, int bits){
  275. int i, k, code;
  276. //printf("final: %d ", v);
  277. v = fold(v - state->bias, bits);
  278. i= state->count;
  279. k=0;
  280. while(i < state->error_sum){ //FIXME optimize
  281. k++;
  282. i += i;
  283. }
  284. assert(k<=8);
  285. #if 0 // JPEG LS
  286. if(k==0 && 2*state->drift <= - state->count) code= v ^ (-1);
  287. else code= v;
  288. #else
  289. code= v ^ ((2*state->drift + state->count)>>31);
  290. #endif
  291. code = -2*code-1;
  292. code^= (code>>31);
  293. //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);
  294. set_ur_golomb(pb, code, k, 12, bits);
  295. update_vlc_state(state, v);
  296. }
  297. static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state, int bits){
  298. int k, i, v, ret;
  299. i= state->count;
  300. k=0;
  301. while(i < state->error_sum){ //FIXME optimize
  302. k++;
  303. i += i;
  304. }
  305. assert(k<=8);
  306. v= get_ur_golomb(gb, k, 12, bits);
  307. //printf("v:%d bias:%d error:%d drift:%d count:%d k:%d", v, state->bias, state->error_sum, state->drift, state->count, k);
  308. v++;
  309. if(v&1) v= (v>>1);
  310. else v= -(v>>1);
  311. #if 0 // JPEG LS
  312. if(k==0 && 2*state->drift <= - state->count) v ^= (-1);
  313. #else
  314. v ^= ((2*state->drift + state->count)>>31);
  315. #endif
  316. ret= fold(v + state->bias, bits);
  317. update_vlc_state(state, v);
  318. //printf("final: %d\n", ret);
  319. return ret;
  320. }
  321. static inline void encode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
  322. PlaneContext * const p= &s->plane[plane_index];
  323. CABACContext * const c= &s->c;
  324. int x;
  325. int run_index= s->run_index;
  326. int run_count=0;
  327. int run_mode=0;
  328. for(x=0; x<w; x++){
  329. int diff, context;
  330. context= get_context(s, sample[1]+x, sample[0]+x, sample[2]+x);
  331. diff= sample[1][x] - predict(sample[1]+x, sample[0]+x);
  332. if(context < 0){
  333. context = -context;
  334. diff= -diff;
  335. }
  336. diff= fold(diff, bits);
  337. if(s->ac){
  338. put_symbol(c, p->state[context], diff, 1, bits-1);
  339. }else{
  340. if(context == 0) run_mode=1;
  341. if(run_mode){
  342. if(diff){
  343. while(run_count >= 1<<log2_run[run_index]){
  344. run_count -= 1<<log2_run[run_index];
  345. run_index++;
  346. put_bits(&s->pb, 1, 1);
  347. }
  348. put_bits(&s->pb, 1 + log2_run[run_index], run_count);
  349. if(run_index) run_index--;
  350. run_count=0;
  351. run_mode=0;
  352. if(diff>0) diff--;
  353. }else{
  354. run_count++;
  355. }
  356. }
  357. // 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));
  358. if(run_mode == 0)
  359. put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
  360. }
  361. }
  362. if(run_mode){
  363. while(run_count >= 1<<log2_run[run_index]){
  364. run_count -= 1<<log2_run[run_index];
  365. run_index++;
  366. put_bits(&s->pb, 1, 1);
  367. }
  368. if(run_count)
  369. put_bits(&s->pb, 1, 1);
  370. }
  371. s->run_index= run_index;
  372. }
  373. static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  374. int x,y;
  375. int_fast16_t sample_buffer[3][w+6];
  376. int_fast16_t *sample[3]= {sample_buffer[0]+3, sample_buffer[1]+3, sample_buffer[2]+3};
  377. s->run_index=0;
  378. memset(sample_buffer, 0, sizeof(sample_buffer));
  379. for(y=0; y<h; y++){
  380. int_fast16_t *temp= sample[0]; //FIXME try a normal buffer
  381. sample[0]= sample[1];
  382. sample[1]= sample[2];
  383. sample[2]= temp;
  384. sample[1][-1]= sample[0][0 ];
  385. sample[0][ w]= sample[0][w-1];
  386. //{START_TIMER
  387. for(x=0; x<w; x++){
  388. sample[1][x]= src[x + stride*y];
  389. }
  390. encode_line(s, w, sample, plane_index, 8);
  391. //STOP_TIMER("encode line")}
  392. }
  393. }
  394. static void encode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  395. int x, y, p;
  396. int_fast16_t sample_buffer[3][2][w+6];
  397. int_fast16_t *sample[3][2]= {
  398. {sample_buffer[0][0]+3, sample_buffer[0][1]+3},
  399. {sample_buffer[1][0]+3, sample_buffer[1][1]+3},
  400. {sample_buffer[2][0]+3, sample_buffer[2][1]+3}};
  401. s->run_index=0;
  402. memset(sample_buffer, 0, sizeof(sample_buffer));
  403. for(y=0; y<h; y++){
  404. for(x=0; x<w; x++){
  405. int v= src[x + stride*y];
  406. int b= v&0xFF;
  407. int g= (v>>8)&0xFF;
  408. int r= (v>>16)&0xFF;
  409. b -= g;
  410. r -= g;
  411. g += (b + r)>>2;
  412. b += 0x100;
  413. r += 0x100;
  414. // assert(g>=0 && b>=0 && r>=0);
  415. // assert(g<256 && b<512 && r<512);
  416. sample[0][0][x]= g;
  417. sample[1][0][x]= b;
  418. sample[2][0][x]= r;
  419. }
  420. for(p=0; p<3; p++){
  421. int_fast16_t *temp= sample[p][0]; //FIXME try a normal buffer
  422. sample[p][0]= sample[p][1];
  423. sample[p][1]= temp;
  424. sample[p][1][-1]= sample[p][0][0 ];
  425. sample[p][0][ w]= sample[p][0][w-1];
  426. encode_line(s, w, sample[p], FFMIN(p, 1), 9);
  427. }
  428. }
  429. }
  430. static void write_quant_table(CABACContext *c, int16_t *quant_table){
  431. int last=0;
  432. int i;
  433. uint8_t state[CONTEXT_SIZE]={0};
  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, 7);
  437. last= i;
  438. }
  439. }
  440. put_symbol(c, state, i-last-1, 0, 7);
  441. }
  442. static void write_header(FFV1Context *f){
  443. uint8_t state[CONTEXT_SIZE]={0};
  444. int i;
  445. CABACContext * const c= &f->c;
  446. put_symbol(c, state, f->version, 0, 7);
  447. put_symbol(c, state, f->avctx->coder_type, 0, 7);
  448. put_symbol(c, state, f->colorspace, 0, 7); //YUV cs type
  449. put_cabac(c, state, 1); //chroma planes
  450. put_symbol(c, state, f->chroma_h_shift, 0, 7);
  451. put_symbol(c, state, f->chroma_v_shift, 0, 7);
  452. put_cabac(c, state, 0); //no transparency plane
  453. for(i=0; i<5; i++)
  454. write_quant_table(c, f->quant_table[i]);
  455. }
  456. static int common_init(AVCodecContext *avctx){
  457. FFV1Context *s = avctx->priv_data;
  458. int width, height;
  459. s->avctx= avctx;
  460. s->flags= avctx->flags;
  461. dsputil_init(&s->dsp, avctx);
  462. width= s->width= avctx->width;
  463. height= s->height= avctx->height;
  464. assert(width && height);
  465. return 0;
  466. }
  467. static int encode_init(AVCodecContext *avctx)
  468. {
  469. FFV1Context *s = avctx->priv_data;
  470. int i;
  471. common_init(avctx);
  472. s->version=0;
  473. s->ac= avctx->coder_type;
  474. s->plane_count=2;
  475. for(i=0; i<256; i++){
  476. s->quant_table[0][i]= quant11[i];
  477. s->quant_table[1][i]= 11*quant11[i];
  478. if(avctx->context_model==0){
  479. s->quant_table[2][i]= 11*11*quant11[i];
  480. s->quant_table[3][i]=
  481. s->quant_table[4][i]=0;
  482. }else{
  483. s->quant_table[2][i]= 11*11*quant5 [i];
  484. s->quant_table[3][i]= 5*11*11*quant5 [i];
  485. s->quant_table[4][i]= 5*5*11*11*quant5 [i];
  486. }
  487. }
  488. for(i=0; i<s->plane_count; i++){
  489. PlaneContext * const p= &s->plane[i];
  490. if(avctx->context_model==0){
  491. p->context_count= (11*11*11+1)/2;
  492. }else{
  493. p->context_count= (11*11*5*5*5+1)/2;
  494. }
  495. if(s->ac){
  496. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  497. }else{
  498. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  499. }
  500. }
  501. avctx->coded_frame= &s->picture;
  502. switch(avctx->pix_fmt){
  503. case PIX_FMT_YUV444P:
  504. case PIX_FMT_YUV422P:
  505. case PIX_FMT_YUV420P:
  506. case PIX_FMT_YUV411P:
  507. case PIX_FMT_YUV410P:
  508. s->colorspace= 0;
  509. break;
  510. case PIX_FMT_RGBA32:
  511. s->colorspace= 1;
  512. break;
  513. default:
  514. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  515. return -1;
  516. }
  517. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  518. s->picture_number=0;
  519. return 0;
  520. }
  521. static void clear_state(FFV1Context *f){
  522. int i, j;
  523. for(i=0; i<f->plane_count; i++){
  524. PlaneContext *p= &f->plane[i];
  525. p->interlace_bit_state[0]= 0;
  526. p->interlace_bit_state[1]= 0;
  527. for(j=0; j<p->context_count; j++){
  528. if(f->ac){
  529. memset(p->state[j], 0, sizeof(uint8_t)*CONTEXT_SIZE);
  530. p->state[j][7] = 2*62;
  531. }else{
  532. p->vlc_state[j].drift= 0;
  533. p->vlc_state[j].error_sum= 4; //FFMAX((RANGE + 32)/64, 2);
  534. p->vlc_state[j].bias= 0;
  535. p->vlc_state[j].count= 1;
  536. }
  537. }
  538. }
  539. }
  540. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  541. FFV1Context *f = avctx->priv_data;
  542. CABACContext * const c= &f->c;
  543. AVFrame *pict = data;
  544. const int width= f->width;
  545. const int height= f->height;
  546. AVFrame * const p= &f->picture;
  547. int used_count= 0;
  548. if(avctx->strict_std_compliance >= 0){
  549. av_log(avctx, AV_LOG_ERROR, "this codec is under development, files encoded with it wont be decodeable with future versions!!!\n"
  550. "use vstrict=-1 to use it anyway\n");
  551. return -1;
  552. }
  553. ff_init_cabac_encoder(c, buf, buf_size);
  554. ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  555. c->lps_state[2] = 1;
  556. c->lps_state[3] = 0;
  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_cabac_bypass(c, 1);
  561. p->key_frame= 1;
  562. write_header(f);
  563. clear_state(f);
  564. }else{
  565. put_cabac_bypass(c, 0);
  566. p->key_frame= 0;
  567. }
  568. if(!f->ac){
  569. used_count += put_cabac_terminate(c, 1);
  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 put_cabac_terminate(c, 1);
  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. CABACContext * 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, bits-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(CABACContext *c, int16_t *quant_table, int scale){
  706. int v;
  707. int i=0;
  708. uint8_t state[CONTEXT_SIZE]={0};
  709. for(v=0; i<128 ; v++){
  710. int len= get_symbol(c, state, 0, 7) + 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]={0};
  727. int i, context_count;
  728. CABACContext * const c= &f->c;
  729. f->version= get_symbol(c, state, 0, 7);
  730. f->ac= f->avctx->coder_type= get_symbol(c, state, 0, 7);
  731. f->colorspace= get_symbol(c, state, 0, 7); //YUV cs type
  732. get_cabac(c, state); //no chroma = false
  733. f->chroma_h_shift= get_symbol(c, state, 0, 7);
  734. f->chroma_v_shift= get_symbol(c, state, 0, 7);
  735. get_cabac(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 0x33: 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_RGBA32;
  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){
  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. CABACContext * 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. AVFrame *picture = data;
  793. *data_size = 0;
  794. /* no supplementary picture */
  795. if (buf_size == 0)
  796. return 0;
  797. ff_init_cabac_decoder(c, buf, buf_size);
  798. ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  799. c->lps_state[2] = 1;
  800. c->lps_state[3] = 0;
  801. p->pict_type= FF_I_TYPE; //FIXME I vs. P
  802. if(get_cabac_bypass(c)){
  803. p->key_frame= 1;
  804. read_header(f);
  805. clear_state(f);
  806. }else{
  807. p->key_frame= 0;
  808. }
  809. p->reference= 0;
  810. if(avctx->get_buffer(avctx, p) < 0){
  811. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  812. return -1;
  813. }
  814. if(avctx->debug&FF_DEBUG_PICT_INFO)
  815. av_log(avctx, AV_LOG_ERROR, "keyframe:%d coder:%d\n", p->key_frame, f->ac);
  816. if(!f->ac){
  817. bytes_read = get_cabac_terminate(c);
  818. if(bytes_read ==0) av_log(avctx, AV_LOG_ERROR, "error at end of AC stream\n");
  819. //printf("pos=%d\n", bytes_read);
  820. init_get_bits(&f->gb, buf + bytes_read, buf_size - bytes_read);
  821. } else {
  822. bytes_read = 0; /* avoid warning */
  823. }
  824. if(f->colorspace==0){
  825. const int chroma_width = -((-width )>>f->chroma_h_shift);
  826. const int chroma_height= -((-height)>>f->chroma_v_shift);
  827. decode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  828. decode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  829. decode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
  830. }else{
  831. decode_rgb_frame(f, (uint32_t*)p->data[0], width, height, p->linesize[0]/4);
  832. }
  833. emms_c();
  834. f->picture_number++;
  835. *picture= *p;
  836. avctx->release_buffer(avctx, p); //FIXME
  837. *data_size = sizeof(AVFrame);
  838. if(f->ac){
  839. bytes_read= get_cabac_terminate(c);
  840. if(bytes_read ==0) av_log(f->avctx, AV_LOG_ERROR, "error at end of frame\n");
  841. }else{
  842. bytes_read+= (get_bits_count(&f->gb)+7)/8;
  843. }
  844. return bytes_read;
  845. }
  846. static int decode_end(AVCodecContext *avctx)
  847. {
  848. FFV1Context *s = avctx->priv_data;
  849. int i;
  850. if(avctx->get_buffer == avcodec_default_get_buffer){
  851. for(i=0; i<4; i++){
  852. av_freep(&s->picture.base[i]);
  853. s->picture.data[i]= NULL;
  854. }
  855. av_freep(&s->picture.opaque);
  856. }
  857. return 0;
  858. }
  859. AVCodec ffv1_decoder = {
  860. "ffv1",
  861. CODEC_TYPE_VIDEO,
  862. CODEC_ID_FFV1,
  863. sizeof(FFV1Context),
  864. decode_init,
  865. NULL,
  866. decode_end,
  867. decode_frame,
  868. CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  869. NULL
  870. };
  871. #ifdef CONFIG_ENCODERS
  872. AVCodec ffv1_encoder = {
  873. "ffv1",
  874. CODEC_TYPE_VIDEO,
  875. CODEC_ID_FFV1,
  876. sizeof(FFV1Context),
  877. encode_init,
  878. encode_frame,
  879. encode_end,
  880. };
  881. #endif