vp3.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410
  1. /*
  2. * Copyright (C) 2003-2004 the ffmpeg project
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * On2 VP3 Video Decoder
  23. *
  24. * VP3 Video Decoder by Mike Melanson (mike at multimedia.cx)
  25. * For more information about the VP3 coding process, visit:
  26. * http://wiki.multimedia.cx/index.php?title=On2_VP3
  27. *
  28. * Theora decoder by Alex Beregszaszi
  29. */
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include "libavutil/imgutils.h"
  34. #include "avcodec.h"
  35. #include "dsputil.h"
  36. #include "get_bits.h"
  37. #include "vp3data.h"
  38. #include "xiph.h"
  39. #include "thread.h"
  40. #define FRAGMENT_PIXELS 8
  41. static av_cold int vp3_decode_end(AVCodecContext *avctx);
  42. static void vp3_decode_flush(AVCodecContext *avctx);
  43. //FIXME split things out into their own arrays
  44. typedef struct Vp3Fragment {
  45. int16_t dc;
  46. uint8_t coding_method;
  47. uint8_t qpi;
  48. } Vp3Fragment;
  49. #define SB_NOT_CODED 0
  50. #define SB_PARTIALLY_CODED 1
  51. #define SB_FULLY_CODED 2
  52. // This is the maximum length of a single long bit run that can be encoded
  53. // for superblock coding or block qps. Theora special-cases this to read a
  54. // bit instead of flipping the current bit to allow for runs longer than 4129.
  55. #define MAXIMUM_LONG_BIT_RUN 4129
  56. #define MODE_INTER_NO_MV 0
  57. #define MODE_INTRA 1
  58. #define MODE_INTER_PLUS_MV 2
  59. #define MODE_INTER_LAST_MV 3
  60. #define MODE_INTER_PRIOR_LAST 4
  61. #define MODE_USING_GOLDEN 5
  62. #define MODE_GOLDEN_MV 6
  63. #define MODE_INTER_FOURMV 7
  64. #define CODING_MODE_COUNT 8
  65. /* special internal mode */
  66. #define MODE_COPY 8
  67. /* There are 6 preset schemes, plus a free-form scheme */
  68. static const int ModeAlphabet[6][CODING_MODE_COUNT] =
  69. {
  70. /* scheme 1: Last motion vector dominates */
  71. { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
  72. MODE_INTER_PLUS_MV, MODE_INTER_NO_MV,
  73. MODE_INTRA, MODE_USING_GOLDEN,
  74. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  75. /* scheme 2 */
  76. { MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
  77. MODE_INTER_NO_MV, MODE_INTER_PLUS_MV,
  78. MODE_INTRA, MODE_USING_GOLDEN,
  79. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  80. /* scheme 3 */
  81. { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
  82. MODE_INTER_PRIOR_LAST, MODE_INTER_NO_MV,
  83. MODE_INTRA, MODE_USING_GOLDEN,
  84. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  85. /* scheme 4 */
  86. { MODE_INTER_LAST_MV, MODE_INTER_PLUS_MV,
  87. MODE_INTER_NO_MV, MODE_INTER_PRIOR_LAST,
  88. MODE_INTRA, MODE_USING_GOLDEN,
  89. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  90. /* scheme 5: No motion vector dominates */
  91. { MODE_INTER_NO_MV, MODE_INTER_LAST_MV,
  92. MODE_INTER_PRIOR_LAST, MODE_INTER_PLUS_MV,
  93. MODE_INTRA, MODE_USING_GOLDEN,
  94. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  95. /* scheme 6 */
  96. { MODE_INTER_NO_MV, MODE_USING_GOLDEN,
  97. MODE_INTER_LAST_MV, MODE_INTER_PRIOR_LAST,
  98. MODE_INTER_PLUS_MV, MODE_INTRA,
  99. MODE_GOLDEN_MV, MODE_INTER_FOURMV },
  100. };
  101. static const uint8_t hilbert_offset[16][2] = {
  102. {0,0}, {1,0}, {1,1}, {0,1},
  103. {0,2}, {0,3}, {1,3}, {1,2},
  104. {2,2}, {2,3}, {3,3}, {3,2},
  105. {3,1}, {2,1}, {2,0}, {3,0}
  106. };
  107. #define MIN_DEQUANT_VAL 2
  108. typedef struct Vp3DecodeContext {
  109. AVCodecContext *avctx;
  110. int theora, theora_tables;
  111. int version;
  112. int width, height;
  113. int chroma_x_shift, chroma_y_shift;
  114. AVFrame golden_frame;
  115. AVFrame last_frame;
  116. AVFrame current_frame;
  117. int keyframe;
  118. DSPContext dsp;
  119. int flipped_image;
  120. int last_slice_end;
  121. int skip_loop_filter;
  122. int qps[3];
  123. int nqps;
  124. int last_qps[3];
  125. int superblock_count;
  126. int y_superblock_width;
  127. int y_superblock_height;
  128. int y_superblock_count;
  129. int c_superblock_width;
  130. int c_superblock_height;
  131. int c_superblock_count;
  132. int u_superblock_start;
  133. int v_superblock_start;
  134. unsigned char *superblock_coding;
  135. int macroblock_count;
  136. int macroblock_width;
  137. int macroblock_height;
  138. int fragment_count;
  139. int fragment_width[2];
  140. int fragment_height[2];
  141. Vp3Fragment *all_fragments;
  142. int fragment_start[3];
  143. int data_offset[3];
  144. int8_t (*motion_val[2])[2];
  145. ScanTable scantable;
  146. /* tables */
  147. uint16_t coded_dc_scale_factor[64];
  148. uint32_t coded_ac_scale_factor[64];
  149. uint8_t base_matrix[384][64];
  150. uint8_t qr_count[2][3];
  151. uint8_t qr_size [2][3][64];
  152. uint16_t qr_base[2][3][64];
  153. /**
  154. * This is a list of all tokens in bitstream order. Reordering takes place
  155. * by pulling from each level during IDCT. As a consequence, IDCT must be
  156. * in Hilbert order, making the minimum slice height 64 for 4:2:0 and 32
  157. * otherwise. The 32 different tokens with up to 12 bits of extradata are
  158. * collapsed into 3 types, packed as follows:
  159. * (from the low to high bits)
  160. *
  161. * 2 bits: type (0,1,2)
  162. * 0: EOB run, 14 bits for run length (12 needed)
  163. * 1: zero run, 7 bits for run length
  164. * 7 bits for the next coefficient (3 needed)
  165. * 2: coefficient, 14 bits (11 needed)
  166. *
  167. * Coefficients are signed, so are packed in the highest bits for automatic
  168. * sign extension.
  169. */
  170. int16_t *dct_tokens[3][64];
  171. int16_t *dct_tokens_base;
  172. #define TOKEN_EOB(eob_run) ((eob_run) << 2)
  173. #define TOKEN_ZERO_RUN(coeff, zero_run) (((coeff) << 9) + ((zero_run) << 2) + 1)
  174. #define TOKEN_COEFF(coeff) (((coeff) << 2) + 2)
  175. /**
  176. * number of blocks that contain DCT coefficients at the given level or higher
  177. */
  178. int num_coded_frags[3][64];
  179. int total_num_coded_frags;
  180. /* this is a list of indexes into the all_fragments array indicating
  181. * which of the fragments are coded */
  182. int *coded_fragment_list[3];
  183. VLC dc_vlc[16];
  184. VLC ac_vlc_1[16];
  185. VLC ac_vlc_2[16];
  186. VLC ac_vlc_3[16];
  187. VLC ac_vlc_4[16];
  188. VLC superblock_run_length_vlc;
  189. VLC fragment_run_length_vlc;
  190. VLC mode_code_vlc;
  191. VLC motion_vector_vlc;
  192. /* these arrays need to be on 16-byte boundaries since SSE2 operations
  193. * index into them */
  194. DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64]; //<qmat[qpi][is_inter][plane]
  195. /* This table contains superblock_count * 16 entries. Each set of 16
  196. * numbers corresponds to the fragment indexes 0..15 of the superblock.
  197. * An entry will be -1 to indicate that no entry corresponds to that
  198. * index. */
  199. int *superblock_fragments;
  200. /* This is an array that indicates how a particular macroblock
  201. * is coded. */
  202. unsigned char *macroblock_coding;
  203. uint8_t *edge_emu_buffer;
  204. /* Huffman decode */
  205. int hti;
  206. unsigned int hbits;
  207. int entries;
  208. int huff_code_size;
  209. uint32_t huffman_table[80][32][2];
  210. uint8_t filter_limit_values[64];
  211. DECLARE_ALIGNED(8, int, bounding_values_array)[256+2];
  212. } Vp3DecodeContext;
  213. /************************************************************************
  214. * VP3 specific functions
  215. ************************************************************************/
  216. /*
  217. * This function sets up all of the various blocks mappings:
  218. * superblocks <-> fragments, macroblocks <-> fragments,
  219. * superblocks <-> macroblocks
  220. *
  221. * @return 0 is successful; returns 1 if *anything* went wrong.
  222. */
  223. static int init_block_mapping(Vp3DecodeContext *s)
  224. {
  225. int sb_x, sb_y, plane;
  226. int x, y, i, j = 0;
  227. for (plane = 0; plane < 3; plane++) {
  228. int sb_width = plane ? s->c_superblock_width : s->y_superblock_width;
  229. int sb_height = plane ? s->c_superblock_height : s->y_superblock_height;
  230. int frag_width = s->fragment_width[!!plane];
  231. int frag_height = s->fragment_height[!!plane];
  232. for (sb_y = 0; sb_y < sb_height; sb_y++)
  233. for (sb_x = 0; sb_x < sb_width; sb_x++)
  234. for (i = 0; i < 16; i++) {
  235. x = 4*sb_x + hilbert_offset[i][0];
  236. y = 4*sb_y + hilbert_offset[i][1];
  237. if (x < frag_width && y < frag_height)
  238. s->superblock_fragments[j++] = s->fragment_start[plane] + y*frag_width + x;
  239. else
  240. s->superblock_fragments[j++] = -1;
  241. }
  242. }
  243. return 0; /* successful path out */
  244. }
  245. /*
  246. * This function sets up the dequantization tables used for a particular
  247. * frame.
  248. */
  249. static void init_dequantizer(Vp3DecodeContext *s, int qpi)
  250. {
  251. int ac_scale_factor = s->coded_ac_scale_factor[s->qps[qpi]];
  252. int dc_scale_factor = s->coded_dc_scale_factor[s->qps[qpi]];
  253. int i, plane, inter, qri, bmi, bmj, qistart;
  254. for(inter=0; inter<2; inter++){
  255. for(plane=0; plane<3; plane++){
  256. int sum=0;
  257. for(qri=0; qri<s->qr_count[inter][plane]; qri++){
  258. sum+= s->qr_size[inter][plane][qri];
  259. if(s->qps[qpi] <= sum)
  260. break;
  261. }
  262. qistart= sum - s->qr_size[inter][plane][qri];
  263. bmi= s->qr_base[inter][plane][qri ];
  264. bmj= s->qr_base[inter][plane][qri+1];
  265. for(i=0; i<64; i++){
  266. int coeff= ( 2*(sum -s->qps[qpi])*s->base_matrix[bmi][i]
  267. - 2*(qistart-s->qps[qpi])*s->base_matrix[bmj][i]
  268. + s->qr_size[inter][plane][qri])
  269. / (2*s->qr_size[inter][plane][qri]);
  270. int qmin= 8<<(inter + !i);
  271. int qscale= i ? ac_scale_factor : dc_scale_factor;
  272. s->qmat[qpi][inter][plane][s->dsp.idct_permutation[i]]= av_clip((qscale * coeff)/100 * 4, qmin, 4096);
  273. }
  274. // all DC coefficients use the same quant so as not to interfere with DC prediction
  275. s->qmat[qpi][inter][plane][0] = s->qmat[0][inter][plane][0];
  276. }
  277. }
  278. }
  279. /*
  280. * This function initializes the loop filter boundary limits if the frame's
  281. * quality index is different from the previous frame's.
  282. *
  283. * The filter_limit_values may not be larger than 127.
  284. */
  285. static void init_loop_filter(Vp3DecodeContext *s)
  286. {
  287. int *bounding_values= s->bounding_values_array+127;
  288. int filter_limit;
  289. int x;
  290. int value;
  291. filter_limit = s->filter_limit_values[s->qps[0]];
  292. /* set up the bounding values */
  293. memset(s->bounding_values_array, 0, 256 * sizeof(int));
  294. for (x = 0; x < filter_limit; x++) {
  295. bounding_values[-x] = -x;
  296. bounding_values[x] = x;
  297. }
  298. for (x = value = filter_limit; x < 128 && value; x++, value--) {
  299. bounding_values[ x] = value;
  300. bounding_values[-x] = -value;
  301. }
  302. if (value)
  303. bounding_values[128] = value;
  304. bounding_values[129] = bounding_values[130] = filter_limit * 0x02020202;
  305. }
  306. /*
  307. * This function unpacks all of the superblock/macroblock/fragment coding
  308. * information from the bitstream.
  309. */
  310. static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)
  311. {
  312. int superblock_starts[3] = { 0, s->u_superblock_start, s->v_superblock_start };
  313. int bit = 0;
  314. int current_superblock = 0;
  315. int current_run = 0;
  316. int num_partial_superblocks = 0;
  317. int i, j;
  318. int current_fragment;
  319. int plane;
  320. if (s->keyframe) {
  321. memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);
  322. } else {
  323. /* unpack the list of partially-coded superblocks */
  324. bit = get_bits1(gb) ^ 1;
  325. current_run = 0;
  326. while (current_superblock < s->superblock_count && get_bits_left(gb) > 0) {
  327. if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
  328. bit = get_bits1(gb);
  329. else
  330. bit ^= 1;
  331. current_run = get_vlc2(gb,
  332. s->superblock_run_length_vlc.table, 6, 2) + 1;
  333. if (current_run == 34)
  334. current_run += get_bits(gb, 12);
  335. if (current_superblock + current_run > s->superblock_count) {
  336. av_log(s->avctx, AV_LOG_ERROR, "Invalid partially coded superblock run length\n");
  337. return -1;
  338. }
  339. memset(s->superblock_coding + current_superblock, bit, current_run);
  340. current_superblock += current_run;
  341. if (bit)
  342. num_partial_superblocks += current_run;
  343. }
  344. /* unpack the list of fully coded superblocks if any of the blocks were
  345. * not marked as partially coded in the previous step */
  346. if (num_partial_superblocks < s->superblock_count) {
  347. int superblocks_decoded = 0;
  348. current_superblock = 0;
  349. bit = get_bits1(gb) ^ 1;
  350. current_run = 0;
  351. while (superblocks_decoded < s->superblock_count - num_partial_superblocks
  352. && get_bits_left(gb) > 0) {
  353. if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)
  354. bit = get_bits1(gb);
  355. else
  356. bit ^= 1;
  357. current_run = get_vlc2(gb,
  358. s->superblock_run_length_vlc.table, 6, 2) + 1;
  359. if (current_run == 34)
  360. current_run += get_bits(gb, 12);
  361. for (j = 0; j < current_run; current_superblock++) {
  362. if (current_superblock >= s->superblock_count) {
  363. av_log(s->avctx, AV_LOG_ERROR, "Invalid fully coded superblock run length\n");
  364. return -1;
  365. }
  366. /* skip any superblocks already marked as partially coded */
  367. if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {
  368. s->superblock_coding[current_superblock] = 2*bit;
  369. j++;
  370. }
  371. }
  372. superblocks_decoded += current_run;
  373. }
  374. }
  375. /* if there were partial blocks, initialize bitstream for
  376. * unpacking fragment codings */
  377. if (num_partial_superblocks) {
  378. current_run = 0;
  379. bit = get_bits1(gb);
  380. /* toggle the bit because as soon as the first run length is
  381. * fetched the bit will be toggled again */
  382. bit ^= 1;
  383. }
  384. }
  385. /* figure out which fragments are coded; iterate through each
  386. * superblock (all planes) */
  387. s->total_num_coded_frags = 0;
  388. memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);
  389. for (plane = 0; plane < 3; plane++) {
  390. int sb_start = superblock_starts[plane];
  391. int sb_end = sb_start + (plane ? s->c_superblock_count : s->y_superblock_count);
  392. int num_coded_frags = 0;
  393. for (i = sb_start; i < sb_end && get_bits_left(gb) > 0; i++) {
  394. /* iterate through all 16 fragments in a superblock */
  395. for (j = 0; j < 16; j++) {
  396. /* if the fragment is in bounds, check its coding status */
  397. current_fragment = s->superblock_fragments[i * 16 + j];
  398. if (current_fragment != -1) {
  399. int coded = s->superblock_coding[i];
  400. if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {
  401. /* fragment may or may not be coded; this is the case
  402. * that cares about the fragment coding runs */
  403. if (current_run-- == 0) {
  404. bit ^= 1;
  405. current_run = get_vlc2(gb,
  406. s->fragment_run_length_vlc.table, 5, 2);
  407. }
  408. coded = bit;
  409. }
  410. if (coded) {
  411. /* default mode; actual mode will be decoded in
  412. * the next phase */
  413. s->all_fragments[current_fragment].coding_method =
  414. MODE_INTER_NO_MV;
  415. s->coded_fragment_list[plane][num_coded_frags++] =
  416. current_fragment;
  417. } else {
  418. /* not coded; copy this fragment from the prior frame */
  419. s->all_fragments[current_fragment].coding_method =
  420. MODE_COPY;
  421. }
  422. }
  423. }
  424. }
  425. s->total_num_coded_frags += num_coded_frags;
  426. for (i = 0; i < 64; i++)
  427. s->num_coded_frags[plane][i] = num_coded_frags;
  428. if (plane < 2)
  429. s->coded_fragment_list[plane+1] = s->coded_fragment_list[plane] + num_coded_frags;
  430. }
  431. return 0;
  432. }
  433. /*
  434. * This function unpacks all the coding mode data for individual macroblocks
  435. * from the bitstream.
  436. */
  437. static int unpack_modes(Vp3DecodeContext *s, GetBitContext *gb)
  438. {
  439. int i, j, k, sb_x, sb_y;
  440. int scheme;
  441. int current_macroblock;
  442. int current_fragment;
  443. int coding_mode;
  444. int custom_mode_alphabet[CODING_MODE_COUNT];
  445. const int *alphabet;
  446. Vp3Fragment *frag;
  447. if (s->keyframe) {
  448. for (i = 0; i < s->fragment_count; i++)
  449. s->all_fragments[i].coding_method = MODE_INTRA;
  450. } else {
  451. /* fetch the mode coding scheme for this frame */
  452. scheme = get_bits(gb, 3);
  453. /* is it a custom coding scheme? */
  454. if (scheme == 0) {
  455. for (i = 0; i < 8; i++)
  456. custom_mode_alphabet[i] = MODE_INTER_NO_MV;
  457. for (i = 0; i < 8; i++)
  458. custom_mode_alphabet[get_bits(gb, 3)] = i;
  459. alphabet = custom_mode_alphabet;
  460. } else
  461. alphabet = ModeAlphabet[scheme-1];
  462. /* iterate through all of the macroblocks that contain 1 or more
  463. * coded fragments */
  464. for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
  465. for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
  466. if (get_bits_left(gb) <= 0)
  467. return -1;
  468. for (j = 0; j < 4; j++) {
  469. int mb_x = 2*sb_x + (j>>1);
  470. int mb_y = 2*sb_y + (((j>>1)+j)&1);
  471. current_macroblock = mb_y * s->macroblock_width + mb_x;
  472. if (mb_x >= s->macroblock_width || mb_y >= s->macroblock_height)
  473. continue;
  474. #define BLOCK_X (2*mb_x + (k&1))
  475. #define BLOCK_Y (2*mb_y + (k>>1))
  476. /* coding modes are only stored if the macroblock has at least one
  477. * luma block coded, otherwise it must be INTER_NO_MV */
  478. for (k = 0; k < 4; k++) {
  479. current_fragment = BLOCK_Y*s->fragment_width[0] + BLOCK_X;
  480. if (s->all_fragments[current_fragment].coding_method != MODE_COPY)
  481. break;
  482. }
  483. if (k == 4) {
  484. s->macroblock_coding[current_macroblock] = MODE_INTER_NO_MV;
  485. continue;
  486. }
  487. /* mode 7 means get 3 bits for each coding mode */
  488. if (scheme == 7)
  489. coding_mode = get_bits(gb, 3);
  490. else
  491. coding_mode = alphabet
  492. [get_vlc2(gb, s->mode_code_vlc.table, 3, 3)];
  493. s->macroblock_coding[current_macroblock] = coding_mode;
  494. for (k = 0; k < 4; k++) {
  495. frag = s->all_fragments + BLOCK_Y*s->fragment_width[0] + BLOCK_X;
  496. if (frag->coding_method != MODE_COPY)
  497. frag->coding_method = coding_mode;
  498. }
  499. #define SET_CHROMA_MODES \
  500. if (frag[s->fragment_start[1]].coding_method != MODE_COPY) \
  501. frag[s->fragment_start[1]].coding_method = coding_mode;\
  502. if (frag[s->fragment_start[2]].coding_method != MODE_COPY) \
  503. frag[s->fragment_start[2]].coding_method = coding_mode;
  504. if (s->chroma_y_shift) {
  505. frag = s->all_fragments + mb_y*s->fragment_width[1] + mb_x;
  506. SET_CHROMA_MODES
  507. } else if (s->chroma_x_shift) {
  508. frag = s->all_fragments + 2*mb_y*s->fragment_width[1] + mb_x;
  509. for (k = 0; k < 2; k++) {
  510. SET_CHROMA_MODES
  511. frag += s->fragment_width[1];
  512. }
  513. } else {
  514. for (k = 0; k < 4; k++) {
  515. frag = s->all_fragments + BLOCK_Y*s->fragment_width[1] + BLOCK_X;
  516. SET_CHROMA_MODES
  517. }
  518. }
  519. }
  520. }
  521. }
  522. }
  523. return 0;
  524. }
  525. /*
  526. * This function unpacks all the motion vectors for the individual
  527. * macroblocks from the bitstream.
  528. */
  529. static int unpack_vectors(Vp3DecodeContext *s, GetBitContext *gb)
  530. {
  531. int j, k, sb_x, sb_y;
  532. int coding_mode;
  533. int motion_x[4];
  534. int motion_y[4];
  535. int last_motion_x = 0;
  536. int last_motion_y = 0;
  537. int prior_last_motion_x = 0;
  538. int prior_last_motion_y = 0;
  539. int current_macroblock;
  540. int current_fragment;
  541. int frag;
  542. if (s->keyframe)
  543. return 0;
  544. /* coding mode 0 is the VLC scheme; 1 is the fixed code scheme */
  545. coding_mode = get_bits1(gb);
  546. /* iterate through all of the macroblocks that contain 1 or more
  547. * coded fragments */
  548. for (sb_y = 0; sb_y < s->y_superblock_height; sb_y++) {
  549. for (sb_x = 0; sb_x < s->y_superblock_width; sb_x++) {
  550. if (get_bits_left(gb) <= 0)
  551. return -1;
  552. for (j = 0; j < 4; j++) {
  553. int mb_x = 2*sb_x + (j>>1);
  554. int mb_y = 2*sb_y + (((j>>1)+j)&1);
  555. current_macroblock = mb_y * s->macroblock_width + mb_x;
  556. if (mb_x >= s->macroblock_width || mb_y >= s->macroblock_height ||
  557. (s->macroblock_coding[current_macroblock] == MODE_COPY))
  558. continue;
  559. switch (s->macroblock_coding[current_macroblock]) {
  560. case MODE_INTER_PLUS_MV:
  561. case MODE_GOLDEN_MV:
  562. /* all 6 fragments use the same motion vector */
  563. if (coding_mode == 0) {
  564. motion_x[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  565. motion_y[0] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  566. } else {
  567. motion_x[0] = fixed_motion_vector_table[get_bits(gb, 6)];
  568. motion_y[0] = fixed_motion_vector_table[get_bits(gb, 6)];
  569. }
  570. /* vector maintenance, only on MODE_INTER_PLUS_MV */
  571. if (s->macroblock_coding[current_macroblock] ==
  572. MODE_INTER_PLUS_MV) {
  573. prior_last_motion_x = last_motion_x;
  574. prior_last_motion_y = last_motion_y;
  575. last_motion_x = motion_x[0];
  576. last_motion_y = motion_y[0];
  577. }
  578. break;
  579. case MODE_INTER_FOURMV:
  580. /* vector maintenance */
  581. prior_last_motion_x = last_motion_x;
  582. prior_last_motion_y = last_motion_y;
  583. /* fetch 4 vectors from the bitstream, one for each
  584. * Y fragment, then average for the C fragment vectors */
  585. for (k = 0; k < 4; k++) {
  586. current_fragment = BLOCK_Y*s->fragment_width[0] + BLOCK_X;
  587. if (s->all_fragments[current_fragment].coding_method != MODE_COPY) {
  588. if (coding_mode == 0) {
  589. motion_x[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  590. motion_y[k] = motion_vector_table[get_vlc2(gb, s->motion_vector_vlc.table, 6, 2)];
  591. } else {
  592. motion_x[k] = fixed_motion_vector_table[get_bits(gb, 6)];
  593. motion_y[k] = fixed_motion_vector_table[get_bits(gb, 6)];
  594. }
  595. last_motion_x = motion_x[k];
  596. last_motion_y = motion_y[k];
  597. } else {
  598. motion_x[k] = 0;
  599. motion_y[k] = 0;
  600. }
  601. }
  602. break;
  603. case MODE_INTER_LAST_MV:
  604. /* all 6 fragments use the last motion vector */
  605. motion_x[0] = last_motion_x;
  606. motion_y[0] = last_motion_y;
  607. /* no vector maintenance (last vector remains the
  608. * last vector) */
  609. break;
  610. case MODE_INTER_PRIOR_LAST:
  611. /* all 6 fragments use the motion vector prior to the
  612. * last motion vector */
  613. motion_x[0] = prior_last_motion_x;
  614. motion_y[0] = prior_last_motion_y;
  615. /* vector maintenance */
  616. prior_last_motion_x = last_motion_x;
  617. prior_last_motion_y = last_motion_y;
  618. last_motion_x = motion_x[0];
  619. last_motion_y = motion_y[0];
  620. break;
  621. default:
  622. /* covers intra, inter without MV, golden without MV */
  623. motion_x[0] = 0;
  624. motion_y[0] = 0;
  625. /* no vector maintenance */
  626. break;
  627. }
  628. /* assign the motion vectors to the correct fragments */
  629. for (k = 0; k < 4; k++) {
  630. current_fragment =
  631. BLOCK_Y*s->fragment_width[0] + BLOCK_X;
  632. if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
  633. s->motion_val[0][current_fragment][0] = motion_x[k];
  634. s->motion_val[0][current_fragment][1] = motion_y[k];
  635. } else {
  636. s->motion_val[0][current_fragment][0] = motion_x[0];
  637. s->motion_val[0][current_fragment][1] = motion_y[0];
  638. }
  639. }
  640. if (s->chroma_y_shift) {
  641. if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
  642. motion_x[0] = RSHIFT(motion_x[0] + motion_x[1] + motion_x[2] + motion_x[3], 2);
  643. motion_y[0] = RSHIFT(motion_y[0] + motion_y[1] + motion_y[2] + motion_y[3], 2);
  644. }
  645. motion_x[0] = (motion_x[0]>>1) | (motion_x[0]&1);
  646. motion_y[0] = (motion_y[0]>>1) | (motion_y[0]&1);
  647. frag = mb_y*s->fragment_width[1] + mb_x;
  648. s->motion_val[1][frag][0] = motion_x[0];
  649. s->motion_val[1][frag][1] = motion_y[0];
  650. } else if (s->chroma_x_shift) {
  651. if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
  652. motion_x[0] = RSHIFT(motion_x[0] + motion_x[1], 1);
  653. motion_y[0] = RSHIFT(motion_y[0] + motion_y[1], 1);
  654. motion_x[1] = RSHIFT(motion_x[2] + motion_x[3], 1);
  655. motion_y[1] = RSHIFT(motion_y[2] + motion_y[3], 1);
  656. } else {
  657. motion_x[1] = motion_x[0];
  658. motion_y[1] = motion_y[0];
  659. }
  660. motion_x[0] = (motion_x[0]>>1) | (motion_x[0]&1);
  661. motion_x[1] = (motion_x[1]>>1) | (motion_x[1]&1);
  662. frag = 2*mb_y*s->fragment_width[1] + mb_x;
  663. for (k = 0; k < 2; k++) {
  664. s->motion_val[1][frag][0] = motion_x[k];
  665. s->motion_val[1][frag][1] = motion_y[k];
  666. frag += s->fragment_width[1];
  667. }
  668. } else {
  669. for (k = 0; k < 4; k++) {
  670. frag = BLOCK_Y*s->fragment_width[1] + BLOCK_X;
  671. if (s->macroblock_coding[current_macroblock] == MODE_INTER_FOURMV) {
  672. s->motion_val[1][frag][0] = motion_x[k];
  673. s->motion_val[1][frag][1] = motion_y[k];
  674. } else {
  675. s->motion_val[1][frag][0] = motion_x[0];
  676. s->motion_val[1][frag][1] = motion_y[0];
  677. }
  678. }
  679. }
  680. }
  681. }
  682. }
  683. return 0;
  684. }
  685. static int unpack_block_qpis(Vp3DecodeContext *s, GetBitContext *gb)
  686. {
  687. int qpi, i, j, bit, run_length, blocks_decoded, num_blocks_at_qpi;
  688. int num_blocks = s->total_num_coded_frags;
  689. for (qpi = 0; qpi < s->nqps-1 && num_blocks > 0; qpi++) {
  690. i = blocks_decoded = num_blocks_at_qpi = 0;
  691. bit = get_bits1(gb) ^ 1;
  692. run_length = 0;
  693. do {
  694. if (run_length == MAXIMUM_LONG_BIT_RUN)
  695. bit = get_bits1(gb);
  696. else
  697. bit ^= 1;
  698. run_length = get_vlc2(gb, s->superblock_run_length_vlc.table, 6, 2) + 1;
  699. if (run_length == 34)
  700. run_length += get_bits(gb, 12);
  701. blocks_decoded += run_length;
  702. if (!bit)
  703. num_blocks_at_qpi += run_length;
  704. for (j = 0; j < run_length; i++) {
  705. if (i >= s->total_num_coded_frags)
  706. return -1;
  707. if (s->all_fragments[s->coded_fragment_list[0][i]].qpi == qpi) {
  708. s->all_fragments[s->coded_fragment_list[0][i]].qpi += bit;
  709. j++;
  710. }
  711. }
  712. } while (blocks_decoded < num_blocks && get_bits_left(gb) > 0);
  713. num_blocks -= num_blocks_at_qpi;
  714. }
  715. return 0;
  716. }
  717. /*
  718. * This function is called by unpack_dct_coeffs() to extract the VLCs from
  719. * the bitstream. The VLCs encode tokens which are used to unpack DCT
  720. * data. This function unpacks all the VLCs for either the Y plane or both
  721. * C planes, and is called for DC coefficients or different AC coefficient
  722. * levels (since different coefficient types require different VLC tables.
  723. *
  724. * This function returns a residual eob run. E.g, if a particular token gave
  725. * instructions to EOB the next 5 fragments and there were only 2 fragments
  726. * left in the current fragment range, 3 would be returned so that it could
  727. * be passed into the next call to this same function.
  728. */
  729. static int unpack_vlcs(Vp3DecodeContext *s, GetBitContext *gb,
  730. VLC *table, int coeff_index,
  731. int plane,
  732. int eob_run)
  733. {
  734. int i, j = 0;
  735. int token;
  736. int zero_run = 0;
  737. DCTELEM coeff = 0;
  738. int bits_to_get;
  739. int blocks_ended;
  740. int coeff_i = 0;
  741. int num_coeffs = s->num_coded_frags[plane][coeff_index];
  742. int16_t *dct_tokens = s->dct_tokens[plane][coeff_index];
  743. /* local references to structure members to avoid repeated deferences */
  744. int *coded_fragment_list = s->coded_fragment_list[plane];
  745. Vp3Fragment *all_fragments = s->all_fragments;
  746. VLC_TYPE (*vlc_table)[2] = table->table;
  747. if (num_coeffs < 0)
  748. av_log(s->avctx, AV_LOG_ERROR, "Invalid number of coefficents at level %d\n", coeff_index);
  749. if (eob_run > num_coeffs) {
  750. coeff_i = blocks_ended = num_coeffs;
  751. eob_run -= num_coeffs;
  752. } else {
  753. coeff_i = blocks_ended = eob_run;
  754. eob_run = 0;
  755. }
  756. // insert fake EOB token to cover the split between planes or zzi
  757. if (blocks_ended)
  758. dct_tokens[j++] = blocks_ended << 2;
  759. while (coeff_i < num_coeffs && get_bits_left(gb) > 0) {
  760. /* decode a VLC into a token */
  761. token = get_vlc2(gb, vlc_table, 11, 3);
  762. /* use the token to get a zero run, a coefficient, and an eob run */
  763. if ((unsigned) token <= 6U) {
  764. eob_run = eob_run_base[token];
  765. if (eob_run_get_bits[token])
  766. eob_run += get_bits(gb, eob_run_get_bits[token]);
  767. // record only the number of blocks ended in this plane,
  768. // any spill will be recorded in the next plane.
  769. if (eob_run > num_coeffs - coeff_i) {
  770. dct_tokens[j++] = TOKEN_EOB(num_coeffs - coeff_i);
  771. blocks_ended += num_coeffs - coeff_i;
  772. eob_run -= num_coeffs - coeff_i;
  773. coeff_i = num_coeffs;
  774. } else {
  775. dct_tokens[j++] = TOKEN_EOB(eob_run);
  776. blocks_ended += eob_run;
  777. coeff_i += eob_run;
  778. eob_run = 0;
  779. }
  780. } else if (token >= 0) {
  781. bits_to_get = coeff_get_bits[token];
  782. if (bits_to_get)
  783. bits_to_get = get_bits(gb, bits_to_get);
  784. coeff = coeff_tables[token][bits_to_get];
  785. zero_run = zero_run_base[token];
  786. if (zero_run_get_bits[token])
  787. zero_run += get_bits(gb, zero_run_get_bits[token]);
  788. if (zero_run) {
  789. dct_tokens[j++] = TOKEN_ZERO_RUN(coeff, zero_run);
  790. } else {
  791. // Save DC into the fragment structure. DC prediction is
  792. // done in raster order, so the actual DC can't be in with
  793. // other tokens. We still need the token in dct_tokens[]
  794. // however, or else the structure collapses on itself.
  795. if (!coeff_index)
  796. all_fragments[coded_fragment_list[coeff_i]].dc = coeff;
  797. dct_tokens[j++] = TOKEN_COEFF(coeff);
  798. }
  799. if (coeff_index + zero_run > 64) {
  800. av_log(s->avctx, AV_LOG_DEBUG, "Invalid zero run of %d with"
  801. " %d coeffs left\n", zero_run, 64-coeff_index);
  802. zero_run = 64 - coeff_index;
  803. }
  804. // zero runs code multiple coefficients,
  805. // so don't try to decode coeffs for those higher levels
  806. for (i = coeff_index+1; i <= coeff_index+zero_run; i++)
  807. s->num_coded_frags[plane][i]--;
  808. coeff_i++;
  809. } else {
  810. av_log(s->avctx, AV_LOG_ERROR,
  811. "Invalid token %d\n", token);
  812. return -1;
  813. }
  814. }
  815. if (blocks_ended > s->num_coded_frags[plane][coeff_index])
  816. av_log(s->avctx, AV_LOG_ERROR, "More blocks ended than coded!\n");
  817. // decrement the number of blocks that have higher coeffecients for each
  818. // EOB run at this level
  819. if (blocks_ended)
  820. for (i = coeff_index+1; i < 64; i++)
  821. s->num_coded_frags[plane][i] -= blocks_ended;
  822. // setup the next buffer
  823. if (plane < 2)
  824. s->dct_tokens[plane+1][coeff_index] = dct_tokens + j;
  825. else if (coeff_index < 63)
  826. s->dct_tokens[0][coeff_index+1] = dct_tokens + j;
  827. return eob_run;
  828. }
  829. static void reverse_dc_prediction(Vp3DecodeContext *s,
  830. int first_fragment,
  831. int fragment_width,
  832. int fragment_height);
  833. /*
  834. * This function unpacks all of the DCT coefficient data from the
  835. * bitstream.
  836. */
  837. static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)
  838. {
  839. int i;
  840. int dc_y_table;
  841. int dc_c_table;
  842. int ac_y_table;
  843. int ac_c_table;
  844. int residual_eob_run = 0;
  845. VLC *y_tables[64];
  846. VLC *c_tables[64];
  847. s->dct_tokens[0][0] = s->dct_tokens_base;
  848. /* fetch the DC table indexes */
  849. dc_y_table = get_bits(gb, 4);
  850. dc_c_table = get_bits(gb, 4);
  851. /* unpack the Y plane DC coefficients */
  852. residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
  853. 0, residual_eob_run);
  854. if (residual_eob_run < 0)
  855. return residual_eob_run;
  856. /* reverse prediction of the Y-plane DC coefficients */
  857. reverse_dc_prediction(s, 0, s->fragment_width[0], s->fragment_height[0]);
  858. /* unpack the C plane DC coefficients */
  859. residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
  860. 1, residual_eob_run);
  861. if (residual_eob_run < 0)
  862. return residual_eob_run;
  863. residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
  864. 2, residual_eob_run);
  865. if (residual_eob_run < 0)
  866. return residual_eob_run;
  867. /* reverse prediction of the C-plane DC coefficients */
  868. if (!(s->avctx->flags & CODEC_FLAG_GRAY))
  869. {
  870. reverse_dc_prediction(s, s->fragment_start[1],
  871. s->fragment_width[1], s->fragment_height[1]);
  872. reverse_dc_prediction(s, s->fragment_start[2],
  873. s->fragment_width[1], s->fragment_height[1]);
  874. }
  875. /* fetch the AC table indexes */
  876. ac_y_table = get_bits(gb, 4);
  877. ac_c_table = get_bits(gb, 4);
  878. /* build tables of AC VLC tables */
  879. for (i = 1; i <= 5; i++) {
  880. y_tables[i] = &s->ac_vlc_1[ac_y_table];
  881. c_tables[i] = &s->ac_vlc_1[ac_c_table];
  882. }
  883. for (i = 6; i <= 14; i++) {
  884. y_tables[i] = &s->ac_vlc_2[ac_y_table];
  885. c_tables[i] = &s->ac_vlc_2[ac_c_table];
  886. }
  887. for (i = 15; i <= 27; i++) {
  888. y_tables[i] = &s->ac_vlc_3[ac_y_table];
  889. c_tables[i] = &s->ac_vlc_3[ac_c_table];
  890. }
  891. for (i = 28; i <= 63; i++) {
  892. y_tables[i] = &s->ac_vlc_4[ac_y_table];
  893. c_tables[i] = &s->ac_vlc_4[ac_c_table];
  894. }
  895. /* decode all AC coefficents */
  896. for (i = 1; i <= 63; i++) {
  897. residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i,
  898. 0, residual_eob_run);
  899. if (residual_eob_run < 0)
  900. return residual_eob_run;
  901. residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
  902. 1, residual_eob_run);
  903. if (residual_eob_run < 0)
  904. return residual_eob_run;
  905. residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
  906. 2, residual_eob_run);
  907. if (residual_eob_run < 0)
  908. return residual_eob_run;
  909. }
  910. return 0;
  911. }
  912. /*
  913. * This function reverses the DC prediction for each coded fragment in
  914. * the frame. Much of this function is adapted directly from the original
  915. * VP3 source code.
  916. */
  917. #define COMPATIBLE_FRAME(x) \
  918. (compatible_frame[s->all_fragments[x].coding_method] == current_frame_type)
  919. #define DC_COEFF(u) s->all_fragments[u].dc
  920. static void reverse_dc_prediction(Vp3DecodeContext *s,
  921. int first_fragment,
  922. int fragment_width,
  923. int fragment_height)
  924. {
  925. #define PUL 8
  926. #define PU 4
  927. #define PUR 2
  928. #define PL 1
  929. int x, y;
  930. int i = first_fragment;
  931. int predicted_dc;
  932. /* DC values for the left, up-left, up, and up-right fragments */
  933. int vl, vul, vu, vur;
  934. /* indexes for the left, up-left, up, and up-right fragments */
  935. int l, ul, u, ur;
  936. /*
  937. * The 6 fields mean:
  938. * 0: up-left multiplier
  939. * 1: up multiplier
  940. * 2: up-right multiplier
  941. * 3: left multiplier
  942. */
  943. static const int predictor_transform[16][4] = {
  944. { 0, 0, 0, 0},
  945. { 0, 0, 0,128}, // PL
  946. { 0, 0,128, 0}, // PUR
  947. { 0, 0, 53, 75}, // PUR|PL
  948. { 0,128, 0, 0}, // PU
  949. { 0, 64, 0, 64}, // PU|PL
  950. { 0,128, 0, 0}, // PU|PUR
  951. { 0, 0, 53, 75}, // PU|PUR|PL
  952. {128, 0, 0, 0}, // PUL
  953. { 0, 0, 0,128}, // PUL|PL
  954. { 64, 0, 64, 0}, // PUL|PUR
  955. { 0, 0, 53, 75}, // PUL|PUR|PL
  956. { 0,128, 0, 0}, // PUL|PU
  957. {-104,116, 0,116}, // PUL|PU|PL
  958. { 24, 80, 24, 0}, // PUL|PU|PUR
  959. {-104,116, 0,116} // PUL|PU|PUR|PL
  960. };
  961. /* This table shows which types of blocks can use other blocks for
  962. * prediction. For example, INTRA is the only mode in this table to
  963. * have a frame number of 0. That means INTRA blocks can only predict
  964. * from other INTRA blocks. There are 2 golden frame coding types;
  965. * blocks encoding in these modes can only predict from other blocks
  966. * that were encoded with these 1 of these 2 modes. */
  967. static const unsigned char compatible_frame[9] = {
  968. 1, /* MODE_INTER_NO_MV */
  969. 0, /* MODE_INTRA */
  970. 1, /* MODE_INTER_PLUS_MV */
  971. 1, /* MODE_INTER_LAST_MV */
  972. 1, /* MODE_INTER_PRIOR_MV */
  973. 2, /* MODE_USING_GOLDEN */
  974. 2, /* MODE_GOLDEN_MV */
  975. 1, /* MODE_INTER_FOUR_MV */
  976. 3 /* MODE_COPY */
  977. };
  978. int current_frame_type;
  979. /* there is a last DC predictor for each of the 3 frame types */
  980. short last_dc[3];
  981. int transform = 0;
  982. vul = vu = vur = vl = 0;
  983. last_dc[0] = last_dc[1] = last_dc[2] = 0;
  984. /* for each fragment row... */
  985. for (y = 0; y < fragment_height; y++) {
  986. /* for each fragment in a row... */
  987. for (x = 0; x < fragment_width; x++, i++) {
  988. /* reverse prediction if this block was coded */
  989. if (s->all_fragments[i].coding_method != MODE_COPY) {
  990. current_frame_type =
  991. compatible_frame[s->all_fragments[i].coding_method];
  992. transform= 0;
  993. if(x){
  994. l= i-1;
  995. vl = DC_COEFF(l);
  996. if(COMPATIBLE_FRAME(l))
  997. transform |= PL;
  998. }
  999. if(y){
  1000. u= i-fragment_width;
  1001. vu = DC_COEFF(u);
  1002. if(COMPATIBLE_FRAME(u))
  1003. transform |= PU;
  1004. if(x){
  1005. ul= i-fragment_width-1;
  1006. vul = DC_COEFF(ul);
  1007. if(COMPATIBLE_FRAME(ul))
  1008. transform |= PUL;
  1009. }
  1010. if(x + 1 < fragment_width){
  1011. ur= i-fragment_width+1;
  1012. vur = DC_COEFF(ur);
  1013. if(COMPATIBLE_FRAME(ur))
  1014. transform |= PUR;
  1015. }
  1016. }
  1017. if (transform == 0) {
  1018. /* if there were no fragments to predict from, use last
  1019. * DC saved */
  1020. predicted_dc = last_dc[current_frame_type];
  1021. } else {
  1022. /* apply the appropriate predictor transform */
  1023. predicted_dc =
  1024. (predictor_transform[transform][0] * vul) +
  1025. (predictor_transform[transform][1] * vu) +
  1026. (predictor_transform[transform][2] * vur) +
  1027. (predictor_transform[transform][3] * vl);
  1028. predicted_dc /= 128;
  1029. /* check for outranging on the [ul u l] and
  1030. * [ul u ur l] predictors */
  1031. if ((transform == 15) || (transform == 13)) {
  1032. if (FFABS(predicted_dc - vu) > 128)
  1033. predicted_dc = vu;
  1034. else if (FFABS(predicted_dc - vl) > 128)
  1035. predicted_dc = vl;
  1036. else if (FFABS(predicted_dc - vul) > 128)
  1037. predicted_dc = vul;
  1038. }
  1039. }
  1040. /* at long last, apply the predictor */
  1041. DC_COEFF(i) += predicted_dc;
  1042. /* save the DC */
  1043. last_dc[current_frame_type] = DC_COEFF(i);
  1044. }
  1045. }
  1046. }
  1047. }
  1048. static void apply_loop_filter(Vp3DecodeContext *s, int plane, int ystart, int yend)
  1049. {
  1050. int x, y;
  1051. int *bounding_values= s->bounding_values_array+127;
  1052. int width = s->fragment_width[!!plane];
  1053. int height = s->fragment_height[!!plane];
  1054. int fragment = s->fragment_start [plane] + ystart * width;
  1055. int stride = s->current_frame.linesize[plane];
  1056. uint8_t *plane_data = s->current_frame.data [plane];
  1057. if (!s->flipped_image) stride = -stride;
  1058. plane_data += s->data_offset[plane] + 8*ystart*stride;
  1059. for (y = ystart; y < yend; y++) {
  1060. for (x = 0; x < width; x++) {
  1061. /* This code basically just deblocks on the edges of coded blocks.
  1062. * However, it has to be much more complicated because of the
  1063. * braindamaged deblock ordering used in VP3/Theora. Order matters
  1064. * because some pixels get filtered twice. */
  1065. if( s->all_fragments[fragment].coding_method != MODE_COPY )
  1066. {
  1067. /* do not perform left edge filter for left columns frags */
  1068. if (x > 0) {
  1069. s->dsp.vp3_h_loop_filter(
  1070. plane_data + 8*x,
  1071. stride, bounding_values);
  1072. }
  1073. /* do not perform top edge filter for top row fragments */
  1074. if (y > 0) {
  1075. s->dsp.vp3_v_loop_filter(
  1076. plane_data + 8*x,
  1077. stride, bounding_values);
  1078. }
  1079. /* do not perform right edge filter for right column
  1080. * fragments or if right fragment neighbor is also coded
  1081. * in this frame (it will be filtered in next iteration) */
  1082. if ((x < width - 1) &&
  1083. (s->all_fragments[fragment + 1].coding_method == MODE_COPY)) {
  1084. s->dsp.vp3_h_loop_filter(
  1085. plane_data + 8*x + 8,
  1086. stride, bounding_values);
  1087. }
  1088. /* do not perform bottom edge filter for bottom row
  1089. * fragments or if bottom fragment neighbor is also coded
  1090. * in this frame (it will be filtered in the next row) */
  1091. if ((y < height - 1) &&
  1092. (s->all_fragments[fragment + width].coding_method == MODE_COPY)) {
  1093. s->dsp.vp3_v_loop_filter(
  1094. plane_data + 8*x + 8*stride,
  1095. stride, bounding_values);
  1096. }
  1097. }
  1098. fragment++;
  1099. }
  1100. plane_data += 8*stride;
  1101. }
  1102. }
  1103. /**
  1104. * Pull DCT tokens from the 64 levels to decode and dequant the coefficients
  1105. * for the next block in coding order
  1106. */
  1107. static inline int vp3_dequant(Vp3DecodeContext *s, Vp3Fragment *frag,
  1108. int plane, int inter, DCTELEM block[64])
  1109. {
  1110. int16_t *dequantizer = s->qmat[frag->qpi][inter][plane];
  1111. uint8_t *perm = s->scantable.permutated;
  1112. int i = 0;
  1113. do {
  1114. int token = *s->dct_tokens[plane][i];
  1115. switch (token & 3) {
  1116. case 0: // EOB
  1117. if (--token < 4) // 0-3 are token types, so the EOB run must now be 0
  1118. s->dct_tokens[plane][i]++;
  1119. else
  1120. *s->dct_tokens[plane][i] = token & ~3;
  1121. goto end;
  1122. case 1: // zero run
  1123. s->dct_tokens[plane][i]++;
  1124. i += (token >> 2) & 0x7f;
  1125. if(i>63){
  1126. av_log(s->avctx, AV_LOG_ERROR, "Coefficient index overflow\n");
  1127. return -1;
  1128. }
  1129. block[perm[i]] = (token >> 9) * dequantizer[perm[i]];
  1130. i++;
  1131. break;
  1132. case 2: // coeff
  1133. block[perm[i]] = (token >> 2) * dequantizer[perm[i]];
  1134. s->dct_tokens[plane][i++]++;
  1135. break;
  1136. default: // shouldn't happen
  1137. return i;
  1138. }
  1139. } while (i < 64);
  1140. end:
  1141. // the actual DC+prediction is in the fragment structure
  1142. block[0] = frag->dc * s->qmat[0][inter][plane][0];
  1143. return i;
  1144. }
  1145. /**
  1146. * called when all pixels up to row y are complete
  1147. */
  1148. static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y)
  1149. {
  1150. int h, cy;
  1151. int offset[4];
  1152. if (HAVE_PTHREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  1153. int y_flipped = s->flipped_image ? s->avctx->height-y : y;
  1154. // At the end of the frame, report INT_MAX instead of the height of the frame.
  1155. // This makes the other threads' ff_thread_await_progress() calls cheaper, because
  1156. // they don't have to clip their values.
  1157. ff_thread_report_progress(&s->current_frame, y_flipped==s->avctx->height ? INT_MAX : y_flipped-1, 0);
  1158. }
  1159. if(s->avctx->draw_horiz_band==NULL)
  1160. return;
  1161. h= y - s->last_slice_end;
  1162. s->last_slice_end= y;
  1163. y -= h;
  1164. if (!s->flipped_image) {
  1165. y = s->avctx->height - y - h;
  1166. }
  1167. cy = y >> s->chroma_y_shift;
  1168. offset[0] = s->current_frame.linesize[0]*y;
  1169. offset[1] = s->current_frame.linesize[1]*cy;
  1170. offset[2] = s->current_frame.linesize[2]*cy;
  1171. offset[3] = 0;
  1172. emms_c();
  1173. s->avctx->draw_horiz_band(s->avctx, &s->current_frame, offset, y, 3, h);
  1174. }
  1175. /**
  1176. * Wait for the reference frame of the current fragment.
  1177. * The progress value is in luma pixel rows.
  1178. */
  1179. static void await_reference_row(Vp3DecodeContext *s, Vp3Fragment *fragment, int motion_y, int y)
  1180. {
  1181. AVFrame *ref_frame;
  1182. int ref_row;
  1183. int border = motion_y&1;
  1184. if (fragment->coding_method == MODE_USING_GOLDEN ||
  1185. fragment->coding_method == MODE_GOLDEN_MV)
  1186. ref_frame = &s->golden_frame;
  1187. else
  1188. ref_frame = &s->last_frame;
  1189. ref_row = y + (motion_y>>1);
  1190. ref_row = FFMAX(FFABS(ref_row), ref_row + 8 + border);
  1191. ff_thread_await_progress(ref_frame, ref_row, 0);
  1192. }
  1193. /*
  1194. * Perform the final rendering for a particular slice of data.
  1195. * The slice number ranges from 0..(c_superblock_height - 1).
  1196. */
  1197. static void render_slice(Vp3DecodeContext *s, int slice)
  1198. {
  1199. int x, y, i, j, fragment;
  1200. LOCAL_ALIGNED_16(DCTELEM, block, [64]);
  1201. int motion_x = 0xdeadbeef, motion_y = 0xdeadbeef;
  1202. int motion_halfpel_index;
  1203. uint8_t *motion_source;
  1204. int plane, first_pixel;
  1205. if (slice >= s->c_superblock_height)
  1206. return;
  1207. for (plane = 0; plane < 3; plane++) {
  1208. uint8_t *output_plane = s->current_frame.data [plane] + s->data_offset[plane];
  1209. uint8_t * last_plane = s-> last_frame.data [plane] + s->data_offset[plane];
  1210. uint8_t *golden_plane = s-> golden_frame.data [plane] + s->data_offset[plane];
  1211. int stride = s->current_frame.linesize[plane];
  1212. int plane_width = s->width >> (plane && s->chroma_x_shift);
  1213. int plane_height = s->height >> (plane && s->chroma_y_shift);
  1214. int8_t (*motion_val)[2] = s->motion_val[!!plane];
  1215. int sb_x, sb_y = slice << (!plane && s->chroma_y_shift);
  1216. int slice_height = sb_y + 1 + (!plane && s->chroma_y_shift);
  1217. int slice_width = plane ? s->c_superblock_width : s->y_superblock_width;
  1218. int fragment_width = s->fragment_width[!!plane];
  1219. int fragment_height = s->fragment_height[!!plane];
  1220. int fragment_start = s->fragment_start[plane];
  1221. int do_await = !plane && HAVE_PTHREADS && (s->avctx->active_thread_type&FF_THREAD_FRAME);
  1222. if (!s->flipped_image) stride = -stride;
  1223. if (CONFIG_GRAY && plane && (s->avctx->flags & CODEC_FLAG_GRAY))
  1224. continue;
  1225. /* for each superblock row in the slice (both of them)... */
  1226. for (; sb_y < slice_height; sb_y++) {
  1227. /* for each superblock in a row... */
  1228. for (sb_x = 0; sb_x < slice_width; sb_x++) {
  1229. /* for each block in a superblock... */
  1230. for (j = 0; j < 16; j++) {
  1231. x = 4*sb_x + hilbert_offset[j][0];
  1232. y = 4*sb_y + hilbert_offset[j][1];
  1233. fragment = y*fragment_width + x;
  1234. i = fragment_start + fragment;
  1235. // bounds check
  1236. if (x >= fragment_width || y >= fragment_height)
  1237. continue;
  1238. first_pixel = 8*y*stride + 8*x;
  1239. if (do_await && s->all_fragments[i].coding_method != MODE_INTRA)
  1240. await_reference_row(s, &s->all_fragments[i], motion_val[fragment][1], (16*y) >> s->chroma_y_shift);
  1241. /* transform if this block was coded */
  1242. if (s->all_fragments[i].coding_method != MODE_COPY) {
  1243. if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
  1244. (s->all_fragments[i].coding_method == MODE_GOLDEN_MV))
  1245. motion_source= golden_plane;
  1246. else
  1247. motion_source= last_plane;
  1248. motion_source += first_pixel;
  1249. motion_halfpel_index = 0;
  1250. /* sort out the motion vector if this fragment is coded
  1251. * using a motion vector method */
  1252. if ((s->all_fragments[i].coding_method > MODE_INTRA) &&
  1253. (s->all_fragments[i].coding_method != MODE_USING_GOLDEN)) {
  1254. int src_x, src_y;
  1255. motion_x = motion_val[fragment][0];
  1256. motion_y = motion_val[fragment][1];
  1257. src_x= (motion_x>>1) + 8*x;
  1258. src_y= (motion_y>>1) + 8*y;
  1259. motion_halfpel_index = motion_x & 0x01;
  1260. motion_source += (motion_x >> 1);
  1261. motion_halfpel_index |= (motion_y & 0x01) << 1;
  1262. motion_source += ((motion_y >> 1) * stride);
  1263. if(src_x<0 || src_y<0 || src_x + 9 >= plane_width || src_y + 9 >= plane_height){
  1264. uint8_t *temp= s->edge_emu_buffer;
  1265. if(stride<0) temp -= 8*stride;
  1266. s->dsp.emulated_edge_mc(temp, motion_source, stride, 9, 9, src_x, src_y, plane_width, plane_height);
  1267. motion_source= temp;
  1268. }
  1269. }
  1270. /* first, take care of copying a block from either the
  1271. * previous or the golden frame */
  1272. if (s->all_fragments[i].coding_method != MODE_INTRA) {
  1273. /* Note, it is possible to implement all MC cases with
  1274. put_no_rnd_pixels_l2 which would look more like the
  1275. VP3 source but this would be slower as
  1276. put_no_rnd_pixels_tab is better optimzed */
  1277. if(motion_halfpel_index != 3){
  1278. s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
  1279. output_plane + first_pixel,
  1280. motion_source, stride, 8);
  1281. }else{
  1282. int d= (motion_x ^ motion_y)>>31; // d is 0 if motion_x and _y have the same sign, else -1
  1283. s->dsp.put_no_rnd_pixels_l2[1](
  1284. output_plane + first_pixel,
  1285. motion_source - d,
  1286. motion_source + stride + 1 + d,
  1287. stride, 8);
  1288. }
  1289. }
  1290. s->dsp.clear_block(block);
  1291. /* invert DCT and place (or add) in final output */
  1292. if (s->all_fragments[i].coding_method == MODE_INTRA) {
  1293. vp3_dequant(s, s->all_fragments + i, plane, 0, block);
  1294. if(s->avctx->idct_algo!=FF_IDCT_VP3)
  1295. block[0] += 128<<3;
  1296. s->dsp.idct_put(
  1297. output_plane + first_pixel,
  1298. stride,
  1299. block);
  1300. } else {
  1301. if (vp3_dequant(s, s->all_fragments + i, plane, 1, block)) {
  1302. s->dsp.idct_add(
  1303. output_plane + first_pixel,
  1304. stride,
  1305. block);
  1306. } else {
  1307. s->dsp.vp3_idct_dc_add(output_plane + first_pixel, stride, block);
  1308. }
  1309. }
  1310. } else {
  1311. /* copy directly from the previous frame */
  1312. s->dsp.put_pixels_tab[1][0](
  1313. output_plane + first_pixel,
  1314. last_plane + first_pixel,
  1315. stride, 8);
  1316. }
  1317. }
  1318. }
  1319. // Filter up to the last row in the superblock row
  1320. if (!s->skip_loop_filter)
  1321. apply_loop_filter(s, plane, 4*sb_y - !!sb_y, FFMIN(4*sb_y+3, fragment_height-1));
  1322. }
  1323. }
  1324. /* this looks like a good place for slice dispatch... */
  1325. /* algorithm:
  1326. * if (slice == s->macroblock_height - 1)
  1327. * dispatch (both last slice & 2nd-to-last slice);
  1328. * else if (slice > 0)
  1329. * dispatch (slice - 1);
  1330. */
  1331. vp3_draw_horiz_band(s, FFMIN((32 << s->chroma_y_shift) * (slice + 1) -16, s->height-16));
  1332. }
  1333. /// Allocate tables for per-frame data in Vp3DecodeContext
  1334. static av_cold int allocate_tables(AVCodecContext *avctx)
  1335. {
  1336. Vp3DecodeContext *s = avctx->priv_data;
  1337. int y_fragment_count, c_fragment_count;
  1338. y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
  1339. c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
  1340. s->superblock_coding = av_malloc(s->superblock_count);
  1341. s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
  1342. s->coded_fragment_list[0] = av_malloc(s->fragment_count * sizeof(int));
  1343. s->dct_tokens_base = av_malloc(64*s->fragment_count * sizeof(*s->dct_tokens_base));
  1344. s->motion_val[0] = av_malloc(y_fragment_count * sizeof(*s->motion_val[0]));
  1345. s->motion_val[1] = av_malloc(c_fragment_count * sizeof(*s->motion_val[1]));
  1346. /* work out the block mapping tables */
  1347. s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
  1348. s->macroblock_coding = av_malloc(s->macroblock_count + 1);
  1349. if (!s->superblock_coding || !s->all_fragments || !s->dct_tokens_base ||
  1350. !s->coded_fragment_list[0] || !s->superblock_fragments || !s->macroblock_coding ||
  1351. !s->motion_val[0] || !s->motion_val[1]) {
  1352. vp3_decode_end(avctx);
  1353. return -1;
  1354. }
  1355. init_block_mapping(s);
  1356. return 0;
  1357. }
  1358. /*
  1359. * This is the ffmpeg/libavcodec API init function.
  1360. */
  1361. static av_cold int vp3_decode_init(AVCodecContext *avctx)
  1362. {
  1363. Vp3DecodeContext *s = avctx->priv_data;
  1364. int i, inter, plane;
  1365. int c_width;
  1366. int c_height;
  1367. int y_fragment_count, c_fragment_count;
  1368. if (avctx->codec_tag == MKTAG('V','P','3','0'))
  1369. s->version = 0;
  1370. else
  1371. s->version = 1;
  1372. s->avctx = avctx;
  1373. s->width = FFALIGN(avctx->width, 16);
  1374. s->height = FFALIGN(avctx->height, 16);
  1375. if (avctx->pix_fmt == PIX_FMT_NONE)
  1376. avctx->pix_fmt = PIX_FMT_YUV420P;
  1377. avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
  1378. if(avctx->idct_algo==FF_IDCT_AUTO)
  1379. avctx->idct_algo=FF_IDCT_VP3;
  1380. dsputil_init(&s->dsp, avctx);
  1381. ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
  1382. /* initialize to an impossible value which will force a recalculation
  1383. * in the first frame decode */
  1384. for (i = 0; i < 3; i++)
  1385. s->qps[i] = -1;
  1386. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
  1387. s->y_superblock_width = (s->width + 31) / 32;
  1388. s->y_superblock_height = (s->height + 31) / 32;
  1389. s->y_superblock_count = s->y_superblock_width * s->y_superblock_height;
  1390. /* work out the dimensions for the C planes */
  1391. c_width = s->width >> s->chroma_x_shift;
  1392. c_height = s->height >> s->chroma_y_shift;
  1393. s->c_superblock_width = (c_width + 31) / 32;
  1394. s->c_superblock_height = (c_height + 31) / 32;
  1395. s->c_superblock_count = s->c_superblock_width * s->c_superblock_height;
  1396. s->superblock_count = s->y_superblock_count + (s->c_superblock_count * 2);
  1397. s->u_superblock_start = s->y_superblock_count;
  1398. s->v_superblock_start = s->u_superblock_start + s->c_superblock_count;
  1399. s->macroblock_width = (s->width + 15) / 16;
  1400. s->macroblock_height = (s->height + 15) / 16;
  1401. s->macroblock_count = s->macroblock_width * s->macroblock_height;
  1402. s->fragment_width[0] = s->width / FRAGMENT_PIXELS;
  1403. s->fragment_height[0] = s->height / FRAGMENT_PIXELS;
  1404. s->fragment_width[1] = s->fragment_width[0] >> s->chroma_x_shift;
  1405. s->fragment_height[1] = s->fragment_height[0] >> s->chroma_y_shift;
  1406. /* fragment count covers all 8x8 blocks for all 3 planes */
  1407. y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
  1408. c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
  1409. s->fragment_count = y_fragment_count + 2*c_fragment_count;
  1410. s->fragment_start[1] = y_fragment_count;
  1411. s->fragment_start[2] = y_fragment_count + c_fragment_count;
  1412. if (!s->theora_tables)
  1413. {
  1414. for (i = 0; i < 64; i++) {
  1415. s->coded_dc_scale_factor[i] = vp31_dc_scale_factor[i];
  1416. s->coded_ac_scale_factor[i] = vp31_ac_scale_factor[i];
  1417. s->base_matrix[0][i] = vp31_intra_y_dequant[i];
  1418. s->base_matrix[1][i] = vp31_intra_c_dequant[i];
  1419. s->base_matrix[2][i] = vp31_inter_dequant[i];
  1420. s->filter_limit_values[i] = vp31_filter_limit_values[i];
  1421. }
  1422. for(inter=0; inter<2; inter++){
  1423. for(plane=0; plane<3; plane++){
  1424. s->qr_count[inter][plane]= 1;
  1425. s->qr_size [inter][plane][0]= 63;
  1426. s->qr_base [inter][plane][0]=
  1427. s->qr_base [inter][plane][1]= 2*inter + (!!plane)*!inter;
  1428. }
  1429. }
  1430. /* init VLC tables */
  1431. for (i = 0; i < 16; i++) {
  1432. /* DC histograms */
  1433. init_vlc(&s->dc_vlc[i], 11, 32,
  1434. &dc_bias[i][0][1], 4, 2,
  1435. &dc_bias[i][0][0], 4, 2, 0);
  1436. /* group 1 AC histograms */
  1437. init_vlc(&s->ac_vlc_1[i], 11, 32,
  1438. &ac_bias_0[i][0][1], 4, 2,
  1439. &ac_bias_0[i][0][0], 4, 2, 0);
  1440. /* group 2 AC histograms */
  1441. init_vlc(&s->ac_vlc_2[i], 11, 32,
  1442. &ac_bias_1[i][0][1], 4, 2,
  1443. &ac_bias_1[i][0][0], 4, 2, 0);
  1444. /* group 3 AC histograms */
  1445. init_vlc(&s->ac_vlc_3[i], 11, 32,
  1446. &ac_bias_2[i][0][1], 4, 2,
  1447. &ac_bias_2[i][0][0], 4, 2, 0);
  1448. /* group 4 AC histograms */
  1449. init_vlc(&s->ac_vlc_4[i], 11, 32,
  1450. &ac_bias_3[i][0][1], 4, 2,
  1451. &ac_bias_3[i][0][0], 4, 2, 0);
  1452. }
  1453. } else {
  1454. for (i = 0; i < 16; i++) {
  1455. /* DC histograms */
  1456. if (init_vlc(&s->dc_vlc[i], 11, 32,
  1457. &s->huffman_table[i][0][1], 8, 4,
  1458. &s->huffman_table[i][0][0], 8, 4, 0) < 0)
  1459. goto vlc_fail;
  1460. /* group 1 AC histograms */
  1461. if (init_vlc(&s->ac_vlc_1[i], 11, 32,
  1462. &s->huffman_table[i+16][0][1], 8, 4,
  1463. &s->huffman_table[i+16][0][0], 8, 4, 0) < 0)
  1464. goto vlc_fail;
  1465. /* group 2 AC histograms */
  1466. if (init_vlc(&s->ac_vlc_2[i], 11, 32,
  1467. &s->huffman_table[i+16*2][0][1], 8, 4,
  1468. &s->huffman_table[i+16*2][0][0], 8, 4, 0) < 0)
  1469. goto vlc_fail;
  1470. /* group 3 AC histograms */
  1471. if (init_vlc(&s->ac_vlc_3[i], 11, 32,
  1472. &s->huffman_table[i+16*3][0][1], 8, 4,
  1473. &s->huffman_table[i+16*3][0][0], 8, 4, 0) < 0)
  1474. goto vlc_fail;
  1475. /* group 4 AC histograms */
  1476. if (init_vlc(&s->ac_vlc_4[i], 11, 32,
  1477. &s->huffman_table[i+16*4][0][1], 8, 4,
  1478. &s->huffman_table[i+16*4][0][0], 8, 4, 0) < 0)
  1479. goto vlc_fail;
  1480. }
  1481. }
  1482. init_vlc(&s->superblock_run_length_vlc, 6, 34,
  1483. &superblock_run_length_vlc_table[0][1], 4, 2,
  1484. &superblock_run_length_vlc_table[0][0], 4, 2, 0);
  1485. init_vlc(&s->fragment_run_length_vlc, 5, 30,
  1486. &fragment_run_length_vlc_table[0][1], 4, 2,
  1487. &fragment_run_length_vlc_table[0][0], 4, 2, 0);
  1488. init_vlc(&s->mode_code_vlc, 3, 8,
  1489. &mode_code_vlc_table[0][1], 2, 1,
  1490. &mode_code_vlc_table[0][0], 2, 1, 0);
  1491. init_vlc(&s->motion_vector_vlc, 6, 63,
  1492. &motion_vector_vlc_table[0][1], 2, 1,
  1493. &motion_vector_vlc_table[0][0], 2, 1, 0);
  1494. for (i = 0; i < 3; i++) {
  1495. s->current_frame.data[i] = NULL;
  1496. s->last_frame.data[i] = NULL;
  1497. s->golden_frame.data[i] = NULL;
  1498. }
  1499. return allocate_tables(avctx);
  1500. vlc_fail:
  1501. av_log(avctx, AV_LOG_FATAL, "Invalid huffman table\n");
  1502. return -1;
  1503. }
  1504. /// Release and shuffle frames after decode finishes
  1505. static void update_frames(AVCodecContext *avctx)
  1506. {
  1507. Vp3DecodeContext *s = avctx->priv_data;
  1508. /* release the last frame, if it is allocated and if it is not the
  1509. * golden frame */
  1510. if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY)
  1511. ff_thread_release_buffer(avctx, &s->last_frame);
  1512. /* shuffle frames (last = current) */
  1513. s->last_frame= s->current_frame;
  1514. if (s->keyframe) {
  1515. if (s->golden_frame.data[0])
  1516. ff_thread_release_buffer(avctx, &s->golden_frame);
  1517. s->golden_frame = s->current_frame;
  1518. s->last_frame.type = FF_BUFFER_TYPE_COPY;
  1519. }
  1520. s->current_frame.data[0]= NULL; /* ensure that we catch any access to this released frame */
  1521. }
  1522. static int vp3_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
  1523. {
  1524. Vp3DecodeContext *s = dst->priv_data, *s1 = src->priv_data;
  1525. int qps_changed = 0, i, err;
  1526. #define copy_fields(to, from, start_field, end_field) memcpy(&to->start_field, &from->start_field, (char*)&to->end_field - (char*)&to->start_field)
  1527. if (!s1->current_frame.data[0]
  1528. ||s->width != s1->width
  1529. ||s->height!= s1->height) {
  1530. if (s != s1)
  1531. copy_fields(s, s1, golden_frame, current_frame);
  1532. return -1;
  1533. }
  1534. if (s != s1) {
  1535. // init tables if the first frame hasn't been decoded
  1536. if (!s->current_frame.data[0]) {
  1537. int y_fragment_count, c_fragment_count;
  1538. s->avctx = dst;
  1539. err = allocate_tables(dst);
  1540. if (err)
  1541. return err;
  1542. y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
  1543. c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
  1544. memcpy(s->motion_val[0], s1->motion_val[0], y_fragment_count * sizeof(*s->motion_val[0]));
  1545. memcpy(s->motion_val[1], s1->motion_val[1], c_fragment_count * sizeof(*s->motion_val[1]));
  1546. }
  1547. // copy previous frame data
  1548. copy_fields(s, s1, golden_frame, dsp);
  1549. // copy qscale data if necessary
  1550. for (i = 0; i < 3; i++) {
  1551. if (s->qps[i] != s1->qps[1]) {
  1552. qps_changed = 1;
  1553. memcpy(&s->qmat[i], &s1->qmat[i], sizeof(s->qmat[i]));
  1554. }
  1555. }
  1556. if (s->qps[0] != s1->qps[0])
  1557. memcpy(&s->bounding_values_array, &s1->bounding_values_array, sizeof(s->bounding_values_array));
  1558. if (qps_changed)
  1559. copy_fields(s, s1, qps, superblock_count);
  1560. #undef copy_fields
  1561. }
  1562. update_frames(dst);
  1563. return 0;
  1564. }
  1565. /*
  1566. * This is the ffmpeg/libavcodec API frame decode function.
  1567. */
  1568. static int vp3_decode_frame(AVCodecContext *avctx,
  1569. void *data, int *data_size,
  1570. AVPacket *avpkt)
  1571. {
  1572. const uint8_t *buf = avpkt->data;
  1573. int buf_size = avpkt->size;
  1574. Vp3DecodeContext *s = avctx->priv_data;
  1575. GetBitContext gb;
  1576. int i;
  1577. init_get_bits(&gb, buf, buf_size * 8);
  1578. if (s->theora && get_bits1(&gb))
  1579. {
  1580. av_log(avctx, AV_LOG_ERROR, "Header packet passed to frame decoder, skipping\n");
  1581. return -1;
  1582. }
  1583. s->keyframe = !get_bits1(&gb);
  1584. if (!s->theora)
  1585. skip_bits(&gb, 1);
  1586. for (i = 0; i < 3; i++)
  1587. s->last_qps[i] = s->qps[i];
  1588. s->nqps=0;
  1589. do{
  1590. s->qps[s->nqps++]= get_bits(&gb, 6);
  1591. } while(s->theora >= 0x030200 && s->nqps<3 && get_bits1(&gb));
  1592. for (i = s->nqps; i < 3; i++)
  1593. s->qps[i] = -1;
  1594. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1595. av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
  1596. s->keyframe?"key":"", avctx->frame_number+1, s->qps[0]);
  1597. s->skip_loop_filter = !s->filter_limit_values[s->qps[0]] ||
  1598. avctx->skip_loop_filter >= (s->keyframe ? AVDISCARD_ALL : AVDISCARD_NONKEY);
  1599. if (s->qps[0] != s->last_qps[0])
  1600. init_loop_filter(s);
  1601. for (i = 0; i < s->nqps; i++)
  1602. // reinit all dequantizers if the first one changed, because
  1603. // the DC of the first quantizer must be used for all matrices
  1604. if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0])
  1605. init_dequantizer(s, i);
  1606. if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
  1607. return buf_size;
  1608. s->current_frame.reference = 3;
  1609. s->current_frame.pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  1610. if (ff_thread_get_buffer(avctx, &s->current_frame) < 0) {
  1611. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1612. goto error;
  1613. }
  1614. if (!s->edge_emu_buffer)
  1615. s->edge_emu_buffer = av_malloc(9*FFABS(s->current_frame.linesize[0]));
  1616. if (s->keyframe) {
  1617. if (!s->theora)
  1618. {
  1619. skip_bits(&gb, 4); /* width code */
  1620. skip_bits(&gb, 4); /* height code */
  1621. if (s->version)
  1622. {
  1623. s->version = get_bits(&gb, 5);
  1624. if (avctx->frame_number == 0)
  1625. av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->version);
  1626. }
  1627. }
  1628. if (s->version || s->theora)
  1629. {
  1630. if (get_bits1(&gb))
  1631. av_log(s->avctx, AV_LOG_ERROR, "Warning, unsupported keyframe coding type?!\n");
  1632. skip_bits(&gb, 2); /* reserved? */
  1633. }
  1634. } else {
  1635. if (!s->golden_frame.data[0]) {
  1636. av_log(s->avctx, AV_LOG_WARNING, "vp3: first frame not a keyframe\n");
  1637. s->golden_frame.reference = 3;
  1638. s->golden_frame.pict_type = AV_PICTURE_TYPE_I;
  1639. if (ff_thread_get_buffer(avctx, &s->golden_frame) < 0) {
  1640. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1641. goto error;
  1642. }
  1643. s->last_frame = s->golden_frame;
  1644. s->last_frame.type = FF_BUFFER_TYPE_COPY;
  1645. ff_thread_report_progress(&s->last_frame, INT_MAX, 0);
  1646. }
  1647. }
  1648. memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment));
  1649. ff_thread_finish_setup(avctx);
  1650. if (unpack_superblocks(s, &gb)){
  1651. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
  1652. goto error;
  1653. }
  1654. if (unpack_modes(s, &gb)){
  1655. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
  1656. goto error;
  1657. }
  1658. if (unpack_vectors(s, &gb)){
  1659. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
  1660. goto error;
  1661. }
  1662. if (unpack_block_qpis(s, &gb)){
  1663. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n");
  1664. goto error;
  1665. }
  1666. if (unpack_dct_coeffs(s, &gb)){
  1667. av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
  1668. goto error;
  1669. }
  1670. for (i = 0; i < 3; i++) {
  1671. int height = s->height >> (i && s->chroma_y_shift);
  1672. if (s->flipped_image)
  1673. s->data_offset[i] = 0;
  1674. else
  1675. s->data_offset[i] = (height-1) * s->current_frame.linesize[i];
  1676. }
  1677. s->last_slice_end = 0;
  1678. for (i = 0; i < s->c_superblock_height; i++)
  1679. render_slice(s, i);
  1680. // filter the last row
  1681. for (i = 0; i < 3; i++) {
  1682. int row = (s->height >> (3+(i && s->chroma_y_shift))) - 1;
  1683. apply_loop_filter(s, i, row, row+1);
  1684. }
  1685. vp3_draw_horiz_band(s, s->avctx->height);
  1686. *data_size=sizeof(AVFrame);
  1687. *(AVFrame*)data= s->current_frame;
  1688. if (!HAVE_PTHREADS || !(s->avctx->active_thread_type&FF_THREAD_FRAME))
  1689. update_frames(avctx);
  1690. return buf_size;
  1691. error:
  1692. ff_thread_report_progress(&s->current_frame, INT_MAX, 0);
  1693. if (!HAVE_PTHREADS || !(s->avctx->active_thread_type&FF_THREAD_FRAME))
  1694. avctx->release_buffer(avctx, &s->current_frame);
  1695. return -1;
  1696. }
  1697. /*
  1698. * This is the ffmpeg/libavcodec API module cleanup function.
  1699. */
  1700. static av_cold int vp3_decode_end(AVCodecContext *avctx)
  1701. {
  1702. Vp3DecodeContext *s = avctx->priv_data;
  1703. int i;
  1704. av_free(s->superblock_coding);
  1705. av_free(s->all_fragments);
  1706. av_free(s->coded_fragment_list[0]);
  1707. av_free(s->dct_tokens_base);
  1708. av_free(s->superblock_fragments);
  1709. av_free(s->macroblock_coding);
  1710. av_free(s->motion_val[0]);
  1711. av_free(s->motion_val[1]);
  1712. av_free(s->edge_emu_buffer);
  1713. if (avctx->is_copy) return 0;
  1714. for (i = 0; i < 16; i++) {
  1715. free_vlc(&s->dc_vlc[i]);
  1716. free_vlc(&s->ac_vlc_1[i]);
  1717. free_vlc(&s->ac_vlc_2[i]);
  1718. free_vlc(&s->ac_vlc_3[i]);
  1719. free_vlc(&s->ac_vlc_4[i]);
  1720. }
  1721. free_vlc(&s->superblock_run_length_vlc);
  1722. free_vlc(&s->fragment_run_length_vlc);
  1723. free_vlc(&s->mode_code_vlc);
  1724. free_vlc(&s->motion_vector_vlc);
  1725. /* release all frames */
  1726. vp3_decode_flush(avctx);
  1727. return 0;
  1728. }
  1729. static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
  1730. {
  1731. Vp3DecodeContext *s = avctx->priv_data;
  1732. if (get_bits1(gb)) {
  1733. int token;
  1734. if (s->entries >= 32) { /* overflow */
  1735. av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
  1736. return -1;
  1737. }
  1738. token = get_bits(gb, 5);
  1739. //av_log(avctx, AV_LOG_DEBUG, "hti %d hbits %x token %d entry : %d size %d\n", s->hti, s->hbits, token, s->entries, s->huff_code_size);
  1740. s->huffman_table[s->hti][token][0] = s->hbits;
  1741. s->huffman_table[s->hti][token][1] = s->huff_code_size;
  1742. s->entries++;
  1743. }
  1744. else {
  1745. if (s->huff_code_size >= 32) {/* overflow */
  1746. av_log(avctx, AV_LOG_ERROR, "huffman tree overflow\n");
  1747. return -1;
  1748. }
  1749. s->huff_code_size++;
  1750. s->hbits <<= 1;
  1751. if (read_huffman_tree(avctx, gb))
  1752. return -1;
  1753. s->hbits |= 1;
  1754. if (read_huffman_tree(avctx, gb))
  1755. return -1;
  1756. s->hbits >>= 1;
  1757. s->huff_code_size--;
  1758. }
  1759. return 0;
  1760. }
  1761. #if CONFIG_THEORA_DECODER
  1762. static const enum PixelFormat theora_pix_fmts[4] = {
  1763. PIX_FMT_YUV420P, PIX_FMT_NONE, PIX_FMT_YUV422P, PIX_FMT_YUV444P
  1764. };
  1765. static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb)
  1766. {
  1767. Vp3DecodeContext *s = avctx->priv_data;
  1768. int visible_width, visible_height, colorspace;
  1769. int offset_x = 0, offset_y = 0;
  1770. AVRational fps, aspect;
  1771. s->theora = get_bits_long(gb, 24);
  1772. av_log(avctx, AV_LOG_DEBUG, "Theora bitstream version %X\n", s->theora);
  1773. /* 3.2.0 aka alpha3 has the same frame orientation as original vp3 */
  1774. /* but previous versions have the image flipped relative to vp3 */
  1775. if (s->theora < 0x030200)
  1776. {
  1777. s->flipped_image = 1;
  1778. av_log(avctx, AV_LOG_DEBUG, "Old (<alpha3) Theora bitstream, flipped image\n");
  1779. }
  1780. visible_width = s->width = get_bits(gb, 16) << 4;
  1781. visible_height = s->height = get_bits(gb, 16) << 4;
  1782. if(av_image_check_size(s->width, s->height, 0, avctx)){
  1783. av_log(avctx, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n", s->width, s->height);
  1784. s->width= s->height= 0;
  1785. return -1;
  1786. }
  1787. if (s->theora >= 0x030200) {
  1788. visible_width = get_bits_long(gb, 24);
  1789. visible_height = get_bits_long(gb, 24);
  1790. offset_x = get_bits(gb, 8); /* offset x */
  1791. offset_y = get_bits(gb, 8); /* offset y, from bottom */
  1792. }
  1793. fps.num = get_bits_long(gb, 32);
  1794. fps.den = get_bits_long(gb, 32);
  1795. if (fps.num && fps.den) {
  1796. av_reduce(&avctx->time_base.num, &avctx->time_base.den,
  1797. fps.den, fps.num, 1<<30);
  1798. }
  1799. aspect.num = get_bits_long(gb, 24);
  1800. aspect.den = get_bits_long(gb, 24);
  1801. if (aspect.num && aspect.den) {
  1802. av_reduce(&avctx->sample_aspect_ratio.num,
  1803. &avctx->sample_aspect_ratio.den,
  1804. aspect.num, aspect.den, 1<<30);
  1805. }
  1806. if (s->theora < 0x030200)
  1807. skip_bits(gb, 5); /* keyframe frequency force */
  1808. colorspace = get_bits(gb, 8);
  1809. skip_bits(gb, 24); /* bitrate */
  1810. skip_bits(gb, 6); /* quality hint */
  1811. if (s->theora >= 0x030200)
  1812. {
  1813. skip_bits(gb, 5); /* keyframe frequency force */
  1814. avctx->pix_fmt = theora_pix_fmts[get_bits(gb, 2)];
  1815. skip_bits(gb, 3); /* reserved */
  1816. }
  1817. // align_get_bits(gb);
  1818. if ( visible_width <= s->width && visible_width > s->width-16
  1819. && visible_height <= s->height && visible_height > s->height-16
  1820. && !offset_x && (offset_y == s->height - visible_height))
  1821. avcodec_set_dimensions(avctx, visible_width, visible_height);
  1822. else
  1823. avcodec_set_dimensions(avctx, s->width, s->height);
  1824. if (colorspace == 1) {
  1825. avctx->color_primaries = AVCOL_PRI_BT470M;
  1826. } else if (colorspace == 2) {
  1827. avctx->color_primaries = AVCOL_PRI_BT470BG;
  1828. }
  1829. if (colorspace == 1 || colorspace == 2) {
  1830. avctx->colorspace = AVCOL_SPC_BT470BG;
  1831. avctx->color_trc = AVCOL_TRC_BT709;
  1832. }
  1833. return 0;
  1834. }
  1835. static int theora_decode_tables(AVCodecContext *avctx, GetBitContext *gb)
  1836. {
  1837. Vp3DecodeContext *s = avctx->priv_data;
  1838. int i, n, matrices, inter, plane;
  1839. if (s->theora >= 0x030200) {
  1840. n = get_bits(gb, 3);
  1841. /* loop filter limit values table */
  1842. if (n)
  1843. for (i = 0; i < 64; i++)
  1844. s->filter_limit_values[i] = get_bits(gb, n);
  1845. }
  1846. if (s->theora >= 0x030200)
  1847. n = get_bits(gb, 4) + 1;
  1848. else
  1849. n = 16;
  1850. /* quality threshold table */
  1851. for (i = 0; i < 64; i++)
  1852. s->coded_ac_scale_factor[i] = get_bits(gb, n);
  1853. if (s->theora >= 0x030200)
  1854. n = get_bits(gb, 4) + 1;
  1855. else
  1856. n = 16;
  1857. /* dc scale factor table */
  1858. for (i = 0; i < 64; i++)
  1859. s->coded_dc_scale_factor[i] = get_bits(gb, n);
  1860. if (s->theora >= 0x030200)
  1861. matrices = get_bits(gb, 9) + 1;
  1862. else
  1863. matrices = 3;
  1864. if(matrices > 384){
  1865. av_log(avctx, AV_LOG_ERROR, "invalid number of base matrixes\n");
  1866. return -1;
  1867. }
  1868. for(n=0; n<matrices; n++){
  1869. for (i = 0; i < 64; i++)
  1870. s->base_matrix[n][i]= get_bits(gb, 8);
  1871. }
  1872. for (inter = 0; inter <= 1; inter++) {
  1873. for (plane = 0; plane <= 2; plane++) {
  1874. int newqr= 1;
  1875. if (inter || plane > 0)
  1876. newqr = get_bits1(gb);
  1877. if (!newqr) {
  1878. int qtj, plj;
  1879. if(inter && get_bits1(gb)){
  1880. qtj = 0;
  1881. plj = plane;
  1882. }else{
  1883. qtj= (3*inter + plane - 1) / 3;
  1884. plj= (plane + 2) % 3;
  1885. }
  1886. s->qr_count[inter][plane]= s->qr_count[qtj][plj];
  1887. memcpy(s->qr_size[inter][plane], s->qr_size[qtj][plj], sizeof(s->qr_size[0][0]));
  1888. memcpy(s->qr_base[inter][plane], s->qr_base[qtj][plj], sizeof(s->qr_base[0][0]));
  1889. } else {
  1890. int qri= 0;
  1891. int qi = 0;
  1892. for(;;){
  1893. i= get_bits(gb, av_log2(matrices-1)+1);
  1894. if(i>= matrices){
  1895. av_log(avctx, AV_LOG_ERROR, "invalid base matrix index\n");
  1896. return -1;
  1897. }
  1898. s->qr_base[inter][plane][qri]= i;
  1899. if(qi >= 63)
  1900. break;
  1901. i = get_bits(gb, av_log2(63-qi)+1) + 1;
  1902. s->qr_size[inter][plane][qri++]= i;
  1903. qi += i;
  1904. }
  1905. if (qi > 63) {
  1906. av_log(avctx, AV_LOG_ERROR, "invalid qi %d > 63\n", qi);
  1907. return -1;
  1908. }
  1909. s->qr_count[inter][plane]= qri;
  1910. }
  1911. }
  1912. }
  1913. /* Huffman tables */
  1914. for (s->hti = 0; s->hti < 80; s->hti++) {
  1915. s->entries = 0;
  1916. s->huff_code_size = 1;
  1917. if (!get_bits1(gb)) {
  1918. s->hbits = 0;
  1919. if(read_huffman_tree(avctx, gb))
  1920. return -1;
  1921. s->hbits = 1;
  1922. if(read_huffman_tree(avctx, gb))
  1923. return -1;
  1924. }
  1925. }
  1926. s->theora_tables = 1;
  1927. return 0;
  1928. }
  1929. static av_cold int theora_decode_init(AVCodecContext *avctx)
  1930. {
  1931. Vp3DecodeContext *s = avctx->priv_data;
  1932. GetBitContext gb;
  1933. int ptype;
  1934. uint8_t *header_start[3];
  1935. int header_len[3];
  1936. int i;
  1937. s->theora = 1;
  1938. if (!avctx->extradata_size)
  1939. {
  1940. av_log(avctx, AV_LOG_ERROR, "Missing extradata!\n");
  1941. return -1;
  1942. }
  1943. if (ff_split_xiph_headers(avctx->extradata, avctx->extradata_size,
  1944. 42, header_start, header_len) < 0) {
  1945. av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
  1946. return -1;
  1947. }
  1948. for(i=0;i<3;i++) {
  1949. init_get_bits(&gb, header_start[i], header_len[i] * 8);
  1950. ptype = get_bits(&gb, 8);
  1951. if (!(ptype & 0x80))
  1952. {
  1953. av_log(avctx, AV_LOG_ERROR, "Invalid extradata!\n");
  1954. // return -1;
  1955. }
  1956. // FIXME: Check for this as well.
  1957. skip_bits_long(&gb, 6*8); /* "theora" */
  1958. switch(ptype)
  1959. {
  1960. case 0x80:
  1961. theora_decode_header(avctx, &gb);
  1962. break;
  1963. case 0x81:
  1964. // FIXME: is this needed? it breaks sometimes
  1965. // theora_decode_comments(avctx, gb);
  1966. break;
  1967. case 0x82:
  1968. if (theora_decode_tables(avctx, &gb))
  1969. return -1;
  1970. break;
  1971. default:
  1972. av_log(avctx, AV_LOG_ERROR, "Unknown Theora config packet: %d\n", ptype&~0x80);
  1973. break;
  1974. }
  1975. if(ptype != 0x81 && 8*header_len[i] != get_bits_count(&gb))
  1976. av_log(avctx, AV_LOG_WARNING, "%d bits left in packet %X\n", 8*header_len[i] - get_bits_count(&gb), ptype);
  1977. if (s->theora < 0x030200)
  1978. break;
  1979. }
  1980. return vp3_decode_init(avctx);
  1981. }
  1982. static void vp3_decode_flush(AVCodecContext *avctx)
  1983. {
  1984. Vp3DecodeContext *s = avctx->priv_data;
  1985. if (s->golden_frame.data[0]) {
  1986. if (s->golden_frame.data[0] == s->last_frame.data[0])
  1987. memset(&s->last_frame, 0, sizeof(AVFrame));
  1988. if (s->current_frame.data[0] == s->golden_frame.data[0])
  1989. memset(&s->current_frame, 0, sizeof(AVFrame));
  1990. ff_thread_release_buffer(avctx, &s->golden_frame);
  1991. }
  1992. if (s->last_frame.data[0]) {
  1993. if (s->current_frame.data[0] == s->last_frame.data[0])
  1994. memset(&s->current_frame, 0, sizeof(AVFrame));
  1995. ff_thread_release_buffer(avctx, &s->last_frame);
  1996. }
  1997. if (s->current_frame.data[0])
  1998. ff_thread_release_buffer(avctx, &s->current_frame);
  1999. }
  2000. static int vp3_init_thread_copy(AVCodecContext *avctx)
  2001. {
  2002. Vp3DecodeContext *s = avctx->priv_data;
  2003. s->superblock_coding = NULL;
  2004. s->all_fragments = NULL;
  2005. s->coded_fragment_list[0] = NULL;
  2006. s->dct_tokens_base = NULL;
  2007. s->superblock_fragments = NULL;
  2008. s->macroblock_coding = NULL;
  2009. s->motion_val[0] = NULL;
  2010. s->motion_val[1] = NULL;
  2011. s->edge_emu_buffer = NULL;
  2012. return 0;
  2013. }
  2014. AVCodec ff_theora_decoder = {
  2015. "theora",
  2016. AVMEDIA_TYPE_VIDEO,
  2017. CODEC_ID_THEORA,
  2018. sizeof(Vp3DecodeContext),
  2019. theora_decode_init,
  2020. NULL,
  2021. vp3_decode_end,
  2022. vp3_decode_frame,
  2023. CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_FRAME_THREADS,
  2024. NULL,
  2025. .flush = vp3_decode_flush,
  2026. .long_name = NULL_IF_CONFIG_SMALL("Theora"),
  2027. .init_thread_copy = ONLY_IF_THREADS_ENABLED(vp3_init_thread_copy),
  2028. .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context)
  2029. };
  2030. #endif
  2031. AVCodec ff_vp3_decoder = {
  2032. "vp3",
  2033. AVMEDIA_TYPE_VIDEO,
  2034. CODEC_ID_VP3,
  2035. sizeof(Vp3DecodeContext),
  2036. vp3_decode_init,
  2037. NULL,
  2038. vp3_decode_end,
  2039. vp3_decode_frame,
  2040. CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_FRAME_THREADS,
  2041. NULL,
  2042. .flush = vp3_decode_flush,
  2043. .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"),
  2044. .init_thread_copy = ONLY_IF_THREADS_ENABLED(vp3_init_thread_copy),
  2045. .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context)
  2046. };