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.

1043 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 "get_bits.h"
  28. #include "put_bits.h"
  29. #include "dsputil.h"
  30. #include "rangecoder.h"
  31. #include "golomb.h"
  32. #include "mathops.h"
  33. #define MAX_PLANES 4
  34. #define CONTEXT_SIZE 32
  35. extern const uint8_t ff_log2_run[32];
  36. static const int8_t quant3[256]={
  37. 0, 0, 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,
  43. 1, 1, 1, 1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
  108. static const int8_t quant11[256]={
  109. 0, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
  110. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  111. 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  112. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  113. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  114. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  115. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  116. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  117. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  118. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  119. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  120. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  121. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  122. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-4,-4,
  123. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  124. -4,-4,-4,-4,-4,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-1,
  125. };
  126. static const int8_t quant13[256]={
  127. 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  128. 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  129. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  130. 5, 5, 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,-6,
  139. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-5,
  140. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  141. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  142. -4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,-2,-2,-1,
  143. };
  144. typedef struct VlcState{
  145. int16_t drift;
  146. uint16_t error_sum;
  147. int8_t bias;
  148. uint8_t count;
  149. } VlcState;
  150. typedef struct PlaneContext{
  151. int context_count;
  152. uint8_t (*state)[CONTEXT_SIZE];
  153. VlcState *vlc_state;
  154. uint8_t interlace_bit_state[2];
  155. } PlaneContext;
  156. typedef struct FFV1Context{
  157. AVCodecContext *avctx;
  158. RangeCoder c;
  159. GetBitContext gb;
  160. PutBitContext pb;
  161. int version;
  162. int width, height;
  163. int chroma_h_shift, chroma_v_shift;
  164. int flags;
  165. int picture_number;
  166. AVFrame picture;
  167. int plane_count;
  168. int ac; ///< 1-> CABAC 0-> golomb rice
  169. PlaneContext plane[MAX_PLANES];
  170. int16_t quant_table[5][256];
  171. int run_index;
  172. int colorspace;
  173. DSPContext dsp;
  174. }FFV1Context;
  175. static av_always_inline int fold(int diff, int bits){
  176. if(bits==8)
  177. diff= (int8_t)diff;
  178. else{
  179. diff+= 1<<(bits-1);
  180. diff&=(1<<bits)-1;
  181. diff-= 1<<(bits-1);
  182. }
  183. return diff;
  184. }
  185. static inline int predict(int_fast16_t *src, int_fast16_t *last){
  186. const int LT= last[-1];
  187. const int T= last[ 0];
  188. const int L = src[-1];
  189. return mid_pred(L, L + T - LT, T);
  190. }
  191. static inline int get_context(FFV1Context *f, int_fast16_t *src, int_fast16_t *last, int_fast16_t *last2){
  192. const int LT= last[-1];
  193. const int T= last[ 0];
  194. const int RT= last[ 1];
  195. const int L = src[-1];
  196. if(f->quant_table[3][127]){
  197. const int TT= last2[0];
  198. const int LL= src[-2];
  199. return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF]
  200. +f->quant_table[3][(LL-L) & 0xFF] + f->quant_table[4][(TT-T) & 0xFF];
  201. }else
  202. return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF];
  203. }
  204. static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
  205. int i;
  206. if(v){
  207. const int a= FFABS(v);
  208. const int e= av_log2(a);
  209. put_rac(c, state+0, 0);
  210. assert(e<=9);
  211. for(i=0; i<e; i++){
  212. put_rac(c, state+1+i, 1); //1..10
  213. }
  214. put_rac(c, state+1+i, 0);
  215. for(i=e-1; i>=0; i--){
  216. put_rac(c, state+22+i, (a>>i)&1); //22..31
  217. }
  218. if(is_signed)
  219. put_rac(c, state+11 + e, v < 0); //11..21
  220. }else{
  221. put_rac(c, state+0, 1);
  222. }
  223. }
  224. static inline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
  225. if(get_rac(c, state+0))
  226. return 0;
  227. else{
  228. int i, e, a;
  229. e= 0;
  230. while(get_rac(c, state+1 + e) && e<9){ //1..10
  231. e++;
  232. }
  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. s->avctx= avctx;
  459. s->flags= avctx->flags;
  460. dsputil_init(&s->dsp, avctx);
  461. s->width = avctx->width;
  462. s->height= avctx->height;
  463. assert(s->width && s->height);
  464. return 0;
  465. }
  466. #if CONFIG_FFV1_ENCODER
  467. static av_cold 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_RGB32:
  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. #endif /* CONFIG_FFV1_ENCODER */
  522. static void clear_state(FFV1Context *f){
  523. int i, j;
  524. for(i=0; i<f->plane_count; i++){
  525. PlaneContext *p= &f->plane[i];
  526. p->interlace_bit_state[0]= 128;
  527. p->interlace_bit_state[1]= 128;
  528. for(j=0; j<p->context_count; j++){
  529. if(f->ac){
  530. memset(p->state[j], 128, sizeof(uint8_t)*CONTEXT_SIZE);
  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. #if CONFIG_FFV1_ENCODER
  541. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  542. FFV1Context *f = avctx->priv_data;
  543. RangeCoder * const c= &f->c;
  544. AVFrame *pict = data;
  545. const int width= f->width;
  546. const int height= f->height;
  547. AVFrame * const p= &f->picture;
  548. int used_count= 0;
  549. uint8_t keystate=128;
  550. ff_init_range_encoder(c, buf, buf_size);
  551. // ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  552. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  553. *p = *pict;
  554. p->pict_type= FF_I_TYPE;
  555. if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){
  556. put_rac(c, &keystate, 1);
  557. p->key_frame= 1;
  558. write_header(f);
  559. clear_state(f);
  560. }else{
  561. put_rac(c, &keystate, 0);
  562. p->key_frame= 0;
  563. }
  564. if(!f->ac){
  565. used_count += ff_rac_terminate(c);
  566. //printf("pos=%d\n", used_count);
  567. init_put_bits(&f->pb, buf + used_count, buf_size - used_count);
  568. }
  569. if(f->colorspace==0){
  570. const int chroma_width = -((-width )>>f->chroma_h_shift);
  571. const int chroma_height= -((-height)>>f->chroma_v_shift);
  572. encode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  573. encode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  574. encode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
  575. }else{
  576. encode_rgb_frame(f, (uint32_t*)(p->data[0]), width, height, p->linesize[0]/4);
  577. }
  578. emms_c();
  579. f->picture_number++;
  580. if(f->ac){
  581. return ff_rac_terminate(c);
  582. }else{
  583. flush_put_bits(&f->pb); //nicer padding FIXME
  584. return used_count + (put_bits_count(&f->pb)+7)/8;
  585. }
  586. }
  587. #endif /* CONFIG_FFV1_ENCODER */
  588. static av_cold int common_end(AVCodecContext *avctx){
  589. FFV1Context *s = avctx->priv_data;
  590. int i;
  591. for(i=0; i<s->plane_count; i++){
  592. PlaneContext *p= &s->plane[i];
  593. av_freep(&p->state);
  594. av_freep(&p->vlc_state);
  595. }
  596. return 0;
  597. }
  598. static inline void decode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
  599. PlaneContext * const p= &s->plane[plane_index];
  600. RangeCoder * const c= &s->c;
  601. int x;
  602. int run_count=0;
  603. int run_mode=0;
  604. int run_index= s->run_index;
  605. for(x=0; x<w; x++){
  606. int diff, context, sign;
  607. context= get_context(s, sample[1] + x, sample[0] + x, sample[1] + x);
  608. if(context < 0){
  609. context= -context;
  610. sign=1;
  611. }else
  612. sign=0;
  613. if(s->ac){
  614. diff= get_symbol(c, p->state[context], 1);
  615. }else{
  616. if(context == 0 && run_mode==0) run_mode=1;
  617. if(run_mode){
  618. if(run_count==0 && run_mode==1){
  619. if(get_bits1(&s->gb)){
  620. run_count = 1<<ff_log2_run[run_index];
  621. if(x + run_count <= w) run_index++;
  622. }else{
  623. if(ff_log2_run[run_index]) run_count = get_bits(&s->gb, ff_log2_run[run_index]);
  624. else run_count=0;
  625. if(run_index) run_index--;
  626. run_mode=2;
  627. }
  628. }
  629. run_count--;
  630. if(run_count < 0){
  631. run_mode=0;
  632. run_count=0;
  633. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  634. if(diff>=0) diff++;
  635. }else
  636. diff=0;
  637. }else
  638. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  639. // 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));
  640. }
  641. if(sign) diff= -diff;
  642. sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
  643. }
  644. s->run_index= run_index;
  645. }
  646. static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  647. int x, y;
  648. int_fast16_t sample_buffer[2][w+6];
  649. int_fast16_t *sample[2];
  650. sample[0]=sample_buffer[0]+3;
  651. sample[1]=sample_buffer[1]+3;
  652. s->run_index=0;
  653. memset(sample_buffer, 0, sizeof(sample_buffer));
  654. for(y=0; y<h; y++){
  655. int_fast16_t *temp= sample[0]; //FIXME try a normal buffer
  656. sample[0]= sample[1];
  657. sample[1]= temp;
  658. sample[1][-1]= sample[0][0 ];
  659. sample[0][ w]= sample[0][w-1];
  660. //{START_TIMER
  661. decode_line(s, w, sample, plane_index, 8);
  662. for(x=0; x<w; x++){
  663. src[x + stride*y]= sample[1][x];
  664. }
  665. //STOP_TIMER("decode-line")}
  666. }
  667. }
  668. static void decode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  669. int x, y, p;
  670. int_fast16_t sample_buffer[3][2][w+6];
  671. int_fast16_t *sample[3][2];
  672. for(x=0; x<3; x++){
  673. sample[x][0] = sample_buffer[x][0]+3;
  674. sample[x][1] = sample_buffer[x][1]+3;
  675. }
  676. s->run_index=0;
  677. memset(sample_buffer, 0, sizeof(sample_buffer));
  678. for(y=0; y<h; y++){
  679. for(p=0; p<3; p++){
  680. int_fast16_t *temp= sample[p][0]; //FIXME try a normal buffer
  681. sample[p][0]= sample[p][1];
  682. sample[p][1]= temp;
  683. sample[p][1][-1]= sample[p][0][0 ];
  684. sample[p][0][ w]= sample[p][0][w-1];
  685. decode_line(s, w, sample[p], FFMIN(p, 1), 9);
  686. }
  687. for(x=0; x<w; x++){
  688. int g= sample[0][1][x];
  689. int b= sample[1][1][x];
  690. int r= sample[2][1][x];
  691. // assert(g>=0 && b>=0 && r>=0);
  692. // assert(g<256 && b<512 && r<512);
  693. b -= 0x100;
  694. r -= 0x100;
  695. g -= (b + r)>>2;
  696. b += g;
  697. r += g;
  698. src[x + stride*y]= b + (g<<8) + (r<<16);
  699. }
  700. }
  701. }
  702. static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){
  703. int v;
  704. int i=0;
  705. uint8_t state[CONTEXT_SIZE];
  706. memset(state, 128, sizeof(state));
  707. for(v=0; i<128 ; v++){
  708. int len= get_symbol(c, state, 0) + 1;
  709. if(len + i > 128) return -1;
  710. while(len--){
  711. quant_table[i] = scale*v;
  712. i++;
  713. //printf("%2d ",v);
  714. //if(i%16==0) printf("\n");
  715. }
  716. }
  717. for(i=1; i<128; i++){
  718. quant_table[256-i]= -quant_table[i];
  719. }
  720. quant_table[128]= -quant_table[127];
  721. return 2*v - 1;
  722. }
  723. static int read_header(FFV1Context *f){
  724. uint8_t state[CONTEXT_SIZE];
  725. int i, context_count;
  726. RangeCoder * const c= &f->c;
  727. memset(state, 128, sizeof(state));
  728. f->version= get_symbol(c, state, 0);
  729. f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
  730. f->colorspace= get_symbol(c, state, 0); //YUV cs type
  731. get_rac(c, state); //no chroma = false
  732. f->chroma_h_shift= get_symbol(c, state, 0);
  733. f->chroma_v_shift= get_symbol(c, state, 0);
  734. get_rac(c, state); //transparency plane
  735. f->plane_count= 2;
  736. if(f->colorspace==0){
  737. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  738. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
  739. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
  740. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
  741. case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
  742. case 0x22: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
  743. default:
  744. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  745. return -1;
  746. }
  747. }else if(f->colorspace==1){
  748. if(f->chroma_h_shift || f->chroma_v_shift){
  749. av_log(f->avctx, AV_LOG_ERROR, "chroma subsampling not supported in this colorspace\n");
  750. return -1;
  751. }
  752. f->avctx->pix_fmt= PIX_FMT_RGB32;
  753. }else{
  754. av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
  755. return -1;
  756. }
  757. //printf("%d %d %d\n", f->chroma_h_shift, f->chroma_v_shift,f->avctx->pix_fmt);
  758. context_count=1;
  759. for(i=0; i<5; i++){
  760. context_count*= read_quant_table(c, f->quant_table[i], context_count);
  761. if(context_count < 0 || context_count > 32768){
  762. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  763. return -1;
  764. }
  765. }
  766. context_count= (context_count+1)/2;
  767. for(i=0; i<f->plane_count; i++){
  768. PlaneContext * const p= &f->plane[i];
  769. p->context_count= context_count;
  770. if(f->ac){
  771. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  772. }else{
  773. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  774. }
  775. }
  776. return 0;
  777. }
  778. static av_cold int decode_init(AVCodecContext *avctx)
  779. {
  780. // FFV1Context *s = avctx->priv_data;
  781. common_init(avctx);
  782. return 0;
  783. }
  784. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt){
  785. const uint8_t *buf = avpkt->data;
  786. int buf_size = avpkt->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. .long_name= NULL_IF_CONFIG_SMALL("FFmpeg codec #1"),
  857. };
  858. #if CONFIG_FFV1_ENCODER
  859. AVCodec ffv1_encoder = {
  860. "ffv1",
  861. CODEC_TYPE_VIDEO,
  862. CODEC_ID_FFV1,
  863. sizeof(FFV1Context),
  864. encode_init,
  865. encode_frame,
  866. common_end,
  867. .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},
  868. .long_name= NULL_IF_CONFIG_SMALL("FFmpeg codec #1"),
  869. };
  870. #endif