ffv1.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815
  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
  24. * FF Video Codec 1 (a 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. #include "libavutil/avassert.h"
  34. #define MAX_PLANES 4
  35. #define CONTEXT_SIZE 32
  36. #define MAX_QUANT_TABLES 8
  37. #define MAX_CONTEXT_INPUTS 5
  38. extern const uint8_t ff_log2_run[32];
  39. static const int8_t quant3[256]={
  40. 0, 0, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  44. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  45. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  46. 1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2,-1,-1,
  110. };
  111. static const int8_t quant9[256]={
  112. 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  113. 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  114. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  115. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  116. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  117. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  118. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  119. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  120. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  121. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  122. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-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,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  125. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  126. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,
  127. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-1,-1,
  128. };
  129. static const int8_t quant9_10bit[256]={
  130. 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
  131. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3,
  132. 3, 3, 3, 3, 3, 3, 3, 3, 3, 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6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  176. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  177. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  178. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-5,
  179. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  180. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  181. -4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,-2,-2,-1,
  182. };
  183. static const uint8_t ver2_state[256]= {
  184. 0, 10, 10, 10, 10, 16, 16, 16, 28, 16, 16, 29, 42, 49, 20, 49,
  185. 59, 25, 26, 26, 27, 31, 33, 33, 33, 34, 34, 37, 67, 38, 39, 39,
  186. 40, 40, 41, 79, 43, 44, 45, 45, 48, 48, 64, 50, 51, 52, 88, 52,
  187. 53, 74, 55, 57, 58, 58, 74, 60, 101, 61, 62, 84, 66, 66, 68, 69,
  188. 87, 82, 71, 97, 73, 73, 82, 75, 111, 77, 94, 78, 87, 81, 83, 97,
  189. 85, 83, 94, 86, 99, 89, 90, 99, 111, 92, 93, 134, 95, 98, 105, 98,
  190. 105, 110, 102, 108, 102, 118, 103, 106, 106, 113, 109, 112, 114, 112, 116, 125,
  191. 115, 116, 117, 117, 126, 119, 125, 121, 121, 123, 145, 124, 126, 131, 127, 129,
  192. 165, 130, 132, 138, 133, 135, 145, 136, 137, 139, 146, 141, 143, 142, 144, 148,
  193. 147, 155, 151, 149, 151, 150, 152, 157, 153, 154, 156, 168, 158, 162, 161, 160,
  194. 172, 163, 169, 164, 166, 184, 167, 170, 177, 174, 171, 173, 182, 176, 180, 178,
  195. 175, 189, 179, 181, 186, 183, 192, 185, 200, 187, 191, 188, 190, 197, 193, 196,
  196. 197, 194, 195, 196, 198, 202, 199, 201, 210, 203, 207, 204, 205, 206, 208, 214,
  197. 209, 211, 221, 212, 213, 215, 224, 216, 217, 218, 219, 220, 222, 228, 223, 225,
  198. 226, 224, 227, 229, 240, 230, 231, 232, 233, 234, 235, 236, 238, 239, 237, 242,
  199. 241, 243, 242, 244, 245, 246, 247, 248, 249, 250, 251, 252, 252, 253, 254, 255,
  200. };
  201. typedef struct VlcState{
  202. int16_t drift;
  203. uint16_t error_sum;
  204. int8_t bias;
  205. uint8_t count;
  206. } VlcState;
  207. typedef struct PlaneContext{
  208. int16_t quant_table[MAX_CONTEXT_INPUTS][256];
  209. int quant_table_index;
  210. int context_count;
  211. uint8_t (*state)[CONTEXT_SIZE];
  212. VlcState *vlc_state;
  213. uint8_t interlace_bit_state[2];
  214. } PlaneContext;
  215. #define MAX_SLICES 256
  216. typedef struct FFV1Context{
  217. AVCodecContext *avctx;
  218. RangeCoder c;
  219. GetBitContext gb;
  220. PutBitContext pb;
  221. uint64_t rc_stat[256][2];
  222. uint64_t (*rc_stat2[MAX_QUANT_TABLES])[32][2];
  223. int version;
  224. int width, height;
  225. int chroma_h_shift, chroma_v_shift;
  226. int flags;
  227. int picture_number;
  228. AVFrame picture;
  229. int plane_count;
  230. int ac; ///< 1=range coder <-> 0=golomb rice
  231. PlaneContext plane[MAX_PLANES];
  232. int16_t quant_table[MAX_CONTEXT_INPUTS][256];
  233. int16_t quant_tables[MAX_QUANT_TABLES][MAX_CONTEXT_INPUTS][256];
  234. int context_count[MAX_QUANT_TABLES];
  235. uint8_t state_transition[256];
  236. uint8_t (*initial_states[MAX_QUANT_TABLES])[32];
  237. int run_index;
  238. int colorspace;
  239. int_fast16_t *sample_buffer;
  240. int gob_count;
  241. int quant_table_count;
  242. DSPContext dsp;
  243. struct FFV1Context *slice_context[MAX_SLICES];
  244. int slice_count;
  245. int num_v_slices;
  246. int num_h_slices;
  247. int slice_width;
  248. int slice_height;
  249. int slice_x;
  250. int slice_y;
  251. }FFV1Context;
  252. static av_always_inline int fold(int diff, int bits){
  253. if(bits==8)
  254. diff= (int8_t)diff;
  255. else{
  256. diff+= 1<<(bits-1);
  257. diff&=(1<<bits)-1;
  258. diff-= 1<<(bits-1);
  259. }
  260. return diff;
  261. }
  262. static inline int predict(int_fast16_t *src, int_fast16_t *last){
  263. const int LT= last[-1];
  264. const int T= last[ 0];
  265. const int L = src[-1];
  266. return mid_pred(L, L + T - LT, T);
  267. }
  268. static inline int get_context(PlaneContext *p, int_fast16_t *src, int_fast16_t *last, int_fast16_t *last2){
  269. const int LT= last[-1];
  270. const int T= last[ 0];
  271. const int RT= last[ 1];
  272. const int L = src[-1];
  273. if(p->quant_table[3][127]){
  274. const int TT= last2[0];
  275. const int LL= src[-2];
  276. return p->quant_table[0][(L-LT) & 0xFF] + p->quant_table[1][(LT-T) & 0xFF] + p->quant_table[2][(T-RT) & 0xFF]
  277. +p->quant_table[3][(LL-L) & 0xFF] + p->quant_table[4][(TT-T) & 0xFF];
  278. }else
  279. return p->quant_table[0][(L-LT) & 0xFF] + p->quant_table[1][(LT-T) & 0xFF] + p->quant_table[2][(T-RT) & 0xFF];
  280. }
  281. static void find_best_state(uint8_t best_state[256][256], const uint8_t one_state[256]){
  282. int i,j,k,m;
  283. double l2tab[256];
  284. for(i=1; i<256; i++)
  285. l2tab[i]= log2(i/256.0);
  286. for(i=0; i<256; i++){
  287. double best_len[256];
  288. double p= i/256.0;
  289. for(j=0; j<256; j++)
  290. best_len[j]= 1<<30;
  291. for(j=FFMAX(i-10,1); j<FFMIN(i+11,256); j++){
  292. double occ[256]={0};
  293. double len=0;
  294. occ[j]=1.0;
  295. for(k=0; k<256; k++){
  296. double newocc[256]={0};
  297. for(m=0; m<256; m++){
  298. if(occ[m]){
  299. len -=occ[m]*( p *l2tab[ m]
  300. + (1-p)*l2tab[256-m]);
  301. }
  302. }
  303. if(len < best_len[k]){
  304. best_len[k]= len;
  305. best_state[i][k]= j;
  306. }
  307. for(m=0; m<256; m++){
  308. if(occ[m]){
  309. newocc[ one_state[ m]] += occ[m]* p ;
  310. newocc[256-one_state[256-m]] += occ[m]*(1-p);
  311. }
  312. }
  313. memcpy(occ, newocc, sizeof(occ));
  314. }
  315. }
  316. }
  317. }
  318. static av_always_inline av_flatten void put_symbol_inline(RangeCoder *c, uint8_t *state, int v, int is_signed, uint64_t rc_stat[256][2], uint64_t rc_stat2[32][2]){
  319. int i;
  320. #define put_rac(C,S,B) \
  321. do{\
  322. if(rc_stat){\
  323. rc_stat[*(S)][B]++;\
  324. rc_stat2[(S)-state][B]++;\
  325. }\
  326. put_rac(C,S,B);\
  327. }while(0)
  328. if(v){
  329. const int a= FFABS(v);
  330. const int e= av_log2(a);
  331. put_rac(c, state+0, 0);
  332. if(e<=9){
  333. for(i=0; i<e; i++){
  334. put_rac(c, state+1+i, 1); //1..10
  335. }
  336. put_rac(c, state+1+i, 0);
  337. for(i=e-1; i>=0; i--){
  338. put_rac(c, state+22+i, (a>>i)&1); //22..31
  339. }
  340. if(is_signed)
  341. put_rac(c, state+11 + e, v < 0); //11..21
  342. }else{
  343. for(i=0; i<e; i++){
  344. put_rac(c, state+1+FFMIN(i,9), 1); //1..10
  345. }
  346. put_rac(c, state+1+9, 0);
  347. for(i=e-1; i>=0; i--){
  348. put_rac(c, state+22+FFMIN(i,9), (a>>i)&1); //22..31
  349. }
  350. if(is_signed)
  351. put_rac(c, state+11 + 10, v < 0); //11..21
  352. }
  353. }else{
  354. put_rac(c, state+0, 1);
  355. }
  356. #undef put_rac
  357. }
  358. static void av_noinline put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
  359. put_symbol_inline(c, state, v, is_signed, NULL, NULL);
  360. }
  361. static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state, int is_signed){
  362. if(get_rac(c, state+0))
  363. return 0;
  364. else{
  365. int i, e, a;
  366. e= 0;
  367. while(get_rac(c, state+1 + FFMIN(e,9))){ //1..10
  368. e++;
  369. }
  370. a= 1;
  371. for(i=e-1; i>=0; i--){
  372. a += a + get_rac(c, state+22 + FFMIN(i,9)); //22..31
  373. }
  374. e= -(is_signed && get_rac(c, state+11 + FFMIN(e, 10))); //11..21
  375. return (a^e)-e;
  376. }
  377. }
  378. static int av_noinline get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
  379. return get_symbol_inline(c, state, is_signed);
  380. }
  381. static inline void update_vlc_state(VlcState * const state, const int v){
  382. int drift= state->drift;
  383. int count= state->count;
  384. state->error_sum += FFABS(v);
  385. drift += v;
  386. if(count == 128){ //FIXME variable
  387. count >>= 1;
  388. drift >>= 1;
  389. state->error_sum >>= 1;
  390. }
  391. count++;
  392. if(drift <= -count){
  393. if(state->bias > -128) state->bias--;
  394. drift += count;
  395. if(drift <= -count)
  396. drift= -count + 1;
  397. }else if(drift > 0){
  398. if(state->bias < 127) state->bias++;
  399. drift -= count;
  400. if(drift > 0)
  401. drift= 0;
  402. }
  403. state->drift= drift;
  404. state->count= count;
  405. }
  406. static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int v, int bits){
  407. int i, k, code;
  408. //printf("final: %d ", v);
  409. v = fold(v - state->bias, bits);
  410. i= state->count;
  411. k=0;
  412. while(i < state->error_sum){ //FIXME optimize
  413. k++;
  414. i += i;
  415. }
  416. assert(k<=8);
  417. #if 0 // JPEG LS
  418. if(k==0 && 2*state->drift <= - state->count) code= v ^ (-1);
  419. else code= v;
  420. #else
  421. code= v ^ ((2*state->drift + state->count)>>31);
  422. #endif
  423. //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);
  424. set_sr_golomb(pb, code, k, 12, bits);
  425. update_vlc_state(state, v);
  426. }
  427. static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state, int bits){
  428. int k, i, v, ret;
  429. i= state->count;
  430. k=0;
  431. while(i < state->error_sum){ //FIXME optimize
  432. k++;
  433. i += i;
  434. }
  435. assert(k<=8);
  436. v= get_sr_golomb(gb, k, 12, bits);
  437. //printf("v:%d bias:%d error:%d drift:%d count:%d k:%d", v, state->bias, state->error_sum, state->drift, state->count, k);
  438. #if 0 // JPEG LS
  439. if(k==0 && 2*state->drift <= - state->count) v ^= (-1);
  440. #else
  441. v ^= ((2*state->drift + state->count)>>31);
  442. #endif
  443. ret= fold(v + state->bias, bits);
  444. update_vlc_state(state, v);
  445. //printf("final: %d\n", ret);
  446. return ret;
  447. }
  448. #if CONFIG_FFV1_ENCODER
  449. static av_always_inline int encode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
  450. PlaneContext * const p= &s->plane[plane_index];
  451. RangeCoder * const c= &s->c;
  452. int x;
  453. int run_index= s->run_index;
  454. int run_count=0;
  455. int run_mode=0;
  456. if(s->ac){
  457. if(c->bytestream_end - c->bytestream < w*20){
  458. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  459. return -1;
  460. }
  461. }else{
  462. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < w*4){
  463. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  464. return -1;
  465. }
  466. }
  467. for(x=0; x<w; x++){
  468. int diff, context;
  469. context= get_context(p, sample[0]+x, sample[1]+x, sample[2]+x);
  470. diff= sample[0][x] - predict(sample[0]+x, sample[1]+x);
  471. if(context < 0){
  472. context = -context;
  473. diff= -diff;
  474. }
  475. diff= fold(diff, bits);
  476. if(s->ac){
  477. if(s->flags & CODEC_FLAG_PASS1){
  478. put_symbol_inline(c, p->state[context], diff, 1, s->rc_stat, s->rc_stat2[p->quant_table_index][context]);
  479. }else{
  480. put_symbol_inline(c, p->state[context], diff, 1, NULL, NULL);
  481. }
  482. }else{
  483. if(context == 0) run_mode=1;
  484. if(run_mode){
  485. if(diff){
  486. while(run_count >= 1<<ff_log2_run[run_index]){
  487. run_count -= 1<<ff_log2_run[run_index];
  488. run_index++;
  489. put_bits(&s->pb, 1, 1);
  490. }
  491. put_bits(&s->pb, 1 + ff_log2_run[run_index], run_count);
  492. if(run_index) run_index--;
  493. run_count=0;
  494. run_mode=0;
  495. if(diff>0) diff--;
  496. }else{
  497. run_count++;
  498. }
  499. }
  500. // 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));
  501. if(run_mode == 0)
  502. put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
  503. }
  504. }
  505. if(run_mode){
  506. while(run_count >= 1<<ff_log2_run[run_index]){
  507. run_count -= 1<<ff_log2_run[run_index];
  508. run_index++;
  509. put_bits(&s->pb, 1, 1);
  510. }
  511. if(run_count)
  512. put_bits(&s->pb, 1, 1);
  513. }
  514. s->run_index= run_index;
  515. return 0;
  516. }
  517. static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  518. int x,y,i;
  519. const int ring_size= s->avctx->context_model ? 3 : 2;
  520. int_fast16_t *sample[3];
  521. s->run_index=0;
  522. memset(s->sample_buffer, 0, ring_size*(w+6)*sizeof(*s->sample_buffer));
  523. for(y=0; y<h; y++){
  524. for(i=0; i<ring_size; i++)
  525. sample[i]= s->sample_buffer + (w+6)*((h+i-y)%ring_size) + 3;
  526. sample[0][-1]= sample[1][0 ];
  527. sample[1][ w]= sample[1][w-1];
  528. //{START_TIMER
  529. if(s->avctx->bits_per_raw_sample<=8){
  530. for(x=0; x<w; x++){
  531. sample[0][x]= src[x + stride*y];
  532. }
  533. encode_line(s, w, sample, plane_index, 8);
  534. }else{
  535. for(x=0; x<w; x++){
  536. sample[0][x]= ((uint16_t*)(src + stride*y))[x] >> (16 - s->avctx->bits_per_raw_sample);
  537. }
  538. encode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
  539. }
  540. //STOP_TIMER("encode line")}
  541. }
  542. }
  543. static void encode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  544. int x, y, p, i;
  545. const int ring_size= s->avctx->context_model ? 3 : 2;
  546. int_fast16_t *sample[3][3];
  547. s->run_index=0;
  548. memset(s->sample_buffer, 0, ring_size*3*(w+6)*sizeof(*s->sample_buffer));
  549. for(y=0; y<h; y++){
  550. for(i=0; i<ring_size; i++)
  551. for(p=0; p<3; p++)
  552. sample[p][i]= s->sample_buffer + p*ring_size*(w+6) + ((h+i-y)%ring_size)*(w+6) + 3;
  553. for(x=0; x<w; x++){
  554. int v= src[x + stride*y];
  555. int b= v&0xFF;
  556. int g= (v>>8)&0xFF;
  557. int r= (v>>16)&0xFF;
  558. b -= g;
  559. r -= g;
  560. g += (b + r)>>2;
  561. b += 0x100;
  562. r += 0x100;
  563. // assert(g>=0 && b>=0 && r>=0);
  564. // assert(g<256 && b<512 && r<512);
  565. sample[0][0][x]= g;
  566. sample[1][0][x]= b;
  567. sample[2][0][x]= r;
  568. }
  569. for(p=0; p<3; p++){
  570. sample[p][0][-1]= sample[p][1][0 ];
  571. sample[p][1][ w]= sample[p][1][w-1];
  572. encode_line(s, w, sample[p], FFMIN(p, 1), 9);
  573. }
  574. }
  575. }
  576. static void write_quant_table(RangeCoder *c, int16_t *quant_table){
  577. int last=0;
  578. int i;
  579. uint8_t state[CONTEXT_SIZE];
  580. memset(state, 128, sizeof(state));
  581. for(i=1; i<128 ; i++){
  582. if(quant_table[i] != quant_table[i-1]){
  583. put_symbol(c, state, i-last-1, 0);
  584. last= i;
  585. }
  586. }
  587. put_symbol(c, state, i-last-1, 0);
  588. }
  589. static void write_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256]){
  590. int i;
  591. for(i=0; i<5; i++)
  592. write_quant_table(c, quant_table[i]);
  593. }
  594. static void write_header(FFV1Context *f){
  595. uint8_t state[CONTEXT_SIZE];
  596. int i, j;
  597. RangeCoder * const c= &f->slice_context[0]->c;
  598. memset(state, 128, sizeof(state));
  599. if(f->version < 2){
  600. put_symbol(c, state, f->version, 0);
  601. put_symbol(c, state, f->ac, 0);
  602. if(f->ac>1){
  603. for(i=1; i<256; i++){
  604. put_symbol(c, state, f->state_transition[i] - c->one_state[i], 1);
  605. }
  606. }
  607. put_symbol(c, state, f->colorspace, 0); //YUV cs type
  608. if(f->version>0)
  609. put_symbol(c, state, f->avctx->bits_per_raw_sample, 0);
  610. put_rac(c, state, 1); //chroma planes
  611. put_symbol(c, state, f->chroma_h_shift, 0);
  612. put_symbol(c, state, f->chroma_v_shift, 0);
  613. put_rac(c, state, 0); //no transparency plane
  614. write_quant_tables(c, f->quant_table);
  615. }else{
  616. put_symbol(c, state, f->slice_count, 0);
  617. for(i=0; i<f->slice_count; i++){
  618. FFV1Context *fs= f->slice_context[i];
  619. put_symbol(c, state, (fs->slice_x +1)*f->num_h_slices / f->width , 0);
  620. put_symbol(c, state, (fs->slice_y +1)*f->num_v_slices / f->height , 0);
  621. put_symbol(c, state, (fs->slice_width +1)*f->num_h_slices / f->width -1, 0);
  622. put_symbol(c, state, (fs->slice_height+1)*f->num_v_slices / f->height-1, 0);
  623. for(j=0; j<f->plane_count; j++){
  624. put_symbol(c, state, f->plane[j].quant_table_index, 0);
  625. av_assert0(f->plane[j].quant_table_index == f->avctx->context_model);
  626. }
  627. }
  628. }
  629. }
  630. #endif /* CONFIG_FFV1_ENCODER */
  631. static av_cold int common_init(AVCodecContext *avctx){
  632. FFV1Context *s = avctx->priv_data;
  633. s->avctx= avctx;
  634. s->flags= avctx->flags;
  635. dsputil_init(&s->dsp, avctx);
  636. s->width = avctx->width;
  637. s->height= avctx->height;
  638. assert(s->width && s->height);
  639. //defaults
  640. s->num_h_slices=1;
  641. s->num_v_slices=1;
  642. return 0;
  643. }
  644. static int init_slice_state(FFV1Context *f){
  645. int i, j;
  646. for(i=0; i<f->slice_count; i++){
  647. FFV1Context *fs= f->slice_context[i];
  648. for(j=0; j<f->plane_count; j++){
  649. PlaneContext * const p= &fs->plane[j];
  650. if(fs->ac){
  651. if(!p-> state) p-> state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  652. if(!p-> state)
  653. return AVERROR(ENOMEM);
  654. }else{
  655. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  656. if(!p->vlc_state)
  657. return AVERROR(ENOMEM);
  658. }
  659. }
  660. if (fs->ac>1){
  661. //FIXME only redo if state_transition changed
  662. for(j=1; j<256; j++){
  663. fs->c.one_state [ j]= fs->state_transition[j];
  664. fs->c.zero_state[256-j]= 256-fs->c.one_state [j];
  665. }
  666. }
  667. }
  668. return 0;
  669. }
  670. static av_cold int init_slice_contexts(FFV1Context *f){
  671. int i;
  672. f->slice_count= f->num_h_slices * f->num_v_slices;
  673. for(i=0; i<f->slice_count; i++){
  674. FFV1Context *fs= av_mallocz(sizeof(*fs));
  675. int sx= i % f->num_h_slices;
  676. int sy= i / f->num_h_slices;
  677. int sxs= f->avctx->width * sx / f->num_h_slices;
  678. int sxe= f->avctx->width *(sx+1) / f->num_h_slices;
  679. int sys= f->avctx->height* sy / f->num_v_slices;
  680. int sye= f->avctx->height*(sy+1) / f->num_v_slices;
  681. f->slice_context[i]= fs;
  682. memcpy(fs, f, sizeof(*fs));
  683. memset(fs->rc_stat2, 0, sizeof(fs->rc_stat2));
  684. fs->slice_width = sxe - sxs;
  685. fs->slice_height= sye - sys;
  686. fs->slice_x = sxs;
  687. fs->slice_y = sys;
  688. fs->sample_buffer = av_malloc(9 * (fs->width+6) * sizeof(*fs->sample_buffer));
  689. if (!fs->sample_buffer)
  690. return AVERROR(ENOMEM);
  691. }
  692. return 0;
  693. }
  694. static int allocate_initial_states(FFV1Context *f){
  695. int i;
  696. for(i=0; i<f->quant_table_count; i++){
  697. f->initial_states[i]= av_malloc(f->context_count[i]*sizeof(*f->initial_states[i]));
  698. if(!f->initial_states[i])
  699. return AVERROR(ENOMEM);
  700. memset(f->initial_states[i], 128, f->context_count[i]*sizeof(*f->initial_states[i]));
  701. }
  702. return 0;
  703. }
  704. #if CONFIG_FFV1_ENCODER
  705. static int write_extra_header(FFV1Context *f){
  706. RangeCoder * const c= &f->c;
  707. uint8_t state[CONTEXT_SIZE];
  708. int i, j, k;
  709. uint8_t state2[32][CONTEXT_SIZE];
  710. memset(state2, 128, sizeof(state2));
  711. memset(state, 128, sizeof(state));
  712. f->avctx->extradata= av_malloc(f->avctx->extradata_size= 10000 + (11*11*5*5*5+11*11*11)*32);
  713. ff_init_range_encoder(c, f->avctx->extradata, f->avctx->extradata_size);
  714. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  715. put_symbol(c, state, f->version, 0);
  716. put_symbol(c, state, f->ac, 0);
  717. if(f->ac>1){
  718. for(i=1; i<256; i++){
  719. put_symbol(c, state, f->state_transition[i] - c->one_state[i], 1);
  720. }
  721. }
  722. put_symbol(c, state, f->colorspace, 0); //YUV cs type
  723. put_symbol(c, state, f->avctx->bits_per_raw_sample, 0);
  724. put_rac(c, state, 1); //chroma planes
  725. put_symbol(c, state, f->chroma_h_shift, 0);
  726. put_symbol(c, state, f->chroma_v_shift, 0);
  727. put_rac(c, state, 0); //no transparency plane
  728. put_symbol(c, state, f->num_h_slices-1, 0);
  729. put_symbol(c, state, f->num_v_slices-1, 0);
  730. put_symbol(c, state, f->quant_table_count, 0);
  731. for(i=0; i<f->quant_table_count; i++)
  732. write_quant_tables(c, f->quant_tables[i]);
  733. for(i=0; i<f->quant_table_count; i++){
  734. for(j=0; j<f->context_count[i]*CONTEXT_SIZE; j++)
  735. if(f->initial_states[i] && f->initial_states[i][0][j] != 128)
  736. break;
  737. if(j<f->context_count[i]*CONTEXT_SIZE){
  738. put_rac(c, state, 1);
  739. for(j=0; j<f->context_count[i]; j++){
  740. for(k=0; k<CONTEXT_SIZE; k++){
  741. int pred= j ? f->initial_states[i][j-1][k] : 128;
  742. put_symbol(c, state2[k], (int8_t)(f->initial_states[i][j][k]-pred), 1);
  743. }
  744. }
  745. }else{
  746. put_rac(c, state, 0);
  747. }
  748. }
  749. f->avctx->extradata_size= ff_rac_terminate(c);
  750. return 0;
  751. }
  752. static int sort_stt(FFV1Context *s, uint8_t stt[256]){
  753. int i,i2,changed,print=0;
  754. do{
  755. changed=0;
  756. for(i=12; i<244; i++){
  757. for(i2=i+1; i2<245 && i2<i+4; i2++){
  758. #define COST(old, new) \
  759. s->rc_stat[old][0]*-log2((256-(new))/256.0)\
  760. +s->rc_stat[old][1]*-log2( (new) /256.0)
  761. #define COST2(old, new) \
  762. COST(old, new)\
  763. +COST(256-(old), 256-(new))
  764. double size0= COST2(i, i ) + COST2(i2, i2);
  765. double sizeX= COST2(i, i2) + COST2(i2, i );
  766. if(sizeX < size0 && i!=128 && i2!=128){
  767. int j;
  768. FFSWAP(int, stt[ i], stt[ i2]);
  769. FFSWAP(int, s->rc_stat[i ][0],s->rc_stat[ i2][0]);
  770. FFSWAP(int, s->rc_stat[i ][1],s->rc_stat[ i2][1]);
  771. if(i != 256-i2){
  772. FFSWAP(int, stt[256-i], stt[256-i2]);
  773. FFSWAP(int, s->rc_stat[256-i][0],s->rc_stat[256-i2][0]);
  774. FFSWAP(int, s->rc_stat[256-i][1],s->rc_stat[256-i2][1]);
  775. }
  776. for(j=1; j<256; j++){
  777. if (stt[j] == i ) stt[j] = i2;
  778. else if(stt[j] == i2) stt[j] = i ;
  779. if(i != 256-i2){
  780. if (stt[256-j] == 256-i ) stt[256-j] = 256-i2;
  781. else if(stt[256-j] == 256-i2) stt[256-j] = 256-i ;
  782. }
  783. }
  784. print=changed=1;
  785. }
  786. }
  787. }
  788. }while(changed);
  789. return print;
  790. }
  791. static av_cold int encode_init(AVCodecContext *avctx)
  792. {
  793. FFV1Context *s = avctx->priv_data;
  794. int i, j, k, m;
  795. common_init(avctx);
  796. s->version=0;
  797. s->ac= avctx->coder_type ? 2:0;
  798. if(s->ac>1)
  799. for(i=1; i<256; i++)
  800. s->state_transition[i]=ver2_state[i];
  801. s->plane_count=2;
  802. for(i=0; i<256; i++){
  803. s->quant_table_count=2;
  804. if(avctx->bits_per_raw_sample <=8){
  805. s->quant_tables[0][0][i]= quant11[i];
  806. s->quant_tables[0][1][i]= 11*quant11[i];
  807. s->quant_tables[0][2][i]= 11*11*quant11[i];
  808. s->quant_tables[1][0][i]= quant11[i];
  809. s->quant_tables[1][1][i]= 11*quant11[i];
  810. s->quant_tables[1][2][i]= 11*11*quant5 [i];
  811. s->quant_tables[1][3][i]= 5*11*11*quant5 [i];
  812. s->quant_tables[1][4][i]= 5*5*11*11*quant5 [i];
  813. }else{
  814. s->quant_tables[0][0][i]= quant9_10bit[i];
  815. s->quant_tables[0][1][i]= 11*quant9_10bit[i];
  816. s->quant_tables[0][2][i]= 11*11*quant9_10bit[i];
  817. s->quant_tables[1][0][i]= quant9_10bit[i];
  818. s->quant_tables[1][1][i]= 11*quant9_10bit[i];
  819. s->quant_tables[1][2][i]= 11*11*quant5_10bit[i];
  820. s->quant_tables[1][3][i]= 5*11*11*quant5_10bit[i];
  821. s->quant_tables[1][4][i]= 5*5*11*11*quant5_10bit[i];
  822. }
  823. }
  824. s->context_count[0]= (11*11*11+1)/2;
  825. s->context_count[1]= (11*11*5*5*5+1)/2;
  826. memcpy(s->quant_table, s->quant_tables[avctx->context_model], sizeof(s->quant_table));
  827. for(i=0; i<s->plane_count; i++){
  828. PlaneContext * const p= &s->plane[i];
  829. memcpy(p->quant_table, s->quant_table, sizeof(p->quant_table));
  830. p->quant_table_index= avctx->context_model;
  831. p->context_count= s->context_count[p->quant_table_index];
  832. }
  833. if(allocate_initial_states(s) < 0)
  834. return AVERROR(ENOMEM);
  835. avctx->coded_frame= &s->picture;
  836. switch(avctx->pix_fmt){
  837. case PIX_FMT_YUV444P16:
  838. case PIX_FMT_YUV422P16:
  839. case PIX_FMT_YUV420P16:
  840. if(avctx->bits_per_raw_sample <=8){
  841. av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample invalid\n");
  842. return -1;
  843. }
  844. if(!s->ac){
  845. av_log(avctx, AV_LOG_ERROR, "bits_per_raw_sample of more than 8 needs -coder 1 currently\n");
  846. return -1;
  847. }
  848. s->version= FFMAX(s->version, 1);
  849. case PIX_FMT_YUV444P:
  850. case PIX_FMT_YUV422P:
  851. case PIX_FMT_YUV420P:
  852. case PIX_FMT_YUV411P:
  853. case PIX_FMT_YUV410P:
  854. s->colorspace= 0;
  855. break;
  856. case PIX_FMT_RGB32:
  857. s->colorspace= 1;
  858. break;
  859. default:
  860. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  861. return -1;
  862. }
  863. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  864. s->picture_number=0;
  865. if(avctx->flags & (CODEC_FLAG_PASS1|CODEC_FLAG_PASS2)){
  866. for(i=0; i<s->quant_table_count; i++){
  867. s->rc_stat2[i]= av_mallocz(s->context_count[i]*sizeof(*s->rc_stat2[i]));
  868. if(!s->rc_stat2[i])
  869. return AVERROR(ENOMEM);
  870. }
  871. }
  872. if(avctx->stats_in){
  873. char *p= avctx->stats_in;
  874. uint8_t best_state[256][256];
  875. int gob_count=0;
  876. char *next;
  877. av_assert0(s->version>=2);
  878. for(;;){
  879. for(j=0; j<256; j++){
  880. for(i=0; i<2; i++){
  881. s->rc_stat[j][i]= strtol(p, &next, 0);
  882. if(next==p){
  883. av_log(avctx, AV_LOG_ERROR, "2Pass file invalid at %d %d [%s]\n", j,i,p);
  884. return -1;
  885. }
  886. p=next;
  887. }
  888. }
  889. for(i=0; i<s->quant_table_count; i++){
  890. for(j=0; j<s->context_count[i]; j++){
  891. for(k=0; k<32; k++){
  892. for(m=0; m<2; m++){
  893. s->rc_stat2[i][j][k][m]= strtol(p, &next, 0);
  894. if(next==p){
  895. av_log(avctx, AV_LOG_ERROR, "2Pass file invalid at %d %d %d %d [%s]\n", i,j,k,m,p);
  896. return -1;
  897. }
  898. p=next;
  899. }
  900. }
  901. }
  902. }
  903. gob_count= strtol(p, &next, 0);
  904. if(next==p || gob_count <0){
  905. av_log(avctx, AV_LOG_ERROR, "2Pass file invalid\n");
  906. return -1;
  907. }
  908. p=next;
  909. while(*p=='\n' || *p==' ') p++;
  910. if(p[0]==0) break;
  911. }
  912. sort_stt(s, s->state_transition);
  913. find_best_state(best_state, s->state_transition);
  914. for(i=0; i<s->quant_table_count; i++){
  915. for(j=0; j<s->context_count[i]; j++){
  916. for(k=0; k<32; k++){
  917. double p= 128;
  918. if(s->rc_stat2[i][j][k][0]+s->rc_stat2[i][j][k][1]){
  919. p=256.0*s->rc_stat2[i][j][k][1] / (s->rc_stat2[i][j][k][0]+s->rc_stat2[i][j][k][1]);
  920. }
  921. s->initial_states[i][j][k]= best_state[av_clip(round(p), 1, 255)][av_clip((s->rc_stat2[i][j][k][0]+s->rc_stat2[i][j][k][1])/gob_count, 0, 255)];
  922. }
  923. }
  924. }
  925. }
  926. if(s->version>1){
  927. s->num_h_slices=2;
  928. s->num_v_slices=2;
  929. write_extra_header(s);
  930. }
  931. if(init_slice_contexts(s) < 0)
  932. return -1;
  933. if(init_slice_state(s) < 0)
  934. return -1;
  935. #define STATS_OUT_SIZE 1024*1024*6
  936. if(avctx->flags & CODEC_FLAG_PASS1){
  937. avctx->stats_out= av_mallocz(STATS_OUT_SIZE);
  938. for(i=0; i<s->quant_table_count; i++){
  939. for(j=0; j<s->slice_count; j++){
  940. FFV1Context *sf= s->slice_context[j];
  941. av_assert0(!sf->rc_stat2[i]);
  942. sf->rc_stat2[i]= av_mallocz(s->context_count[i]*sizeof(*sf->rc_stat2[i]));
  943. if(!sf->rc_stat2[i])
  944. return AVERROR(ENOMEM);
  945. }
  946. }
  947. }
  948. return 0;
  949. }
  950. #endif /* CONFIG_FFV1_ENCODER */
  951. static void clear_state(FFV1Context *f){
  952. int i, si, j;
  953. for(si=0; si<f->slice_count; si++){
  954. FFV1Context *fs= f->slice_context[si];
  955. for(i=0; i<f->plane_count; i++){
  956. PlaneContext *p= &fs->plane[i];
  957. p->interlace_bit_state[0]= 128;
  958. p->interlace_bit_state[1]= 128;
  959. if(fs->ac){
  960. if(f->initial_states[p->quant_table_index]){
  961. memcpy(p->state, f->initial_states[p->quant_table_index], CONTEXT_SIZE*p->context_count);
  962. }else
  963. memset(p->state, 128, CONTEXT_SIZE*p->context_count);
  964. }else{
  965. for(j=0; j<p->context_count; j++){
  966. p->vlc_state[j].drift= 0;
  967. p->vlc_state[j].error_sum= 4; //FFMAX((RANGE + 32)/64, 2);
  968. p->vlc_state[j].bias= 0;
  969. p->vlc_state[j].count= 1;
  970. }
  971. }
  972. }
  973. }
  974. }
  975. #if CONFIG_FFV1_ENCODER
  976. static int encode_slice(AVCodecContext *c, void *arg){
  977. FFV1Context *fs= *(void**)arg;
  978. FFV1Context *f= fs->avctx->priv_data;
  979. int width = fs->slice_width;
  980. int height= fs->slice_height;
  981. int x= fs->slice_x;
  982. int y= fs->slice_y;
  983. AVFrame * const p= &f->picture;
  984. if(f->colorspace==0){
  985. const int chroma_width = -((-width )>>f->chroma_h_shift);
  986. const int chroma_height= -((-height)>>f->chroma_v_shift);
  987. const int cx= x>>f->chroma_h_shift;
  988. const int cy= y>>f->chroma_v_shift;
  989. encode_plane(fs, p->data[0] + x + y*p->linesize[0], width, height, p->linesize[0], 0);
  990. encode_plane(fs, p->data[1] + cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
  991. encode_plane(fs, p->data[2] + cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1);
  992. }else{
  993. encode_rgb_frame(fs, (uint32_t*)(p->data[0]) + x + y*(p->linesize[0]/4), width, height, p->linesize[0]/4);
  994. }
  995. emms_c();
  996. return 0;
  997. }
  998. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  999. FFV1Context *f = avctx->priv_data;
  1000. RangeCoder * const c= &f->slice_context[0]->c;
  1001. AVFrame *pict = data;
  1002. AVFrame * const p= &f->picture;
  1003. int used_count= 0;
  1004. uint8_t keystate=128;
  1005. uint8_t *buf_p;
  1006. int i;
  1007. ff_init_range_encoder(c, buf, buf_size);
  1008. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  1009. *p = *pict;
  1010. p->pict_type= FF_I_TYPE;
  1011. if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){
  1012. put_rac(c, &keystate, 1);
  1013. p->key_frame= 1;
  1014. f->gob_count++;
  1015. write_header(f);
  1016. clear_state(f);
  1017. }else{
  1018. put_rac(c, &keystate, 0);
  1019. p->key_frame= 0;
  1020. }
  1021. if(!f->ac){
  1022. used_count += ff_rac_terminate(c);
  1023. //printf("pos=%d\n", used_count);
  1024. init_put_bits(&f->slice_context[0]->pb, buf + used_count, buf_size - used_count);
  1025. }else if (f->ac>1){
  1026. int i;
  1027. for(i=1; i<256; i++){
  1028. c->one_state[i]= f->state_transition[i];
  1029. c->zero_state[256-i]= 256-c->one_state[i];
  1030. }
  1031. }
  1032. for(i=1; i<f->slice_count; i++){
  1033. FFV1Context *fs= f->slice_context[i];
  1034. uint8_t *start= buf + (buf_size-used_count)*i/f->slice_count;
  1035. int len= buf_size/f->slice_count;
  1036. if(fs->ac){
  1037. ff_init_range_encoder(&fs->c, start, len);
  1038. }else{
  1039. init_put_bits(&fs->pb, start, len);
  1040. }
  1041. }
  1042. avctx->execute(avctx, encode_slice, &f->slice_context[0], NULL, f->slice_count, sizeof(void*));
  1043. buf_p=buf;
  1044. for(i=0; i<f->slice_count; i++){
  1045. FFV1Context *fs= f->slice_context[i];
  1046. int bytes;
  1047. if(fs->ac){
  1048. uint8_t state=128;
  1049. put_rac(&fs->c, &state, 0);
  1050. bytes= ff_rac_terminate(&fs->c);
  1051. }else{
  1052. flush_put_bits(&fs->pb); //nicer padding FIXME
  1053. bytes= used_count + (put_bits_count(&fs->pb)+7)/8;
  1054. used_count= 0;
  1055. }
  1056. if(i>0){
  1057. av_assert0(bytes < buf_size/f->slice_count);
  1058. memmove(buf_p, fs->ac ? fs->c.bytestream_start : fs->pb.buf, bytes);
  1059. av_assert0(bytes < (1<<24));
  1060. AV_WB24(buf_p+bytes, bytes);
  1061. bytes+=3;
  1062. }
  1063. buf_p += bytes;
  1064. }
  1065. if((avctx->flags&CODEC_FLAG_PASS1) && (f->picture_number&31)==0){
  1066. int j, k, m;
  1067. char *p= avctx->stats_out;
  1068. char *end= p + STATS_OUT_SIZE;
  1069. memset(f->rc_stat, 0, sizeof(f->rc_stat));
  1070. for(i=0; i<f->quant_table_count; i++)
  1071. memset(f->rc_stat2[i], 0, f->context_count[i]*sizeof(*f->rc_stat2[i]));
  1072. for(j=0; j<f->slice_count; j++){
  1073. FFV1Context *fs= f->slice_context[j];
  1074. for(i=0; i<256; i++){
  1075. f->rc_stat[i][0] += fs->rc_stat[i][0];
  1076. f->rc_stat[i][1] += fs->rc_stat[i][1];
  1077. }
  1078. for(i=0; i<f->quant_table_count; i++){
  1079. for(k=0; k<f->context_count[i]; k++){
  1080. for(m=0; m<32; m++){
  1081. f->rc_stat2[i][k][m][0] += fs->rc_stat2[i][k][m][0];
  1082. f->rc_stat2[i][k][m][1] += fs->rc_stat2[i][k][m][1];
  1083. }
  1084. }
  1085. }
  1086. }
  1087. for(j=0; j<256; j++){
  1088. snprintf(p, end-p, "%"PRIu64" %"PRIu64" ", f->rc_stat[j][0], f->rc_stat[j][1]);
  1089. p+= strlen(p);
  1090. }
  1091. snprintf(p, end-p, "\n");
  1092. for(i=0; i<f->quant_table_count; i++){
  1093. for(j=0; j<f->context_count[i]; j++){
  1094. for(m=0; m<32; m++){
  1095. snprintf(p, end-p, "%"PRIu64" %"PRIu64" ", f->rc_stat2[i][j][m][0], f->rc_stat2[i][j][m][1]);
  1096. p+= strlen(p);
  1097. }
  1098. }
  1099. }
  1100. snprintf(p, end-p, "%d\n", f->gob_count);
  1101. } else if(avctx->flags&CODEC_FLAG_PASS1)
  1102. avctx->stats_out[0] = '\0';
  1103. f->picture_number++;
  1104. return buf_p-buf;
  1105. }
  1106. #endif /* CONFIG_FFV1_ENCODER */
  1107. static av_cold int common_end(AVCodecContext *avctx){
  1108. FFV1Context *s = avctx->priv_data;
  1109. int i, j;
  1110. if (avctx->codec->decode && s->picture.data[0])
  1111. avctx->release_buffer(avctx, &s->picture);
  1112. for(j=0; j<s->slice_count; j++){
  1113. FFV1Context *fs= s->slice_context[j];
  1114. for(i=0; i<s->plane_count; i++){
  1115. PlaneContext *p= &fs->plane[i];
  1116. av_freep(&p->state);
  1117. av_freep(&p->vlc_state);
  1118. }
  1119. av_freep(&fs->sample_buffer);
  1120. }
  1121. av_freep(&avctx->stats_out);
  1122. for(j=0; j<s->quant_table_count; j++){
  1123. av_freep(&s->initial_states[j]);
  1124. for(i=0; i<s->slice_count; i++){
  1125. FFV1Context *sf= s->slice_context[i];
  1126. av_freep(&sf->rc_stat2[j]);
  1127. }
  1128. av_freep(&s->rc_stat2[j]);
  1129. }
  1130. for(i=0; i<s->slice_count; i++){
  1131. av_freep(&s->slice_context[i]);
  1132. }
  1133. return 0;
  1134. }
  1135. static av_always_inline void decode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
  1136. PlaneContext * const p= &s->plane[plane_index];
  1137. RangeCoder * const c= &s->c;
  1138. int x;
  1139. int run_count=0;
  1140. int run_mode=0;
  1141. int run_index= s->run_index;
  1142. for(x=0; x<w; x++){
  1143. int diff, context, sign;
  1144. context= get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
  1145. if(context < 0){
  1146. context= -context;
  1147. sign=1;
  1148. }else
  1149. sign=0;
  1150. av_assert2(context < p->context_count);
  1151. if(s->ac){
  1152. diff= get_symbol_inline(c, p->state[context], 1);
  1153. }else{
  1154. if(context == 0 && run_mode==0) run_mode=1;
  1155. if(run_mode){
  1156. if(run_count==0 && run_mode==1){
  1157. if(get_bits1(&s->gb)){
  1158. run_count = 1<<ff_log2_run[run_index];
  1159. if(x + run_count <= w) run_index++;
  1160. }else{
  1161. if(ff_log2_run[run_index]) run_count = get_bits(&s->gb, ff_log2_run[run_index]);
  1162. else run_count=0;
  1163. if(run_index) run_index--;
  1164. run_mode=2;
  1165. }
  1166. }
  1167. run_count--;
  1168. if(run_count < 0){
  1169. run_mode=0;
  1170. run_count=0;
  1171. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  1172. if(diff>=0) diff++;
  1173. }else
  1174. diff=0;
  1175. }else
  1176. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  1177. // 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));
  1178. }
  1179. if(sign) diff= -diff;
  1180. sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
  1181. }
  1182. s->run_index= run_index;
  1183. }
  1184. static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  1185. int x, y;
  1186. int_fast16_t *sample[2];
  1187. sample[0]=s->sample_buffer +3;
  1188. sample[1]=s->sample_buffer+w+6+3;
  1189. s->run_index=0;
  1190. memset(s->sample_buffer, 0, 2*(w+6)*sizeof(*s->sample_buffer));
  1191. for(y=0; y<h; y++){
  1192. int_fast16_t *temp= sample[0]; //FIXME try a normal buffer
  1193. sample[0]= sample[1];
  1194. sample[1]= temp;
  1195. sample[1][-1]= sample[0][0 ];
  1196. sample[0][ w]= sample[0][w-1];
  1197. //{START_TIMER
  1198. if(s->avctx->bits_per_raw_sample <= 8){
  1199. decode_line(s, w, sample, plane_index, 8);
  1200. for(x=0; x<w; x++){
  1201. src[x + stride*y]= sample[1][x];
  1202. }
  1203. }else{
  1204. decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
  1205. for(x=0; x<w; x++){
  1206. ((uint16_t*)(src + stride*y))[x]= sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
  1207. }
  1208. }
  1209. //STOP_TIMER("decode-line")}
  1210. }
  1211. }
  1212. static void decode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  1213. int x, y, p;
  1214. int_fast16_t *sample[3][2];
  1215. for(x=0; x<3; x++){
  1216. sample[x][0] = s->sample_buffer + x*2 *(w+6) + 3;
  1217. sample[x][1] = s->sample_buffer + (x*2+1)*(w+6) + 3;
  1218. }
  1219. s->run_index=0;
  1220. memset(s->sample_buffer, 0, 6*(w+6)*sizeof(*s->sample_buffer));
  1221. for(y=0; y<h; y++){
  1222. for(p=0; p<3; p++){
  1223. int_fast16_t *temp= sample[p][0]; //FIXME try a normal buffer
  1224. sample[p][0]= sample[p][1];
  1225. sample[p][1]= temp;
  1226. sample[p][1][-1]= sample[p][0][0 ];
  1227. sample[p][0][ w]= sample[p][0][w-1];
  1228. decode_line(s, w, sample[p], FFMIN(p, 1), 9);
  1229. }
  1230. for(x=0; x<w; x++){
  1231. int g= sample[0][1][x];
  1232. int b= sample[1][1][x];
  1233. int r= sample[2][1][x];
  1234. // assert(g>=0 && b>=0 && r>=0);
  1235. // assert(g<256 && b<512 && r<512);
  1236. b -= 0x100;
  1237. r -= 0x100;
  1238. g -= (b + r)>>2;
  1239. b += g;
  1240. r += g;
  1241. src[x + stride*y]= b + (g<<8) + (r<<16) + (0xFF<<24);
  1242. }
  1243. }
  1244. }
  1245. static int decode_slice(AVCodecContext *c, void *arg){
  1246. FFV1Context *fs= *(void**)arg;
  1247. FFV1Context *f= fs->avctx->priv_data;
  1248. int width = fs->slice_width;
  1249. int height= fs->slice_height;
  1250. int x= fs->slice_x;
  1251. int y= fs->slice_y;
  1252. AVFrame * const p= &f->picture;
  1253. av_assert1(width && height);
  1254. if(f->colorspace==0){
  1255. const int chroma_width = -((-width )>>f->chroma_h_shift);
  1256. const int chroma_height= -((-height)>>f->chroma_v_shift);
  1257. const int cx= x>>f->chroma_h_shift;
  1258. const int cy= y>>f->chroma_v_shift;
  1259. decode_plane(fs, p->data[0] + x + y*p->linesize[0], width, height, p->linesize[0], 0);
  1260. decode_plane(fs, p->data[1] + cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
  1261. decode_plane(fs, p->data[2] + cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[2], 1);
  1262. }else{
  1263. decode_rgb_frame(fs, (uint32_t*)p->data[0] + x + y*(p->linesize[0]/4), width, height, p->linesize[0]/4);
  1264. }
  1265. emms_c();
  1266. return 0;
  1267. }
  1268. static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){
  1269. int v;
  1270. int i=0;
  1271. uint8_t state[CONTEXT_SIZE];
  1272. memset(state, 128, sizeof(state));
  1273. for(v=0; i<128 ; v++){
  1274. int len= get_symbol(c, state, 0) + 1;
  1275. if(len + i > 128) return -1;
  1276. while(len--){
  1277. quant_table[i] = scale*v;
  1278. i++;
  1279. //printf("%2d ",v);
  1280. //if(i%16==0) printf("\n");
  1281. }
  1282. }
  1283. for(i=1; i<128; i++){
  1284. quant_table[256-i]= -quant_table[i];
  1285. }
  1286. quant_table[128]= -quant_table[127];
  1287. return 2*v - 1;
  1288. }
  1289. static int read_quant_tables(RangeCoder *c, int16_t quant_table[MAX_CONTEXT_INPUTS][256]){
  1290. int i;
  1291. int context_count=1;
  1292. for(i=0; i<5; i++){
  1293. context_count*= read_quant_table(c, quant_table[i], context_count);
  1294. if(context_count > 32768U){
  1295. return -1;
  1296. }
  1297. }
  1298. return (context_count+1)/2;
  1299. }
  1300. static int read_extra_header(FFV1Context *f){
  1301. RangeCoder * const c= &f->c;
  1302. uint8_t state[CONTEXT_SIZE];
  1303. int i, j, k;
  1304. uint8_t state2[32][CONTEXT_SIZE];
  1305. memset(state2, 128, sizeof(state2));
  1306. memset(state, 128, sizeof(state));
  1307. ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
  1308. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  1309. f->version= get_symbol(c, state, 0);
  1310. f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
  1311. if(f->ac>1){
  1312. for(i=1; i<256; i++){
  1313. f->state_transition[i]= get_symbol(c, state, 1) + c->one_state[i];
  1314. }
  1315. }
  1316. f->colorspace= get_symbol(c, state, 0); //YUV cs type
  1317. f->avctx->bits_per_raw_sample= get_symbol(c, state, 0);
  1318. get_rac(c, state); //no chroma = false
  1319. f->chroma_h_shift= get_symbol(c, state, 0);
  1320. f->chroma_v_shift= get_symbol(c, state, 0);
  1321. get_rac(c, state); //transparency plane
  1322. f->plane_count= 2;
  1323. f->num_h_slices= 1 + get_symbol(c, state, 0);
  1324. f->num_v_slices= 1 + get_symbol(c, state, 0);
  1325. if(f->num_h_slices > (unsigned)f->width || f->num_v_slices > (unsigned)f->height){
  1326. av_log(f->avctx, AV_LOG_ERROR, "too many slices\n");
  1327. return -1;
  1328. }
  1329. f->quant_table_count= get_symbol(c, state, 0);
  1330. if(f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
  1331. return -1;
  1332. for(i=0; i<f->quant_table_count; i++){
  1333. if((f->context_count[i]= read_quant_tables(c, f->quant_tables[i])) < 0){
  1334. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  1335. return -1;
  1336. }
  1337. }
  1338. if(allocate_initial_states(f) < 0)
  1339. return AVERROR(ENOMEM);
  1340. for(i=0; i<f->quant_table_count; i++){
  1341. if(get_rac(c, state)){
  1342. for(j=0; j<f->context_count[i]; j++){
  1343. for(k=0; k<CONTEXT_SIZE; k++){
  1344. int pred= j ? f->initial_states[i][j-1][k] : 128;
  1345. f->initial_states[i][j][k]= (pred+get_symbol(c, state2[k], 1))&0xFF;
  1346. }
  1347. }
  1348. }
  1349. }
  1350. return 0;
  1351. }
  1352. static int read_header(FFV1Context *f){
  1353. uint8_t state[CONTEXT_SIZE];
  1354. int i, j, context_count;
  1355. RangeCoder * const c= &f->slice_context[0]->c;
  1356. memset(state, 128, sizeof(state));
  1357. if(f->version < 2){
  1358. f->version= get_symbol(c, state, 0);
  1359. f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
  1360. if(f->ac>1){
  1361. for(i=1; i<256; i++){
  1362. f->state_transition[i]= get_symbol(c, state, 1) + c->one_state[i];
  1363. }
  1364. }
  1365. f->colorspace= get_symbol(c, state, 0); //YUV cs type
  1366. if(f->version>0)
  1367. f->avctx->bits_per_raw_sample= get_symbol(c, state, 0);
  1368. get_rac(c, state); //no chroma = false
  1369. f->chroma_h_shift= get_symbol(c, state, 0);
  1370. f->chroma_v_shift= get_symbol(c, state, 0);
  1371. get_rac(c, state); //transparency plane
  1372. f->plane_count= 2;
  1373. }
  1374. if(f->colorspace==0){
  1375. if(f->avctx->bits_per_raw_sample<=8){
  1376. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  1377. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
  1378. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
  1379. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
  1380. case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
  1381. case 0x22: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
  1382. default:
  1383. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  1384. return -1;
  1385. }
  1386. }else{
  1387. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  1388. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P16; break;
  1389. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P16; break;
  1390. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P16; break;
  1391. default:
  1392. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  1393. return -1;
  1394. }
  1395. }
  1396. }else if(f->colorspace==1){
  1397. if(f->chroma_h_shift || f->chroma_v_shift){
  1398. av_log(f->avctx, AV_LOG_ERROR, "chroma subsampling not supported in this colorspace\n");
  1399. return -1;
  1400. }
  1401. f->avctx->pix_fmt= PIX_FMT_RGB32;
  1402. }else{
  1403. av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
  1404. return -1;
  1405. }
  1406. //printf("%d %d %d\n", f->chroma_h_shift, f->chroma_v_shift,f->avctx->pix_fmt);
  1407. if(f->version < 2){
  1408. context_count= read_quant_tables(c, f->quant_table);
  1409. if(context_count < 0){
  1410. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  1411. return -1;
  1412. }
  1413. }else{
  1414. f->slice_count= get_symbol(c, state, 0);
  1415. if(f->slice_count > (unsigned)MAX_SLICES)
  1416. return -1;
  1417. }
  1418. for(j=0; j<f->slice_count; j++){
  1419. FFV1Context *fs= f->slice_context[j];
  1420. fs->ac= f->ac;
  1421. if(f->version >= 2){
  1422. fs->slice_x = get_symbol(c, state, 0) *f->width ;
  1423. fs->slice_y = get_symbol(c, state, 0) *f->height;
  1424. fs->slice_width =(get_symbol(c, state, 0)+1)*f->width + fs->slice_x;
  1425. fs->slice_height=(get_symbol(c, state, 0)+1)*f->height + fs->slice_y;
  1426. fs->slice_x /= f->num_h_slices;
  1427. fs->slice_y /= f->num_v_slices;
  1428. fs->slice_width = fs->slice_width /f->num_h_slices - fs->slice_x;
  1429. fs->slice_height = fs->slice_height/f->num_v_slices - fs->slice_y;
  1430. if((unsigned)fs->slice_width > f->width || (unsigned)fs->slice_height > f->height)
  1431. return -1;
  1432. if( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
  1433. || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
  1434. return -1;
  1435. }
  1436. for(i=0; i<f->plane_count; i++){
  1437. PlaneContext * const p= &fs->plane[i];
  1438. if(f->version >= 2){
  1439. int idx=get_symbol(c, state, 0);
  1440. if(idx > (unsigned)f->quant_table_count){
  1441. av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
  1442. return -1;
  1443. }
  1444. p->quant_table_index= idx;
  1445. memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
  1446. context_count= f->context_count[idx];
  1447. }else{
  1448. memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
  1449. }
  1450. if(p->context_count < context_count){
  1451. av_freep(&p->state);
  1452. av_freep(&p->vlc_state);
  1453. }
  1454. p->context_count= context_count;
  1455. }
  1456. }
  1457. return 0;
  1458. }
  1459. static av_cold int decode_init(AVCodecContext *avctx)
  1460. {
  1461. FFV1Context *f = avctx->priv_data;
  1462. common_init(avctx);
  1463. if(avctx->extradata && read_extra_header(f) < 0)
  1464. return -1;
  1465. if(init_slice_contexts(f) < 0)
  1466. return -1;
  1467. return 0;
  1468. }
  1469. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt){
  1470. const uint8_t *buf = avpkt->data;
  1471. int buf_size = avpkt->size;
  1472. FFV1Context *f = avctx->priv_data;
  1473. RangeCoder * const c= &f->slice_context[0]->c;
  1474. AVFrame * const p= &f->picture;
  1475. int bytes_read, i;
  1476. uint8_t keystate= 128;
  1477. const uint8_t *buf_p;
  1478. AVFrame *picture = data;
  1479. /* release previously stored data */
  1480. if (p->data[0])
  1481. avctx->release_buffer(avctx, p);
  1482. ff_init_range_decoder(c, buf, buf_size);
  1483. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  1484. p->pict_type= FF_I_TYPE; //FIXME I vs. P
  1485. if(get_rac(c, &keystate)){
  1486. p->key_frame= 1;
  1487. if(read_header(f) < 0)
  1488. return -1;
  1489. if(init_slice_state(f) < 0)
  1490. return -1;
  1491. clear_state(f);
  1492. }else{
  1493. p->key_frame= 0;
  1494. }
  1495. if(f->ac>1){
  1496. int i;
  1497. for(i=1; i<256; i++){
  1498. c->one_state[i]= f->state_transition[i];
  1499. c->zero_state[256-i]= 256-c->one_state[i];
  1500. }
  1501. }
  1502. p->reference= 0;
  1503. if(avctx->get_buffer(avctx, p) < 0){
  1504. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1505. return -1;
  1506. }
  1507. if(avctx->debug&FF_DEBUG_PICT_INFO)
  1508. av_log(avctx, AV_LOG_ERROR, "keyframe:%d coder:%d\n", p->key_frame, f->ac);
  1509. if(!f->ac){
  1510. bytes_read = c->bytestream - c->bytestream_start - 1;
  1511. if(bytes_read ==0) av_log(avctx, AV_LOG_ERROR, "error at end of AC stream\n"); //FIXME
  1512. //printf("pos=%d\n", bytes_read);
  1513. init_get_bits(&f->slice_context[0]->gb, buf + bytes_read, buf_size - bytes_read);
  1514. } else {
  1515. bytes_read = 0; /* avoid warning */
  1516. }
  1517. buf_p= buf + buf_size;
  1518. for(i=f->slice_count-1; i>0; i--){
  1519. FFV1Context *fs= f->slice_context[i];
  1520. int v= AV_RB24(buf_p-3)+3;
  1521. if(buf_p - buf <= v){
  1522. av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
  1523. return -1;
  1524. }
  1525. buf_p -= v;
  1526. if(fs->ac){
  1527. ff_init_range_decoder(&fs->c, buf_p, v);
  1528. }else{
  1529. init_get_bits(&fs->gb, buf_p, v);
  1530. }
  1531. }
  1532. avctx->execute(avctx, decode_slice, &f->slice_context[0], NULL, f->slice_count, sizeof(void*));
  1533. f->picture_number++;
  1534. *picture= *p;
  1535. *data_size = sizeof(AVFrame);
  1536. return buf_size;
  1537. }
  1538. AVCodec ff_ffv1_decoder = {
  1539. "ffv1",
  1540. AVMEDIA_TYPE_VIDEO,
  1541. CODEC_ID_FFV1,
  1542. sizeof(FFV1Context),
  1543. decode_init,
  1544. NULL,
  1545. common_end,
  1546. decode_frame,
  1547. CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  1548. NULL,
  1549. .long_name= NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  1550. };
  1551. #if CONFIG_FFV1_ENCODER
  1552. AVCodec ff_ffv1_encoder = {
  1553. "ffv1",
  1554. AVMEDIA_TYPE_VIDEO,
  1555. CODEC_ID_FFV1,
  1556. sizeof(FFV1Context),
  1557. encode_init,
  1558. encode_frame,
  1559. common_end,
  1560. .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV411P, PIX_FMT_YUV410P, PIX_FMT_RGB32, PIX_FMT_YUV420P16, PIX_FMT_YUV422P16, PIX_FMT_YUV444P16, PIX_FMT_NONE},
  1561. .long_name= NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
  1562. };
  1563. #endif