vp3.c 82 KB

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