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.

1042 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 libavcodec/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. #include "mathops.h"
  32. #define MAX_PLANES 4
  33. #define CONTEXT_SIZE 32
  34. extern const uint8_t ff_log2_run[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, 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3,-3,-3,-3,-3,-3,-3,-3,-3,-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[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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,-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. typedef struct VlcState{
  144. int16_t drift;
  145. uint16_t error_sum;
  146. int8_t bias;
  147. uint8_t count;
  148. } VlcState;
  149. typedef struct PlaneContext{
  150. int context_count;
  151. uint8_t (*state)[CONTEXT_SIZE];
  152. VlcState *vlc_state;
  153. uint8_t interlace_bit_state[2];
  154. } PlaneContext;
  155. typedef struct FFV1Context{
  156. AVCodecContext *avctx;
  157. RangeCoder c;
  158. GetBitContext gb;
  159. PutBitContext pb;
  160. int version;
  161. int width, height;
  162. int chroma_h_shift, chroma_v_shift;
  163. int flags;
  164. int picture_number;
  165. AVFrame picture;
  166. int plane_count;
  167. int ac; ///< 1-> CABAC 0-> golomb rice
  168. PlaneContext plane[MAX_PLANES];
  169. int16_t quant_table[5][256];
  170. int run_index;
  171. int colorspace;
  172. DSPContext dsp;
  173. }FFV1Context;
  174. static av_always_inline int fold(int diff, int bits){
  175. if(bits==8)
  176. diff= (int8_t)diff;
  177. else{
  178. diff+= 1<<(bits-1);
  179. diff&=(1<<bits)-1;
  180. diff-= 1<<(bits-1);
  181. }
  182. return diff;
  183. }
  184. static inline int predict(int_fast16_t *src, int_fast16_t *last){
  185. const int LT= last[-1];
  186. const int T= last[ 0];
  187. const int L = src[-1];
  188. return mid_pred(L, L + T - LT, T);
  189. }
  190. static inline int get_context(FFV1Context *f, int_fast16_t *src, int_fast16_t *last, int_fast16_t *last2){
  191. const int LT= last[-1];
  192. const int T= last[ 0];
  193. const int RT= last[ 1];
  194. const int L = src[-1];
  195. if(f->quant_table[3][127]){
  196. const int TT= last2[0];
  197. const int LL= src[-2];
  198. return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF]
  199. +f->quant_table[3][(LL-L) & 0xFF] + f->quant_table[4][(TT-T) & 0xFF];
  200. }else
  201. return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF];
  202. }
  203. static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
  204. int i;
  205. if(v){
  206. const int a= FFABS(v);
  207. const int e= av_log2(a);
  208. put_rac(c, state+0, 0);
  209. assert(e<=9);
  210. for(i=0; i<e; i++){
  211. put_rac(c, state+1+i, 1); //1..10
  212. }
  213. put_rac(c, state+1+i, 0);
  214. for(i=e-1; i>=0; i--){
  215. put_rac(c, state+22+i, (a>>i)&1); //22..31
  216. }
  217. if(is_signed)
  218. put_rac(c, state+11 + e, v < 0); //11..21
  219. }else{
  220. put_rac(c, state+0, 1);
  221. }
  222. }
  223. static inline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
  224. if(get_rac(c, state+0))
  225. return 0;
  226. else{
  227. int i, e, a;
  228. e= 0;
  229. while(get_rac(c, state+1 + e)){ //1..10
  230. e++;
  231. }
  232. assert(e<=9);
  233. a= 1;
  234. for(i=e-1; i>=0; i--){
  235. a += a + get_rac(c, state+22 + i); //22..31
  236. }
  237. if(is_signed && get_rac(c, state+11 + e)) //11..21
  238. return -a;
  239. else
  240. return a;
  241. }
  242. }
  243. static inline void update_vlc_state(VlcState * const state, const int v){
  244. int drift= state->drift;
  245. int count= state->count;
  246. state->error_sum += FFABS(v);
  247. drift += v;
  248. if(count == 128){ //FIXME variable
  249. count >>= 1;
  250. drift >>= 1;
  251. state->error_sum >>= 1;
  252. }
  253. count++;
  254. if(drift <= -count){
  255. if(state->bias > -128) state->bias--;
  256. drift += count;
  257. if(drift <= -count)
  258. drift= -count + 1;
  259. }else if(drift > 0){
  260. if(state->bias < 127) state->bias++;
  261. drift -= count;
  262. if(drift > 0)
  263. drift= 0;
  264. }
  265. state->drift= drift;
  266. state->count= count;
  267. }
  268. static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int v, int bits){
  269. int i, k, code;
  270. //printf("final: %d ", v);
  271. v = fold(v - state->bias, bits);
  272. i= state->count;
  273. k=0;
  274. while(i < state->error_sum){ //FIXME optimize
  275. k++;
  276. i += i;
  277. }
  278. assert(k<=8);
  279. #if 0 // JPEG LS
  280. if(k==0 && 2*state->drift <= - state->count) code= v ^ (-1);
  281. else code= v;
  282. #else
  283. code= v ^ ((2*state->drift + state->count)>>31);
  284. #endif
  285. //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);
  286. set_sr_golomb(pb, code, k, 12, bits);
  287. update_vlc_state(state, v);
  288. }
  289. static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state, int bits){
  290. int k, i, v, ret;
  291. i= state->count;
  292. k=0;
  293. while(i < state->error_sum){ //FIXME optimize
  294. k++;
  295. i += i;
  296. }
  297. assert(k<=8);
  298. v= get_sr_golomb(gb, k, 12, bits);
  299. //printf("v:%d bias:%d error:%d drift:%d count:%d k:%d", v, state->bias, state->error_sum, state->drift, state->count, k);
  300. #if 0 // JPEG LS
  301. if(k==0 && 2*state->drift <= - state->count) v ^= (-1);
  302. #else
  303. v ^= ((2*state->drift + state->count)>>31);
  304. #endif
  305. ret= fold(v + state->bias, bits);
  306. update_vlc_state(state, v);
  307. //printf("final: %d\n", ret);
  308. return ret;
  309. }
  310. #if CONFIG_FFV1_ENCODER
  311. static inline int encode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
  312. PlaneContext * const p= &s->plane[plane_index];
  313. RangeCoder * const c= &s->c;
  314. int x;
  315. int run_index= s->run_index;
  316. int run_count=0;
  317. int run_mode=0;
  318. if(s->ac){
  319. if(c->bytestream_end - c->bytestream < w*20){
  320. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  321. return -1;
  322. }
  323. }else{
  324. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < w*4){
  325. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  326. return -1;
  327. }
  328. }
  329. for(x=0; x<w; x++){
  330. int diff, context;
  331. context= get_context(s, sample[0]+x, sample[1]+x, sample[2]+x);
  332. diff= sample[0][x] - predict(sample[0]+x, sample[1]+x);
  333. if(context < 0){
  334. context = -context;
  335. diff= -diff;
  336. }
  337. diff= fold(diff, bits);
  338. if(s->ac){
  339. put_symbol(c, p->state[context], diff, 1);
  340. }else{
  341. if(context == 0) run_mode=1;
  342. if(run_mode){
  343. if(diff){
  344. while(run_count >= 1<<ff_log2_run[run_index]){
  345. run_count -= 1<<ff_log2_run[run_index];
  346. run_index++;
  347. put_bits(&s->pb, 1, 1);
  348. }
  349. put_bits(&s->pb, 1 + ff_log2_run[run_index], run_count);
  350. if(run_index) run_index--;
  351. run_count=0;
  352. run_mode=0;
  353. if(diff>0) diff--;
  354. }else{
  355. run_count++;
  356. }
  357. }
  358. // 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));
  359. if(run_mode == 0)
  360. put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
  361. }
  362. }
  363. if(run_mode){
  364. while(run_count >= 1<<ff_log2_run[run_index]){
  365. run_count -= 1<<ff_log2_run[run_index];
  366. run_index++;
  367. put_bits(&s->pb, 1, 1);
  368. }
  369. if(run_count)
  370. put_bits(&s->pb, 1, 1);
  371. }
  372. s->run_index= run_index;
  373. return 0;
  374. }
  375. static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  376. int x,y,i;
  377. const int ring_size= s->avctx->context_model ? 3 : 2;
  378. int_fast16_t sample_buffer[ring_size][w+6], *sample[ring_size];
  379. s->run_index=0;
  380. memset(sample_buffer, 0, sizeof(sample_buffer));
  381. for(y=0; y<h; y++){
  382. for(i=0; i<ring_size; i++)
  383. sample[i]= sample_buffer[(h+i-y)%ring_size]+3;
  384. sample[0][-1]= sample[1][0 ];
  385. sample[1][ w]= sample[1][w-1];
  386. //{START_TIMER
  387. for(x=0; x<w; x++){
  388. sample[0][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, i;
  396. const int ring_size= s->avctx->context_model ? 3 : 2;
  397. int_fast16_t sample_buffer[3][ring_size][w+6], *sample[3][ring_size];
  398. s->run_index=0;
  399. memset(sample_buffer, 0, sizeof(sample_buffer));
  400. for(y=0; y<h; y++){
  401. for(i=0; i<ring_size; i++)
  402. for(p=0; p<3; p++)
  403. sample[p][i]= sample_buffer[p][(h+i-y)%ring_size]+3;
  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. sample[p][0][-1]= sample[p][1][0 ];
  422. sample[p][1][ w]= sample[p][1][w-1];
  423. encode_line(s, w, sample[p], FFMIN(p, 1), 9);
  424. }
  425. }
  426. }
  427. static void write_quant_table(RangeCoder *c, int16_t *quant_table){
  428. int last=0;
  429. int i;
  430. uint8_t state[CONTEXT_SIZE];
  431. memset(state, 128, sizeof(state));
  432. for(i=1; i<128 ; i++){
  433. if(quant_table[i] != quant_table[i-1]){
  434. put_symbol(c, state, i-last-1, 0);
  435. last= i;
  436. }
  437. }
  438. put_symbol(c, state, i-last-1, 0);
  439. }
  440. static void write_header(FFV1Context *f){
  441. uint8_t state[CONTEXT_SIZE];
  442. int i;
  443. RangeCoder * const c= &f->c;
  444. memset(state, 128, sizeof(state));
  445. put_symbol(c, state, f->version, 0);
  446. put_symbol(c, state, f->avctx->coder_type, 0);
  447. put_symbol(c, state, f->colorspace, 0); //YUV cs type
  448. put_rac(c, state, 1); //chroma planes
  449. put_symbol(c, state, f->chroma_h_shift, 0);
  450. put_symbol(c, state, f->chroma_v_shift, 0);
  451. put_rac(c, state, 0); //no transparency plane
  452. for(i=0; i<5; i++)
  453. write_quant_table(c, f->quant_table[i]);
  454. }
  455. #endif /* CONFIG_FFV1_ENCODER */
  456. static av_cold 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. #if CONFIG_FFV1_ENCODER
  468. static av_cold int encode_init(AVCodecContext *avctx)
  469. {
  470. FFV1Context *s = avctx->priv_data;
  471. int i;
  472. common_init(avctx);
  473. s->version=0;
  474. s->ac= avctx->coder_type;
  475. s->plane_count=2;
  476. for(i=0; i<256; i++){
  477. s->quant_table[0][i]= quant11[i];
  478. s->quant_table[1][i]= 11*quant11[i];
  479. if(avctx->context_model==0){
  480. s->quant_table[2][i]= 11*11*quant11[i];
  481. s->quant_table[3][i]=
  482. s->quant_table[4][i]=0;
  483. }else{
  484. s->quant_table[2][i]= 11*11*quant5 [i];
  485. s->quant_table[3][i]= 5*11*11*quant5 [i];
  486. s->quant_table[4][i]= 5*5*11*11*quant5 [i];
  487. }
  488. }
  489. for(i=0; i<s->plane_count; i++){
  490. PlaneContext * const p= &s->plane[i];
  491. if(avctx->context_model==0){
  492. p->context_count= (11*11*11+1)/2;
  493. }else{
  494. p->context_count= (11*11*5*5*5+1)/2;
  495. }
  496. if(s->ac){
  497. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  498. }else{
  499. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  500. }
  501. }
  502. avctx->coded_frame= &s->picture;
  503. switch(avctx->pix_fmt){
  504. case PIX_FMT_YUV444P:
  505. case PIX_FMT_YUV422P:
  506. case PIX_FMT_YUV420P:
  507. case PIX_FMT_YUV411P:
  508. case PIX_FMT_YUV410P:
  509. s->colorspace= 0;
  510. break;
  511. case PIX_FMT_RGB32:
  512. s->colorspace= 1;
  513. break;
  514. default:
  515. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  516. return -1;
  517. }
  518. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  519. s->picture_number=0;
  520. return 0;
  521. }
  522. #endif /* CONFIG_FFV1_ENCODER */
  523. static void clear_state(FFV1Context *f){
  524. int i, j;
  525. for(i=0; i<f->plane_count; i++){
  526. PlaneContext *p= &f->plane[i];
  527. p->interlace_bit_state[0]= 128;
  528. p->interlace_bit_state[1]= 128;
  529. for(j=0; j<p->context_count; j++){
  530. if(f->ac){
  531. memset(p->state[j], 128, sizeof(uint8_t)*CONTEXT_SIZE);
  532. }else{
  533. p->vlc_state[j].drift= 0;
  534. p->vlc_state[j].error_sum= 4; //FFMAX((RANGE + 32)/64, 2);
  535. p->vlc_state[j].bias= 0;
  536. p->vlc_state[j].count= 1;
  537. }
  538. }
  539. }
  540. }
  541. #if CONFIG_FFV1_ENCODER
  542. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  543. FFV1Context *f = avctx->priv_data;
  544. RangeCoder * const c= &f->c;
  545. AVFrame *pict = data;
  546. const int width= f->width;
  547. const int height= f->height;
  548. AVFrame * const p= &f->picture;
  549. int used_count= 0;
  550. uint8_t keystate=128;
  551. ff_init_range_encoder(c, buf, buf_size);
  552. // ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  553. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  554. *p = *pict;
  555. p->pict_type= FF_I_TYPE;
  556. if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){
  557. put_rac(c, &keystate, 1);
  558. p->key_frame= 1;
  559. write_header(f);
  560. clear_state(f);
  561. }else{
  562. put_rac(c, &keystate, 0);
  563. p->key_frame= 0;
  564. }
  565. if(!f->ac){
  566. used_count += ff_rac_terminate(c);
  567. //printf("pos=%d\n", used_count);
  568. init_put_bits(&f->pb, buf + used_count, buf_size - used_count);
  569. }
  570. if(f->colorspace==0){
  571. const int chroma_width = -((-width )>>f->chroma_h_shift);
  572. const int chroma_height= -((-height)>>f->chroma_v_shift);
  573. encode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  574. encode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  575. encode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
  576. }else{
  577. encode_rgb_frame(f, (uint32_t*)(p->data[0]), width, height, p->linesize[0]/4);
  578. }
  579. emms_c();
  580. f->picture_number++;
  581. if(f->ac){
  582. return ff_rac_terminate(c);
  583. }else{
  584. flush_put_bits(&f->pb); //nicer padding FIXME
  585. return used_count + (put_bits_count(&f->pb)+7)/8;
  586. }
  587. }
  588. #endif /* CONFIG_FFV1_ENCODER */
  589. static av_cold int common_end(AVCodecContext *avctx){
  590. FFV1Context *s = avctx->priv_data;
  591. int i;
  592. for(i=0; i<s->plane_count; i++){
  593. PlaneContext *p= &s->plane[i];
  594. av_freep(&p->state);
  595. av_freep(&p->vlc_state);
  596. }
  597. return 0;
  598. }
  599. static inline void decode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
  600. PlaneContext * const p= &s->plane[plane_index];
  601. RangeCoder * const c= &s->c;
  602. int x;
  603. int run_count=0;
  604. int run_mode=0;
  605. int run_index= s->run_index;
  606. for(x=0; x<w; x++){
  607. int diff, context, sign;
  608. context= get_context(s, sample[1] + x, sample[0] + x, sample[1] + x);
  609. if(context < 0){
  610. context= -context;
  611. sign=1;
  612. }else
  613. sign=0;
  614. if(s->ac){
  615. diff= get_symbol(c, p->state[context], 1);
  616. }else{
  617. if(context == 0 && run_mode==0) run_mode=1;
  618. if(run_mode){
  619. if(run_count==0 && run_mode==1){
  620. if(get_bits1(&s->gb)){
  621. run_count = 1<<ff_log2_run[run_index];
  622. if(x + run_count <= w) run_index++;
  623. }else{
  624. if(ff_log2_run[run_index]) run_count = get_bits(&s->gb, ff_log2_run[run_index]);
  625. else run_count=0;
  626. if(run_index) run_index--;
  627. run_mode=2;
  628. }
  629. }
  630. run_count--;
  631. if(run_count < 0){
  632. run_mode=0;
  633. run_count=0;
  634. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  635. if(diff>=0) diff++;
  636. }else
  637. diff=0;
  638. }else
  639. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  640. // 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));
  641. }
  642. if(sign) diff= -diff;
  643. sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
  644. }
  645. s->run_index= run_index;
  646. }
  647. static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  648. int x, y;
  649. int_fast16_t sample_buffer[2][w+6];
  650. int_fast16_t *sample[2];
  651. sample[0]=sample_buffer[0]+3;
  652. sample[1]=sample_buffer[1]+3;
  653. s->run_index=0;
  654. memset(sample_buffer, 0, sizeof(sample_buffer));
  655. for(y=0; y<h; y++){
  656. int_fast16_t *temp= sample[0]; //FIXME try a normal buffer
  657. sample[0]= sample[1];
  658. sample[1]= temp;
  659. sample[1][-1]= sample[0][0 ];
  660. sample[0][ w]= sample[0][w-1];
  661. //{START_TIMER
  662. decode_line(s, w, sample, plane_index, 8);
  663. for(x=0; x<w; x++){
  664. src[x + stride*y]= sample[1][x];
  665. }
  666. //STOP_TIMER("decode-line")}
  667. }
  668. }
  669. static void decode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  670. int x, y, p;
  671. int_fast16_t sample_buffer[3][2][w+6];
  672. int_fast16_t *sample[3][2];
  673. for(x=0; x<3; x++){
  674. sample[x][0] = sample_buffer[x][0]+3;
  675. sample[x][1] = sample_buffer[x][1]+3;
  676. }
  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 av_cold 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, const 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. .long_name= NULL_IF_CONFIG_SMALL("FFmpeg codec #1"),
  856. };
  857. #if CONFIG_FFV1_ENCODER
  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, PIX_FMT_NONE},
  867. .long_name= NULL_IF_CONFIG_SMALL("FFmpeg codec #1"),
  868. };
  869. #endif