mpeg1.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include "mpeg1.h"
  4. static const float PICTURE_RATE[] = {
  5. 0.000, 23.976, 24.000, 25.000, 29.970, 30.000, 50.000, 59.940,
  6. 60.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000
  7. };
  8. static const uint8_t ZIG_ZAG[] = {
  9. 0, 1, 8, 16, 9, 2, 3, 10,
  10. 17, 24, 32, 25, 18, 11, 4, 5,
  11. 12, 19, 26, 33, 40, 48, 41, 34,
  12. 27, 20, 13, 6, 7, 14, 21, 28,
  13. 35, 42, 49, 56, 57, 50, 43, 36,
  14. 29, 22, 15, 23, 30, 37, 44, 51,
  15. 58, 59, 52, 45, 38, 31, 39, 46,
  16. 53, 60, 61, 54, 47, 55, 62, 63
  17. };
  18. static const uint8_t DEFAULT_INTRA_QUANT_MATRIX[] = {
  19. 8, 16, 19, 22, 26, 27, 29, 34,
  20. 16, 16, 22, 24, 27, 29, 34, 37,
  21. 19, 22, 26, 27, 29, 34, 34, 38,
  22. 22, 22, 26, 27, 29, 34, 37, 40,
  23. 22, 26, 27, 29, 32, 35, 40, 48,
  24. 26, 27, 29, 32, 35, 40, 48, 58,
  25. 26, 27, 29, 34, 38, 46, 56, 69,
  26. 27, 29, 35, 38, 46, 56, 69, 83
  27. };
  28. static const uint8_t DEFAULT_NON_INTRA_QUANT_MATRIX[] = {
  29. 16, 16, 16, 16, 16, 16, 16, 16,
  30. 16, 16, 16, 16, 16, 16, 16, 16,
  31. 16, 16, 16, 16, 16, 16, 16, 16,
  32. 16, 16, 16, 16, 16, 16, 16, 16,
  33. 16, 16, 16, 16, 16, 16, 16, 16,
  34. 16, 16, 16, 16, 16, 16, 16, 16,
  35. 16, 16, 16, 16, 16, 16, 16, 16,
  36. 16, 16, 16, 16, 16, 16, 16, 16
  37. };
  38. static const uint8_t PREMULTIPLIER_MATRIX[] = {
  39. 32, 44, 42, 38, 32, 25, 17, 9,
  40. 44, 62, 58, 52, 44, 35, 24, 12,
  41. 42, 58, 55, 49, 42, 33, 23, 12,
  42. 38, 52, 49, 44, 38, 30, 20, 10,
  43. 32, 44, 42, 38, 32, 25, 17, 9,
  44. 25, 35, 33, 30, 25, 20, 14, 7,
  45. 17, 24, 23, 20, 17, 14, 9, 5,
  46. 9, 12, 12, 10, 9, 7, 5, 2
  47. };
  48. // MPEG-1 VLC
  49. // macroblock_stuffing decodes as 34.
  50. // macroblock_escape decodes as 35.
  51. static const int MACROBLOCK_ADDRESS_INCREMENT[] = {
  52. 1*3, 2*3, 0, // 0
  53. 3*3, 4*3, 0, // 1 0
  54. 0, 0, 1, // 2 1.
  55. 5*3, 6*3, 0, // 3 00
  56. 7*3, 8*3, 0, // 4 01
  57. 9*3, 10*3, 0, // 5 000
  58. 11*3, 12*3, 0, // 6 001
  59. 0, 0, 3, // 7 010.
  60. 0, 0, 2, // 8 011.
  61. 13*3, 14*3, 0, // 9 0000
  62. 15*3, 16*3, 0, // 10 0001
  63. 0, 0, 5, // 11 0010.
  64. 0, 0, 4, // 12 0011.
  65. 17*3, 18*3, 0, // 13 0000 0
  66. 19*3, 20*3, 0, // 14 0000 1
  67. 0, 0, 7, // 15 0001 0.
  68. 0, 0, 6, // 16 0001 1.
  69. 21*3, 22*3, 0, // 17 0000 00
  70. 23*3, 24*3, 0, // 18 0000 01
  71. 25*3, 26*3, 0, // 19 0000 10
  72. 27*3, 28*3, 0, // 20 0000 11
  73. -1, 29*3, 0, // 21 0000 000
  74. -1, 30*3, 0, // 22 0000 001
  75. 31*3, 32*3, 0, // 23 0000 010
  76. 33*3, 34*3, 0, // 24 0000 011
  77. 35*3, 36*3, 0, // 25 0000 100
  78. 37*3, 38*3, 0, // 26 0000 101
  79. 0, 0, 9, // 27 0000 110.
  80. 0, 0, 8, // 28 0000 111.
  81. 39*3, 40*3, 0, // 29 0000 0001
  82. 41*3, 42*3, 0, // 30 0000 0011
  83. 43*3, 44*3, 0, // 31 0000 0100
  84. 45*3, 46*3, 0, // 32 0000 0101
  85. 0, 0, 15, // 33 0000 0110.
  86. 0, 0, 14, // 34 0000 0111.
  87. 0, 0, 13, // 35 0000 1000.
  88. 0, 0, 12, // 36 0000 1001.
  89. 0, 0, 11, // 37 0000 1010.
  90. 0, 0, 10, // 38 0000 1011.
  91. 47*3, -1, 0, // 39 0000 0001 0
  92. -1, 48*3, 0, // 40 0000 0001 1
  93. 49*3, 50*3, 0, // 41 0000 0011 0
  94. 51*3, 52*3, 0, // 42 0000 0011 1
  95. 53*3, 54*3, 0, // 43 0000 0100 0
  96. 55*3, 56*3, 0, // 44 0000 0100 1
  97. 57*3, 58*3, 0, // 45 0000 0101 0
  98. 59*3, 60*3, 0, // 46 0000 0101 1
  99. 61*3, -1, 0, // 47 0000 0001 00
  100. -1, 62*3, 0, // 48 0000 0001 11
  101. 63*3, 64*3, 0, // 49 0000 0011 00
  102. 65*3, 66*3, 0, // 50 0000 0011 01
  103. 67*3, 68*3, 0, // 51 0000 0011 10
  104. 69*3, 70*3, 0, // 52 0000 0011 11
  105. 71*3, 72*3, 0, // 53 0000 0100 00
  106. 73*3, 74*3, 0, // 54 0000 0100 01
  107. 0, 0, 21, // 55 0000 0100 10.
  108. 0, 0, 20, // 56 0000 0100 11.
  109. 0, 0, 19, // 57 0000 0101 00.
  110. 0, 0, 18, // 58 0000 0101 01.
  111. 0, 0, 17, // 59 0000 0101 10.
  112. 0, 0, 16, // 60 0000 0101 11.
  113. 0, 0, 35, // 61 0000 0001 000. -- macroblock_escape
  114. 0, 0, 34, // 62 0000 0001 111. -- macroblock_stuffing
  115. 0, 0, 33, // 63 0000 0011 000.
  116. 0, 0, 32, // 64 0000 0011 001.
  117. 0, 0, 31, // 65 0000 0011 010.
  118. 0, 0, 30, // 66 0000 0011 011.
  119. 0, 0, 29, // 67 0000 0011 100.
  120. 0, 0, 28, // 68 0000 0011 101.
  121. 0, 0, 27, // 69 0000 0011 110.
  122. 0, 0, 26, // 70 0000 0011 111.
  123. 0, 0, 25, // 71 0000 0100 000.
  124. 0, 0, 24, // 72 0000 0100 001.
  125. 0, 0, 23, // 73 0000 0100 010.
  126. 0, 0, 22 // 74 0000 0100 011.
  127. };
  128. // macroblock_type bitmap:
  129. // 0x10 macroblock_quant
  130. // 0x08 macroblock_motion_forward
  131. // 0x04 macroblock_motion_backward
  132. // 0x02 macrobkock_pattern
  133. // 0x01 macroblock_intra
  134. //
  135. static const int MACROBLOCK_TYPE_INTRA[] = {
  136. 1*3, 2*3, 0, // 0
  137. -1, 3*3, 0, // 1 0
  138. 0, 0, 0x01, // 2 1.
  139. 0, 0, 0x11 // 3 01.
  140. };
  141. static const int MACROBLOCK_TYPE_PREDICTIVE[] = {
  142. 1*3, 2*3, 0, // 0
  143. 3*3, 4*3, 0, // 1 0
  144. 0, 0, 0x0a, // 2 1.
  145. 5*3, 6*3, 0, // 3 00
  146. 0, 0, 0x02, // 4 01.
  147. 7*3, 8*3, 0, // 5 000
  148. 0, 0, 0x08, // 6 001.
  149. 9*3, 10*3, 0, // 7 0000
  150. 11*3, 12*3, 0, // 8 0001
  151. -1, 13*3, 0, // 9 00000
  152. 0, 0, 0x12, // 10 00001.
  153. 0, 0, 0x1a, // 11 00010.
  154. 0, 0, 0x01, // 12 00011.
  155. 0, 0, 0x11 // 13 000001.
  156. };
  157. static const int MACROBLOCK_TYPE_B[] = {
  158. 1*3, 2*3, 0, // 0
  159. 3*3, 5*3, 0, // 1 0
  160. 4*3, 6*3, 0, // 2 1
  161. 8*3, 7*3, 0, // 3 00
  162. 0, 0, 0x0c, // 4 10.
  163. 9*3, 10*3, 0, // 5 01
  164. 0, 0, 0x0e, // 6 11.
  165. 13*3, 14*3, 0, // 7 001
  166. 12*3, 11*3, 0, // 8 000
  167. 0, 0, 0x04, // 9 010.
  168. 0, 0, 0x06, // 10 011.
  169. 18*3, 16*3, 0, // 11 0001
  170. 15*3, 17*3, 0, // 12 0000
  171. 0, 0, 0x08, // 13 0010.
  172. 0, 0, 0x0a, // 14 0011.
  173. -1, 19*3, 0, // 15 00000
  174. 0, 0, 0x01, // 16 00011.
  175. 20*3, 21*3, 0, // 17 00001
  176. 0, 0, 0x1e, // 18 00010.
  177. 0, 0, 0x11, // 19 000001.
  178. 0, 0, 0x16, // 20 000010.
  179. 0, 0, 0x1a // 21 000011.
  180. };
  181. static const int *MACROBLOCK_TYPE[] = {
  182. NULL,
  183. MACROBLOCK_TYPE_INTRA,
  184. MACROBLOCK_TYPE_PREDICTIVE,
  185. MACROBLOCK_TYPE_B
  186. };
  187. static const int CODE_BLOCK_PATTERN[] = {
  188. 2*3, 1*3, 0, // 0
  189. 3*3, 6*3, 0, // 1 1
  190. 4*3, 5*3, 0, // 2 0
  191. 8*3, 11*3, 0, // 3 10
  192. 12*3, 13*3, 0, // 4 00
  193. 9*3, 7*3, 0, // 5 01
  194. 10*3, 14*3, 0, // 6 11
  195. 20*3, 19*3, 0, // 7 011
  196. 18*3, 16*3, 0, // 8 100
  197. 23*3, 17*3, 0, // 9 010
  198. 27*3, 25*3, 0, // 10 110
  199. 21*3, 28*3, 0, // 11 101
  200. 15*3, 22*3, 0, // 12 000
  201. 24*3, 26*3, 0, // 13 001
  202. 0, 0, 60, // 14 111.
  203. 35*3, 40*3, 0, // 15 0000
  204. 44*3, 48*3, 0, // 16 1001
  205. 38*3, 36*3, 0, // 17 0101
  206. 42*3, 47*3, 0, // 18 1000
  207. 29*3, 31*3, 0, // 19 0111
  208. 39*3, 32*3, 0, // 20 0110
  209. 0, 0, 32, // 21 1010.
  210. 45*3, 46*3, 0, // 22 0001
  211. 33*3, 41*3, 0, // 23 0100
  212. 43*3, 34*3, 0, // 24 0010
  213. 0, 0, 4, // 25 1101.
  214. 30*3, 37*3, 0, // 26 0011
  215. 0, 0, 8, // 27 1100.
  216. 0, 0, 16, // 28 1011.
  217. 0, 0, 44, // 29 0111 0.
  218. 50*3, 56*3, 0, // 30 0011 0
  219. 0, 0, 28, // 31 0111 1.
  220. 0, 0, 52, // 32 0110 1.
  221. 0, 0, 62, // 33 0100 0.
  222. 61*3, 59*3, 0, // 34 0010 1
  223. 52*3, 60*3, 0, // 35 0000 0
  224. 0, 0, 1, // 36 0101 1.
  225. 55*3, 54*3, 0, // 37 0011 1
  226. 0, 0, 61, // 38 0101 0.
  227. 0, 0, 56, // 39 0110 0.
  228. 57*3, 58*3, 0, // 40 0000 1
  229. 0, 0, 2, // 41 0100 1.
  230. 0, 0, 40, // 42 1000 0.
  231. 51*3, 62*3, 0, // 43 0010 0
  232. 0, 0, 48, // 44 1001 0.
  233. 64*3, 63*3, 0, // 45 0001 0
  234. 49*3, 53*3, 0, // 46 0001 1
  235. 0, 0, 20, // 47 1000 1.
  236. 0, 0, 12, // 48 1001 1.
  237. 80*3, 83*3, 0, // 49 0001 10
  238. 0, 0, 63, // 50 0011 00.
  239. 77*3, 75*3, 0, // 51 0010 00
  240. 65*3, 73*3, 0, // 52 0000 00
  241. 84*3, 66*3, 0, // 53 0001 11
  242. 0, 0, 24, // 54 0011 11.
  243. 0, 0, 36, // 55 0011 10.
  244. 0, 0, 3, // 56 0011 01.
  245. 69*3, 87*3, 0, // 57 0000 10
  246. 81*3, 79*3, 0, // 58 0000 11
  247. 68*3, 71*3, 0, // 59 0010 11
  248. 70*3, 78*3, 0, // 60 0000 01
  249. 67*3, 76*3, 0, // 61 0010 10
  250. 72*3, 74*3, 0, // 62 0010 01
  251. 86*3, 85*3, 0, // 63 0001 01
  252. 88*3, 82*3, 0, // 64 0001 00
  253. -1, 94*3, 0, // 65 0000 000
  254. 95*3, 97*3, 0, // 66 0001 111
  255. 0, 0, 33, // 67 0010 100.
  256. 0, 0, 9, // 68 0010 110.
  257. 106*3, 110*3, 0, // 69 0000 100
  258. 102*3, 116*3, 0, // 70 0000 010
  259. 0, 0, 5, // 71 0010 111.
  260. 0, 0, 10, // 72 0010 010.
  261. 93*3, 89*3, 0, // 73 0000 001
  262. 0, 0, 6, // 74 0010 011.
  263. 0, 0, 18, // 75 0010 001.
  264. 0, 0, 17, // 76 0010 101.
  265. 0, 0, 34, // 77 0010 000.
  266. 113*3, 119*3, 0, // 78 0000 011
  267. 103*3, 104*3, 0, // 79 0000 111
  268. 90*3, 92*3, 0, // 80 0001 100
  269. 109*3, 107*3, 0, // 81 0000 110
  270. 117*3, 118*3, 0, // 82 0001 001
  271. 101*3, 99*3, 0, // 83 0001 101
  272. 98*3, 96*3, 0, // 84 0001 110
  273. 100*3, 91*3, 0, // 85 0001 011
  274. 114*3, 115*3, 0, // 86 0001 010
  275. 105*3, 108*3, 0, // 87 0000 101
  276. 112*3, 111*3, 0, // 88 0001 000
  277. 121*3, 125*3, 0, // 89 0000 0011
  278. 0, 0, 41, // 90 0001 1000.
  279. 0, 0, 14, // 91 0001 0111.
  280. 0, 0, 21, // 92 0001 1001.
  281. 124*3, 122*3, 0, // 93 0000 0010
  282. 120*3, 123*3, 0, // 94 0000 0001
  283. 0, 0, 11, // 95 0001 1110.
  284. 0, 0, 19, // 96 0001 1101.
  285. 0, 0, 7, // 97 0001 1111.
  286. 0, 0, 35, // 98 0001 1100.
  287. 0, 0, 13, // 99 0001 1011.
  288. 0, 0, 50, // 100 0001 0110.
  289. 0, 0, 49, // 101 0001 1010.
  290. 0, 0, 58, // 102 0000 0100.
  291. 0, 0, 37, // 103 0000 1110.
  292. 0, 0, 25, // 104 0000 1111.
  293. 0, 0, 45, // 105 0000 1010.
  294. 0, 0, 57, // 106 0000 1000.
  295. 0, 0, 26, // 107 0000 1101.
  296. 0, 0, 29, // 108 0000 1011.
  297. 0, 0, 38, // 109 0000 1100.
  298. 0, 0, 53, // 110 0000 1001.
  299. 0, 0, 23, // 111 0001 0001.
  300. 0, 0, 43, // 112 0001 0000.
  301. 0, 0, 46, // 113 0000 0110.
  302. 0, 0, 42, // 114 0001 0100.
  303. 0, 0, 22, // 115 0001 0101.
  304. 0, 0, 54, // 116 0000 0101.
  305. 0, 0, 51, // 117 0001 0010.
  306. 0, 0, 15, // 118 0001 0011.
  307. 0, 0, 30, // 119 0000 0111.
  308. 0, 0, 39, // 120 0000 0001 0.
  309. 0, 0, 47, // 121 0000 0011 0.
  310. 0, 0, 55, // 122 0000 0010 1.
  311. 0, 0, 27, // 123 0000 0001 1.
  312. 0, 0, 59, // 124 0000 0010 0.
  313. 0, 0, 31 // 125 0000 0011 1.
  314. };
  315. static const int MOTION[] = {
  316. 1*3, 2*3, 0, // 0
  317. 4*3, 3*3, 0, // 1 0
  318. 0, 0, 0, // 2 1.
  319. 6*3, 5*3, 0, // 3 01
  320. 8*3, 7*3, 0, // 4 00
  321. 0, 0, -1, // 5 011.
  322. 0, 0, 1, // 6 010.
  323. 9*3, 10*3, 0, // 7 001
  324. 12*3, 11*3, 0, // 8 000
  325. 0, 0, 2, // 9 0010.
  326. 0, 0, -2, // 10 0011.
  327. 14*3, 15*3, 0, // 11 0001
  328. 16*3, 13*3, 0, // 12 0000
  329. 20*3, 18*3, 0, // 13 0000 1
  330. 0, 0, 3, // 14 0001 0.
  331. 0, 0, -3, // 15 0001 1.
  332. 17*3, 19*3, 0, // 16 0000 0
  333. -1, 23*3, 0, // 17 0000 00
  334. 27*3, 25*3, 0, // 18 0000 11
  335. 26*3, 21*3, 0, // 19 0000 01
  336. 24*3, 22*3, 0, // 20 0000 10
  337. 32*3, 28*3, 0, // 21 0000 011
  338. 29*3, 31*3, 0, // 22 0000 101
  339. -1, 33*3, 0, // 23 0000 001
  340. 36*3, 35*3, 0, // 24 0000 100
  341. 0, 0, -4, // 25 0000 111.
  342. 30*3, 34*3, 0, // 26 0000 010
  343. 0, 0, 4, // 27 0000 110.
  344. 0, 0, -7, // 28 0000 0111.
  345. 0, 0, 5, // 29 0000 1010.
  346. 37*3, 41*3, 0, // 30 0000 0100
  347. 0, 0, -5, // 31 0000 1011.
  348. 0, 0, 7, // 32 0000 0110.
  349. 38*3, 40*3, 0, // 33 0000 0011
  350. 42*3, 39*3, 0, // 34 0000 0101
  351. 0, 0, -6, // 35 0000 1001.
  352. 0, 0, 6, // 36 0000 1000.
  353. 51*3, 54*3, 0, // 37 0000 0100 0
  354. 50*3, 49*3, 0, // 38 0000 0011 0
  355. 45*3, 46*3, 0, // 39 0000 0101 1
  356. 52*3, 47*3, 0, // 40 0000 0011 1
  357. 43*3, 53*3, 0, // 41 0000 0100 1
  358. 44*3, 48*3, 0, // 42 0000 0101 0
  359. 0, 0, 10, // 43 0000 0100 10.
  360. 0, 0, 9, // 44 0000 0101 00.
  361. 0, 0, 8, // 45 0000 0101 10.
  362. 0, 0, -8, // 46 0000 0101 11.
  363. 57*3, 66*3, 0, // 47 0000 0011 11
  364. 0, 0, -9, // 48 0000 0101 01.
  365. 60*3, 64*3, 0, // 49 0000 0011 01
  366. 56*3, 61*3, 0, // 50 0000 0011 00
  367. 55*3, 62*3, 0, // 51 0000 0100 00
  368. 58*3, 63*3, 0, // 52 0000 0011 10
  369. 0, 0, -10, // 53 0000 0100 11.
  370. 59*3, 65*3, 0, // 54 0000 0100 01
  371. 0, 0, 12, // 55 0000 0100 000.
  372. 0, 0, 16, // 56 0000 0011 000.
  373. 0, 0, 13, // 57 0000 0011 110.
  374. 0, 0, 14, // 58 0000 0011 100.
  375. 0, 0, 11, // 59 0000 0100 010.
  376. 0, 0, 15, // 60 0000 0011 010.
  377. 0, 0, -16, // 61 0000 0011 001.
  378. 0, 0, -12, // 62 0000 0100 001.
  379. 0, 0, -14, // 63 0000 0011 101.
  380. 0, 0, -15, // 64 0000 0011 011.
  381. 0, 0, -11, // 65 0000 0100 011.
  382. 0, 0, -13 // 66 0000 0011 111.
  383. };
  384. static const int DCT_DC_SIZE_LUMINANCE[] = {
  385. 2*3, 1*3, 0, // 0
  386. 6*3, 5*3, 0, // 1 1
  387. 3*3, 4*3, 0, // 2 0
  388. 0, 0, 1, // 3 00.
  389. 0, 0, 2, // 4 01.
  390. 9*3, 8*3, 0, // 5 11
  391. 7*3, 10*3, 0, // 6 10
  392. 0, 0, 0, // 7 100.
  393. 12*3, 11*3, 0, // 8 111
  394. 0, 0, 4, // 9 110.
  395. 0, 0, 3, // 10 101.
  396. 13*3, 14*3, 0, // 11 1111
  397. 0, 0, 5, // 12 1110.
  398. 0, 0, 6, // 13 1111 0.
  399. 16*3, 15*3, 0, // 14 1111 1
  400. 17*3, -1, 0, // 15 1111 11
  401. 0, 0, 7, // 16 1111 10.
  402. 0, 0, 8 // 17 1111 110.
  403. };
  404. static const int DCT_DC_SIZE_CHROMINANCE[] = {
  405. 2*3, 1*3, 0, // 0
  406. 4*3, 3*3, 0, // 1 1
  407. 6*3, 5*3, 0, // 2 0
  408. 8*3, 7*3, 0, // 3 11
  409. 0, 0, 2, // 4 10.
  410. 0, 0, 1, // 5 01.
  411. 0, 0, 0, // 6 00.
  412. 10*3, 9*3, 0, // 7 111
  413. 0, 0, 3, // 8 110.
  414. 12*3, 11*3, 0, // 9 1111
  415. 0, 0, 4, // 10 1110.
  416. 14*3, 13*3, 0, // 11 1111 1
  417. 0, 0, 5, // 12 1111 0.
  418. 16*3, 15*3, 0, // 13 1111 11
  419. 0, 0, 6, // 14 1111 10.
  420. 17*3, -1, 0, // 15 1111 111
  421. 0, 0, 7, // 16 1111 110.
  422. 0, 0, 8 // 17 1111 1110.
  423. };
  424. // dct_coeff bitmap:
  425. // 0xff00 run
  426. // 0x00ff level
  427. // Decoded values are unsigned. Sign bit follows in the stream.
  428. // Interpretation of the value 0x0001
  429. // for dc_coeff_first: run=0, level=1
  430. // for dc_coeff_next: If the next bit is 1: run=0, level=1
  431. // If the next bit is 0: end_of_block
  432. // escape decodes as 0xffff.
  433. static const int DCT_COEFF[] = {
  434. 1*3, 2*3, 0, // 0
  435. 4*3, 3*3, 0, // 1 0
  436. 0, 0, 0x0001, // 2 1.
  437. 7*3, 8*3, 0, // 3 01
  438. 6*3, 5*3, 0, // 4 00
  439. 13*3, 9*3, 0, // 5 001
  440. 11*3, 10*3, 0, // 6 000
  441. 14*3, 12*3, 0, // 7 010
  442. 0, 0, 0x0101, // 8 011.
  443. 20*3, 22*3, 0, // 9 0011
  444. 18*3, 21*3, 0, // 10 0001
  445. 16*3, 19*3, 0, // 11 0000
  446. 0, 0, 0x0201, // 12 0101.
  447. 17*3, 15*3, 0, // 13 0010
  448. 0, 0, 0x0002, // 14 0100.
  449. 0, 0, 0x0003, // 15 0010 1.
  450. 27*3, 25*3, 0, // 16 0000 0
  451. 29*3, 31*3, 0, // 17 0010 0
  452. 24*3, 26*3, 0, // 18 0001 0
  453. 32*3, 30*3, 0, // 19 0000 1
  454. 0, 0, 0x0401, // 20 0011 0.
  455. 23*3, 28*3, 0, // 21 0001 1
  456. 0, 0, 0x0301, // 22 0011 1.
  457. 0, 0, 0x0102, // 23 0001 10.
  458. 0, 0, 0x0701, // 24 0001 00.
  459. 0, 0, 0xffff, // 25 0000 01. -- escape
  460. 0, 0, 0x0601, // 26 0001 01.
  461. 37*3, 36*3, 0, // 27 0000 00
  462. 0, 0, 0x0501, // 28 0001 11.
  463. 35*3, 34*3, 0, // 29 0010 00
  464. 39*3, 38*3, 0, // 30 0000 11
  465. 33*3, 42*3, 0, // 31 0010 01
  466. 40*3, 41*3, 0, // 32 0000 10
  467. 52*3, 50*3, 0, // 33 0010 010
  468. 54*3, 53*3, 0, // 34 0010 001
  469. 48*3, 49*3, 0, // 35 0010 000
  470. 43*3, 45*3, 0, // 36 0000 001
  471. 46*3, 44*3, 0, // 37 0000 000
  472. 0, 0, 0x0801, // 38 0000 111.
  473. 0, 0, 0x0004, // 39 0000 110.
  474. 0, 0, 0x0202, // 40 0000 100.
  475. 0, 0, 0x0901, // 41 0000 101.
  476. 51*3, 47*3, 0, // 42 0010 011
  477. 55*3, 57*3, 0, // 43 0000 0010
  478. 60*3, 56*3, 0, // 44 0000 0001
  479. 59*3, 58*3, 0, // 45 0000 0011
  480. 61*3, 62*3, 0, // 46 0000 0000
  481. 0, 0, 0x0a01, // 47 0010 0111.
  482. 0, 0, 0x0d01, // 48 0010 0000.
  483. 0, 0, 0x0006, // 49 0010 0001.
  484. 0, 0, 0x0103, // 50 0010 0101.
  485. 0, 0, 0x0005, // 51 0010 0110.
  486. 0, 0, 0x0302, // 52 0010 0100.
  487. 0, 0, 0x0b01, // 53 0010 0011.
  488. 0, 0, 0x0c01, // 54 0010 0010.
  489. 76*3, 75*3, 0, // 55 0000 0010 0
  490. 67*3, 70*3, 0, // 56 0000 0001 1
  491. 73*3, 71*3, 0, // 57 0000 0010 1
  492. 78*3, 74*3, 0, // 58 0000 0011 1
  493. 72*3, 77*3, 0, // 59 0000 0011 0
  494. 69*3, 64*3, 0, // 60 0000 0001 0
  495. 68*3, 63*3, 0, // 61 0000 0000 0
  496. 66*3, 65*3, 0, // 62 0000 0000 1
  497. 81*3, 87*3, 0, // 63 0000 0000 01
  498. 91*3, 80*3, 0, // 64 0000 0001 01
  499. 82*3, 79*3, 0, // 65 0000 0000 11
  500. 83*3, 86*3, 0, // 66 0000 0000 10
  501. 93*3, 92*3, 0, // 67 0000 0001 10
  502. 84*3, 85*3, 0, // 68 0000 0000 00
  503. 90*3, 94*3, 0, // 69 0000 0001 00
  504. 88*3, 89*3, 0, // 70 0000 0001 11
  505. 0, 0, 0x0203, // 71 0000 0010 11.
  506. 0, 0, 0x0104, // 72 0000 0011 00.
  507. 0, 0, 0x0007, // 73 0000 0010 10.
  508. 0, 0, 0x0402, // 74 0000 0011 11.
  509. 0, 0, 0x0502, // 75 0000 0010 01.
  510. 0, 0, 0x1001, // 76 0000 0010 00.
  511. 0, 0, 0x0f01, // 77 0000 0011 01.
  512. 0, 0, 0x0e01, // 78 0000 0011 10.
  513. 105*3, 107*3, 0, // 79 0000 0000 111
  514. 111*3, 114*3, 0, // 80 0000 0001 011
  515. 104*3, 97*3, 0, // 81 0000 0000 010
  516. 125*3, 119*3, 0, // 82 0000 0000 110
  517. 96*3, 98*3, 0, // 83 0000 0000 100
  518. -1, 123*3, 0, // 84 0000 0000 000
  519. 95*3, 101*3, 0, // 85 0000 0000 001
  520. 106*3, 121*3, 0, // 86 0000 0000 101
  521. 99*3, 102*3, 0, // 87 0000 0000 011
  522. 113*3, 103*3, 0, // 88 0000 0001 110
  523. 112*3, 116*3, 0, // 89 0000 0001 111
  524. 110*3, 100*3, 0, // 90 0000 0001 000
  525. 124*3, 115*3, 0, // 91 0000 0001 010
  526. 117*3, 122*3, 0, // 92 0000 0001 101
  527. 109*3, 118*3, 0, // 93 0000 0001 100
  528. 120*3, 108*3, 0, // 94 0000 0001 001
  529. 127*3, 136*3, 0, // 95 0000 0000 0010
  530. 139*3, 140*3, 0, // 96 0000 0000 1000
  531. 130*3, 126*3, 0, // 97 0000 0000 0101
  532. 145*3, 146*3, 0, // 98 0000 0000 1001
  533. 128*3, 129*3, 0, // 99 0000 0000 0110
  534. 0, 0, 0x0802, // 100 0000 0001 0001.
  535. 132*3, 134*3, 0, // 101 0000 0000 0011
  536. 155*3, 154*3, 0, // 102 0000 0000 0111
  537. 0, 0, 0x0008, // 103 0000 0001 1101.
  538. 137*3, 133*3, 0, // 104 0000 0000 0100
  539. 143*3, 144*3, 0, // 105 0000 0000 1110
  540. 151*3, 138*3, 0, // 106 0000 0000 1010
  541. 142*3, 141*3, 0, // 107 0000 0000 1111
  542. 0, 0, 0x000a, // 108 0000 0001 0011.
  543. 0, 0, 0x0009, // 109 0000 0001 1000.
  544. 0, 0, 0x000b, // 110 0000 0001 0000.
  545. 0, 0, 0x1501, // 111 0000 0001 0110.
  546. 0, 0, 0x0602, // 112 0000 0001 1110.
  547. 0, 0, 0x0303, // 113 0000 0001 1100.
  548. 0, 0, 0x1401, // 114 0000 0001 0111.
  549. 0, 0, 0x0702, // 115 0000 0001 0101.
  550. 0, 0, 0x1101, // 116 0000 0001 1111.
  551. 0, 0, 0x1201, // 117 0000 0001 1010.
  552. 0, 0, 0x1301, // 118 0000 0001 1001.
  553. 148*3, 152*3, 0, // 119 0000 0000 1101
  554. 0, 0, 0x0403, // 120 0000 0001 0010.
  555. 153*3, 150*3, 0, // 121 0000 0000 1011
  556. 0, 0, 0x0105, // 122 0000 0001 1011.
  557. 131*3, 135*3, 0, // 123 0000 0000 0001
  558. 0, 0, 0x0204, // 124 0000 0001 0100.
  559. 149*3, 147*3, 0, // 125 0000 0000 1100
  560. 172*3, 173*3, 0, // 126 0000 0000 0101 1
  561. 162*3, 158*3, 0, // 127 0000 0000 0010 0
  562. 170*3, 161*3, 0, // 128 0000 0000 0110 0
  563. 168*3, 166*3, 0, // 129 0000 0000 0110 1
  564. 157*3, 179*3, 0, // 130 0000 0000 0101 0
  565. 169*3, 167*3, 0, // 131 0000 0000 0001 0
  566. 174*3, 171*3, 0, // 132 0000 0000 0011 0
  567. 178*3, 177*3, 0, // 133 0000 0000 0100 1
  568. 156*3, 159*3, 0, // 134 0000 0000 0011 1
  569. 164*3, 165*3, 0, // 135 0000 0000 0001 1
  570. 183*3, 182*3, 0, // 136 0000 0000 0010 1
  571. 175*3, 176*3, 0, // 137 0000 0000 0100 0
  572. 0, 0, 0x0107, // 138 0000 0000 1010 1.
  573. 0, 0, 0x0a02, // 139 0000 0000 1000 0.
  574. 0, 0, 0x0902, // 140 0000 0000 1000 1.
  575. 0, 0, 0x1601, // 141 0000 0000 1111 1.
  576. 0, 0, 0x1701, // 142 0000 0000 1111 0.
  577. 0, 0, 0x1901, // 143 0000 0000 1110 0.
  578. 0, 0, 0x1801, // 144 0000 0000 1110 1.
  579. 0, 0, 0x0503, // 145 0000 0000 1001 0.
  580. 0, 0, 0x0304, // 146 0000 0000 1001 1.
  581. 0, 0, 0x000d, // 147 0000 0000 1100 1.
  582. 0, 0, 0x000c, // 148 0000 0000 1101 0.
  583. 0, 0, 0x000e, // 149 0000 0000 1100 0.
  584. 0, 0, 0x000f, // 150 0000 0000 1011 1.
  585. 0, 0, 0x0205, // 151 0000 0000 1010 0.
  586. 0, 0, 0x1a01, // 152 0000 0000 1101 1.
  587. 0, 0, 0x0106, // 153 0000 0000 1011 0.
  588. 180*3, 181*3, 0, // 154 0000 0000 0111 1
  589. 160*3, 163*3, 0, // 155 0000 0000 0111 0
  590. 196*3, 199*3, 0, // 156 0000 0000 0011 10
  591. 0, 0, 0x001b, // 157 0000 0000 0101 00.
  592. 203*3, 185*3, 0, // 158 0000 0000 0010 01
  593. 202*3, 201*3, 0, // 159 0000 0000 0011 11
  594. 0, 0, 0x0013, // 160 0000 0000 0111 00.
  595. 0, 0, 0x0016, // 161 0000 0000 0110 01.
  596. 197*3, 207*3, 0, // 162 0000 0000 0010 00
  597. 0, 0, 0x0012, // 163 0000 0000 0111 01.
  598. 191*3, 192*3, 0, // 164 0000 0000 0001 10
  599. 188*3, 190*3, 0, // 165 0000 0000 0001 11
  600. 0, 0, 0x0014, // 166 0000 0000 0110 11.
  601. 184*3, 194*3, 0, // 167 0000 0000 0001 01
  602. 0, 0, 0x0015, // 168 0000 0000 0110 10.
  603. 186*3, 193*3, 0, // 169 0000 0000 0001 00
  604. 0, 0, 0x0017, // 170 0000 0000 0110 00.
  605. 204*3, 198*3, 0, // 171 0000 0000 0011 01
  606. 0, 0, 0x0019, // 172 0000 0000 0101 10.
  607. 0, 0, 0x0018, // 173 0000 0000 0101 11.
  608. 200*3, 205*3, 0, // 174 0000 0000 0011 00
  609. 0, 0, 0x001f, // 175 0000 0000 0100 00.
  610. 0, 0, 0x001e, // 176 0000 0000 0100 01.
  611. 0, 0, 0x001c, // 177 0000 0000 0100 11.
  612. 0, 0, 0x001d, // 178 0000 0000 0100 10.
  613. 0, 0, 0x001a, // 179 0000 0000 0101 01.
  614. 0, 0, 0x0011, // 180 0000 0000 0111 10.
  615. 0, 0, 0x0010, // 181 0000 0000 0111 11.
  616. 189*3, 206*3, 0, // 182 0000 0000 0010 11
  617. 187*3, 195*3, 0, // 183 0000 0000 0010 10
  618. 218*3, 211*3, 0, // 184 0000 0000 0001 010
  619. 0, 0, 0x0025, // 185 0000 0000 0010 011.
  620. 215*3, 216*3, 0, // 186 0000 0000 0001 000
  621. 0, 0, 0x0024, // 187 0000 0000 0010 100.
  622. 210*3, 212*3, 0, // 188 0000 0000 0001 110
  623. 0, 0, 0x0022, // 189 0000 0000 0010 110.
  624. 213*3, 209*3, 0, // 190 0000 0000 0001 111
  625. 221*3, 222*3, 0, // 191 0000 0000 0001 100
  626. 219*3, 208*3, 0, // 192 0000 0000 0001 101
  627. 217*3, 214*3, 0, // 193 0000 0000 0001 001
  628. 223*3, 220*3, 0, // 194 0000 0000 0001 011
  629. 0, 0, 0x0023, // 195 0000 0000 0010 101.
  630. 0, 0, 0x010b, // 196 0000 0000 0011 100.
  631. 0, 0, 0x0028, // 197 0000 0000 0010 000.
  632. 0, 0, 0x010c, // 198 0000 0000 0011 011.
  633. 0, 0, 0x010a, // 199 0000 0000 0011 101.
  634. 0, 0, 0x0020, // 200 0000 0000 0011 000.
  635. 0, 0, 0x0108, // 201 0000 0000 0011 111.
  636. 0, 0, 0x0109, // 202 0000 0000 0011 110.
  637. 0, 0, 0x0026, // 203 0000 0000 0010 010.
  638. 0, 0, 0x010d, // 204 0000 0000 0011 010.
  639. 0, 0, 0x010e, // 205 0000 0000 0011 001.
  640. 0, 0, 0x0021, // 206 0000 0000 0010 111.
  641. 0, 0, 0x0027, // 207 0000 0000 0010 001.
  642. 0, 0, 0x1f01, // 208 0000 0000 0001 1011.
  643. 0, 0, 0x1b01, // 209 0000 0000 0001 1111.
  644. 0, 0, 0x1e01, // 210 0000 0000 0001 1100.
  645. 0, 0, 0x1002, // 211 0000 0000 0001 0101.
  646. 0, 0, 0x1d01, // 212 0000 0000 0001 1101.
  647. 0, 0, 0x1c01, // 213 0000 0000 0001 1110.
  648. 0, 0, 0x010f, // 214 0000 0000 0001 0011.
  649. 0, 0, 0x0112, // 215 0000 0000 0001 0000.
  650. 0, 0, 0x0111, // 216 0000 0000 0001 0001.
  651. 0, 0, 0x0110, // 217 0000 0000 0001 0010.
  652. 0, 0, 0x0603, // 218 0000 0000 0001 0100.
  653. 0, 0, 0x0b02, // 219 0000 0000 0001 1010.
  654. 0, 0, 0x0e02, // 220 0000 0000 0001 0111.
  655. 0, 0, 0x0d02, // 221 0000 0000 0001 1000.
  656. 0, 0, 0x0c02, // 222 0000 0000 0001 1001.
  657. 0, 0, 0x0f02 // 223 0000 0000 0001 0110.
  658. };
  659. static const int PICTURE_TYPE_INTRA = 1;
  660. static const int PICTURE_TYPE_PREDICTIVE = 2;
  661. static const int PICTURE_TYPE_B = 3;
  662. static const int START_SEQUENCE = 0xB3;
  663. static const int START_SLICE_FIRST = 0x01;
  664. static const int START_SLICE_LAST = 0xAF;
  665. static const int START_PICTURE = 0x00;
  666. static const int START_EXTENSION = 0xB5;
  667. static const int START_USER_DATA = 0xB2;
  668. typedef struct mpeg1_planes_t {
  669. uint8_t *y;
  670. uint8_t *cr;
  671. uint8_t *cb;
  672. } mpeg1_planes_t;
  673. typedef struct mpeg1_decoder_t {
  674. float frame_rate;
  675. int width;
  676. int height;
  677. int mb_width;
  678. int mb_height;
  679. int mb_size;
  680. int coded_width;
  681. int coded_height;
  682. int coded_size;
  683. int half_width;
  684. int half_height;
  685. int picture_type;
  686. int full_pel_forward;
  687. int forward_f_code;
  688. int forward_r_size;
  689. int forward_f;
  690. int has_sequence_header;
  691. int quantizer_scale;
  692. int slice_begin;
  693. int macroblock_address;
  694. int mb_row;
  695. int mb_col;
  696. int macroblock_type;
  697. int macroblock_intra;
  698. int macroblock_motion_fw;
  699. int motion_fw_h;
  700. int motion_fw_v;
  701. int motion_fw_h_prev;
  702. int motion_fw_v_prev;
  703. int dc_predictor_y;
  704. int dc_predictor_cr;
  705. int dc_predictor_cb;
  706. bit_buffer_t *bits;
  707. mpeg1_planes_t planes_current;
  708. mpeg1_planes_t planes_forward;
  709. int block_data[64];
  710. uint8_t intra_quant_matrix[64];
  711. uint8_t non_intra_quant_matrix[64];
  712. } mpeg1_decoder_t;
  713. void decode_sequence_header(mpeg1_decoder_t *self);
  714. void decode_picture(mpeg1_decoder_t *self);
  715. void init_buffers(mpeg1_decoder_t *self);
  716. void decode_slice(mpeg1_decoder_t *self, int slice);
  717. void decode_macroblock(mpeg1_decoder_t *self);
  718. void decode_motion_vectors(mpeg1_decoder_t *self);
  719. void copy_macroblock(mpeg1_decoder_t *self, int motion_h, int motion_v, uint8_t *s_y, uint8_t *s_cr, uint8_t *s_cb);
  720. void decode_block(mpeg1_decoder_t *self, int block);
  721. void copy_block_to_destination(int *block, uint8_t *dest, int index, int scan);
  722. void add_block_to_destination(int *block, uint8_t *dest, int index, int scan);
  723. void copy_value_to_destination(int value, uint8_t *dest, int index, int scan);
  724. void add_value_to_destination(int value, uint8_t *dest, int index, int scan);
  725. void idct(int *block);
  726. void zero_block_data(mpeg1_decoder_t *self);
  727. int read_huffman(bit_buffer_t *bits, const int *code_table);
  728. // -----------------------------------------------------------------------------
  729. // Public interface
  730. mpeg1_decoder_t *mpeg1_decoder_create(unsigned int buffer_size, bit_buffer_mode_t buffer_mode) {
  731. mpeg1_decoder_t *self = malloc(sizeof(mpeg1_decoder_t));
  732. memset(self, 0, sizeof(mpeg1_decoder_t));
  733. self->bits = bit_buffer_create(buffer_size, buffer_mode);
  734. return self;
  735. }
  736. void mpeg1_decoder_destroy(mpeg1_decoder_t *self) {
  737. bit_buffer_destroy(self->bits);
  738. if (self->has_sequence_header) {
  739. free(self->planes_current.y);
  740. free(self->planes_current.cr);
  741. free(self->planes_current.cb);
  742. free(self->planes_forward.y);
  743. free(self->planes_forward.cr);
  744. free(self->planes_forward.cb);
  745. }
  746. free(self);
  747. }
  748. void *mpeg1_decoder_get_write_ptr(mpeg1_decoder_t *self, unsigned int byte_size) {
  749. return bit_buffer_get_write_ptr(self->bits, byte_size);
  750. }
  751. int mpeg1_decoder_get_index(mpeg1_decoder_t *self) {
  752. return bit_buffer_get_index(self->bits);
  753. }
  754. void mpeg1_decoder_set_index(mpeg1_decoder_t *self, unsigned int index) {
  755. bit_buffer_set_index(self->bits, index);
  756. }
  757. void mpeg1_decoder_did_write(mpeg1_decoder_t *self, unsigned int byte_size) {
  758. bit_buffer_did_write(self->bits, byte_size);
  759. if (!self->has_sequence_header) {
  760. if (bit_buffer_find_start_code(self->bits, START_SEQUENCE) != -1) {
  761. decode_sequence_header(self);
  762. }
  763. }
  764. }
  765. int mpeg1_decoder_has_sequence_header(mpeg1_decoder_t *self) {
  766. return self->has_sequence_header;
  767. }
  768. float mpeg1_decoder_get_frame_rate(mpeg1_decoder_t *self) {
  769. return self->frame_rate;
  770. }
  771. int mpeg1_decoder_get_coded_size(mpeg1_decoder_t *self) {
  772. return self->coded_size;
  773. }
  774. int mpeg1_decoder_get_width(mpeg1_decoder_t *self) {
  775. return self->width;
  776. }
  777. int mpeg1_decoder_get_height(mpeg1_decoder_t *self) {
  778. return self->height;
  779. }
  780. void *mpeg1_decoder_get_y_ptr(mpeg1_decoder_t *self) {
  781. return self->planes_forward.y;
  782. }
  783. void *mpeg1_decoder_get_cr_ptr(mpeg1_decoder_t *self) {
  784. return self->planes_forward.cr;
  785. }
  786. void *mpeg1_decoder_get_cb_ptr(mpeg1_decoder_t *self) {
  787. return self->planes_forward.cb;
  788. }
  789. bool mpeg1_decoder_decode(mpeg1_decoder_t *self) {
  790. if (!self->has_sequence_header) {
  791. return false;
  792. }
  793. if (bit_buffer_find_start_code(self->bits, START_PICTURE) == -1) {
  794. return false;
  795. }
  796. decode_picture(self);
  797. return true;
  798. }
  799. // -----------------------------------------------------------------------------
  800. // Private methods
  801. void decode_sequence_header(mpeg1_decoder_t *self) {
  802. int previous_width = self->width;
  803. int previous_height = self->height;
  804. self->width = bit_buffer_read(self->bits, 12);
  805. self->height = bit_buffer_read(self->bits, 12);
  806. // skip pixel aspect ratio
  807. bit_buffer_skip(self->bits, 4);
  808. self->frame_rate = PICTURE_RATE[bit_buffer_read(self->bits, 4)];
  809. // skip bitRate, marker, bufferSize and constrained bit
  810. bit_buffer_skip(self->bits, 18 + 1 + 10 + 1);
  811. if (bit_buffer_read(self->bits, 1)) { // load custom intra quant matrix?
  812. for (int i = 0; i < 64; i++) {
  813. self->intra_quant_matrix[ZIG_ZAG[i]] = bit_buffer_read(self->bits, 8);
  814. }
  815. }
  816. else {
  817. memcpy(self->intra_quant_matrix, DEFAULT_INTRA_QUANT_MATRIX, 64);
  818. }
  819. if (bit_buffer_read(self->bits, 1)) { // load custom non intra quant matrix?
  820. for (int i = 0; i < 64; i++) {
  821. int idx = ZIG_ZAG[i];
  822. self->non_intra_quant_matrix[idx] = bit_buffer_read(self->bits, 8);
  823. }
  824. }
  825. else {
  826. memcpy(self->non_intra_quant_matrix, DEFAULT_NON_INTRA_QUANT_MATRIX, 64);
  827. }
  828. if (self->has_sequence_header) {
  829. if (self->width == previous_width && self->height == previous_height) {
  830. // We already had a sequence header with the same width/height;
  831. // nothing else to do here.
  832. return;
  833. }
  834. // We had a sequence header but with different dimensions;
  835. // delete the previous planes and allocate new.
  836. free(self->planes_current.y);
  837. free(self->planes_current.cr);
  838. free(self->planes_current.cb);
  839. free(self->planes_forward.y);
  840. free(self->planes_forward.cr);
  841. free(self->planes_forward.cb);
  842. }
  843. self->mb_width = (self->width + 15) >> 4;
  844. self->mb_height = (self->height + 15) >> 4;
  845. self->mb_size = self->mb_width * self->mb_height;
  846. self->coded_width = self->mb_width << 4;
  847. self->coded_height = self->mb_height << 4;
  848. self->coded_size = self->coded_width * self->coded_height;
  849. self->half_width = self->mb_width << 3;
  850. self->half_height = self->mb_height << 3;
  851. self->planes_current.y = (uint8_t*)malloc(self->coded_size);
  852. self->planes_current.cr = (uint8_t*)malloc(self->coded_size >> 2);
  853. self->planes_current.cb = (uint8_t*)malloc(self->coded_size >> 2);
  854. self->planes_forward.y = (uint8_t*)malloc(self->coded_size);
  855. self->planes_forward.cr = (uint8_t*)malloc(self->coded_size >> 2);
  856. self->planes_forward.cb = (uint8_t*)malloc(self->coded_size >> 2);
  857. self->has_sequence_header = true;
  858. }
  859. void decode_picture(mpeg1_decoder_t *self) {
  860. bit_buffer_skip(self->bits, 10); // skip temporalReference
  861. self->picture_type = bit_buffer_read(self->bits, 3);
  862. bit_buffer_skip(self->bits, 16); // skip vbv_delay
  863. // Skip B and D frames or unknown coding type
  864. if (self->picture_type <= 0 || self->picture_type >= PICTURE_TYPE_B) {
  865. return;
  866. }
  867. // full_pel_forward, forward_f_code
  868. if (self->picture_type == PICTURE_TYPE_PREDICTIVE) {
  869. self->full_pel_forward = bit_buffer_read(self->bits, 1);
  870. self->forward_f_code = bit_buffer_read(self->bits, 3);
  871. if (self->forward_f_code == 0) {
  872. // Ignore picture with zero self->forward_f_code
  873. return;
  874. }
  875. self->forward_r_size = self->forward_f_code - 1;
  876. self->forward_f = 1 << self->forward_r_size;
  877. }
  878. int code = 0;
  879. do {
  880. code = bit_buffer_find_next_start_code(self->bits);
  881. } while (code == START_EXTENSION || code == START_USER_DATA);
  882. while (code >= START_SLICE_FIRST && code <= START_SLICE_LAST) {
  883. decode_slice(self, code & 0x000000FF);
  884. code = bit_buffer_find_next_start_code(self->bits);
  885. }
  886. if (code != -1) {
  887. // We found the next start code; rewind 32self->bits and let the main loop
  888. // handle it.
  889. bit_buffer_rewind(self->bits, 32);
  890. }
  891. // If this is a reference picutre then rotate the prediction pointers
  892. if (
  893. self->picture_type == PICTURE_TYPE_INTRA ||
  894. self->picture_type == PICTURE_TYPE_PREDICTIVE
  895. ) {
  896. mpeg1_planes_t temp = self->planes_forward;
  897. self->planes_forward = self->planes_current;
  898. self->planes_current = temp;
  899. }
  900. }
  901. // Slice Layer
  902. void decode_slice(mpeg1_decoder_t *self, int slice) {
  903. self->slice_begin = true;
  904. self->macroblock_address = (slice - 1) * self->mb_width - 1;
  905. // Reset motion vectors and DC predictors
  906. self->motion_fw_h = self->motion_fw_h_prev = 0;
  907. self->motion_fw_v = self->motion_fw_v_prev = 0;
  908. self->dc_predictor_y = 128;
  909. self->dc_predictor_cr = 128;
  910. self->dc_predictor_cb = 128;
  911. self->quantizer_scale = bit_buffer_read(self->bits, 5);
  912. // skip extra self->bits
  913. while (bit_buffer_read(self->bits, 1)) {
  914. bit_buffer_skip(self->bits, 8);
  915. }
  916. do {
  917. decode_macroblock(self);
  918. } while (!bit_buffer_next_bytes_are_start_code(self->bits));
  919. };
  920. // Macroblock Layer
  921. void decode_macroblock(mpeg1_decoder_t *self) {
  922. // Decode self->macroblock_address_increment
  923. int increment = 0;
  924. int x = 0;
  925. int t = read_huffman(self->bits, MACROBLOCK_ADDRESS_INCREMENT);
  926. while (t == 34) {
  927. // macroblock_stuffing
  928. x = 1;
  929. t = read_huffman(self->bits, MACROBLOCK_ADDRESS_INCREMENT);
  930. }
  931. while (t == 35) {
  932. // macroblock_escape
  933. increment += 33;
  934. x =
  935. t = read_huffman(self->bits, MACROBLOCK_ADDRESS_INCREMENT);
  936. }
  937. increment += t;
  938. // Process any skipped macroblocks
  939. if (self->slice_begin) {
  940. // The first self->macroblock_address_increment of each slice is relative
  941. // to beginning of the preverious row, not the preverious macroblock
  942. self->slice_begin = false;
  943. self->macroblock_address += increment;
  944. }
  945. else {
  946. if (self->macroblock_address + increment >= self->mb_size) {
  947. // Illegal (too large) self->macroblock_address_increment
  948. // abort();
  949. return;
  950. }
  951. if (increment > 1) {
  952. // Skipped macroblocks reset DC predictors
  953. self->dc_predictor_y = 128;
  954. self->dc_predictor_cr = 128;
  955. self->dc_predictor_cb = 128;
  956. // Skipped macroblocks in P-pictures reset motion vectors
  957. if (self->picture_type == PICTURE_TYPE_PREDICTIVE) {
  958. self->motion_fw_h = self->motion_fw_h_prev = 0;
  959. self->motion_fw_v = self->motion_fw_v_prev = 0;
  960. }
  961. }
  962. // Predict skipped macroblocks
  963. while (increment > 1) {
  964. self->macroblock_address++;
  965. self->mb_row = (self->macroblock_address / self->mb_width)|0;
  966. self->mb_col = self->macroblock_address % self->mb_width;
  967. copy_macroblock(
  968. self,
  969. self->motion_fw_h, self->motion_fw_v,
  970. self->planes_forward.y, self->planes_forward.cr, self->planes_forward.cb
  971. );
  972. increment--;
  973. }
  974. self->macroblock_address++;
  975. }
  976. self->mb_row = (self->macroblock_address / self->mb_width)|0;
  977. self->mb_col = self->macroblock_address % self->mb_width;
  978. // Process the current macroblock
  979. // static const s16 *mbTable = MACROBLOCK_TYPE[self->picture_type];
  980. // macroblock_type = read_huffman(self->bits, mbTable);
  981. if (self->picture_type == PICTURE_TYPE_INTRA) {
  982. self->macroblock_type = read_huffman(self->bits, MACROBLOCK_TYPE_INTRA);
  983. }
  984. else if (self->picture_type == PICTURE_TYPE_PREDICTIVE) {
  985. self->macroblock_type = read_huffman(self->bits, MACROBLOCK_TYPE_PREDICTIVE);
  986. }
  987. else {
  988. // Unhandled picture type
  989. // abort();
  990. }
  991. self->macroblock_intra = (self->macroblock_type & 0x01);
  992. self->macroblock_motion_fw = (self->macroblock_type & 0x08);
  993. // Quantizer scale
  994. if ((self->macroblock_type & 0x10) != 0) {
  995. self->quantizer_scale = bit_buffer_read(self->bits, 5);
  996. }
  997. if (self->macroblock_intra) {
  998. // Intra-coded macroblocks reset motion vectors
  999. self->motion_fw_h = self->motion_fw_h_prev = 0;
  1000. self->motion_fw_v = self->motion_fw_v_prev = 0;
  1001. }
  1002. else {
  1003. // Non-intra macroblocks reset DC predictors
  1004. self->dc_predictor_y = 128;
  1005. self->dc_predictor_cr = 128;
  1006. self->dc_predictor_cb = 128;
  1007. decode_motion_vectors(self);
  1008. copy_macroblock(
  1009. self,
  1010. self->motion_fw_h, self->motion_fw_v,
  1011. self->planes_forward.y, self->planes_forward.cr, self->planes_forward.cb
  1012. );
  1013. }
  1014. // Decode blocks
  1015. int cbp = ((self->macroblock_type & 0x02) != 0)
  1016. ? read_huffman(self->bits, CODE_BLOCK_PATTERN)
  1017. : (self->macroblock_intra ? 0x3f : 0);
  1018. for (int block = 0, mask = 0x20; block < 6; block++) {
  1019. if ((cbp & mask) != 0) {
  1020. decode_block(self, block);
  1021. }
  1022. mask >>= 1;
  1023. }
  1024. };
  1025. void decode_motion_vectors(mpeg1_decoder_t *self) {
  1026. int code, d, r = 0;
  1027. // Forward
  1028. if (self->macroblock_motion_fw) {
  1029. // Horizontal forward
  1030. code = read_huffman(self->bits, MOTION);
  1031. if ((code != 0) && (self->forward_f != 1)) {
  1032. r = bit_buffer_read(self->bits, self->forward_r_size);
  1033. d = ((abs(code) - 1) << self->forward_r_size) + r + 1;
  1034. if (code < 0) {
  1035. d = -d;
  1036. }
  1037. }
  1038. else {
  1039. d = code;
  1040. }
  1041. self->motion_fw_h_prev += d;
  1042. if (self->motion_fw_h_prev > (self->forward_f << 4) - 1) {
  1043. self->motion_fw_h_prev -= self->forward_f << 5;
  1044. }
  1045. else if (self->motion_fw_h_prev < ((-self->forward_f) << 4)) {
  1046. self->motion_fw_h_prev += self->forward_f << 5;
  1047. }
  1048. self->motion_fw_h = self->motion_fw_h_prev;
  1049. if (self->full_pel_forward) {
  1050. self->motion_fw_h <<= 1;
  1051. }
  1052. // Vertical forward
  1053. code = read_huffman(self->bits, MOTION);
  1054. if ((code != 0) && (self->forward_f != 1)) {
  1055. r = bit_buffer_read(self->bits, self->forward_r_size);
  1056. d = ((abs(code) - 1) << self->forward_r_size) + r + 1;
  1057. if (code < 0) {
  1058. d = -d;
  1059. }
  1060. }
  1061. else {
  1062. d = code;
  1063. }
  1064. self->motion_fw_v_prev += d;
  1065. if (self->motion_fw_v_prev > (self->forward_f << 4) - 1) {
  1066. self->motion_fw_v_prev -= self->forward_f << 5;
  1067. }
  1068. else if (self->motion_fw_v_prev < ((-self->forward_f) << 4)) {
  1069. self->motion_fw_v_prev += self->forward_f << 5;
  1070. }
  1071. self->motion_fw_v = self->motion_fw_v_prev;
  1072. if (self->full_pel_forward) {
  1073. self->motion_fw_v <<= 1;
  1074. }
  1075. }
  1076. else if (self->picture_type == PICTURE_TYPE_PREDICTIVE) {
  1077. // No motion information in P-picture, reset vectors
  1078. self->motion_fw_h = self->motion_fw_h_prev = 0;
  1079. self->motion_fw_v = self->motion_fw_v_prev = 0;
  1080. }
  1081. }
  1082. void copy_macroblock(mpeg1_decoder_t *self, int motion_h, int motion_v, uint8_t *s_y, uint8_t *s_cr, uint8_t *s_cb) {
  1083. int
  1084. width, scan,
  1085. H, V,
  1086. src, dest, last;
  1087. bool odd_h, odd_v;
  1088. // We use 32bit writes here
  1089. int *d_y = (int*)self->planes_current.y;
  1090. int *d_cb = (int*)self->planes_current.cb;
  1091. int *d_cr = (int*)self->planes_current.cr;
  1092. // Luminance
  1093. width = self->coded_width;
  1094. scan = width - 16;
  1095. H = motion_h >> 1;
  1096. V = motion_v >> 1;
  1097. odd_h = (motion_h & 1) == 1;
  1098. odd_v = (motion_v & 1) == 1;
  1099. src = ((self->mb_row << 4) + V) * width + (self->mb_col << 4) + H;
  1100. dest = (self->mb_row * width + self->mb_col) << 2;
  1101. last = dest + (width << 2);
  1102. int x, y1, y2, y;
  1103. if (odd_h) {
  1104. if (odd_v) {
  1105. while (dest < last) {
  1106. y1 = s_y[src] + s_y[src+width]; src++;
  1107. for (x = 0; x < 4; x++) {
  1108. y2 = s_y[src] + s_y[src+width]; src++;
  1109. y = (((y1 + y2 + 2) >> 2) & 0xff);
  1110. y1 = s_y[src] + s_y[src+width]; src++;
  1111. y |= (((y1 + y2 + 2) << 6) & 0xff00);
  1112. y2 = s_y[src] + s_y[src+width]; src++;
  1113. y |= (((y1 + y2 + 2) << 14) & 0xff0000);
  1114. y1 = s_y[src] + s_y[src+width]; src++;
  1115. y |= (((y1 + y2 + 2) << 22) & 0xff000000);
  1116. d_y[dest++] = y;
  1117. }
  1118. dest += scan >> 2; src += scan-1;
  1119. }
  1120. }
  1121. else {
  1122. while (dest < last) {
  1123. y1 = s_y[src++];
  1124. for (x = 0; x < 4; x++) {
  1125. y2 = s_y[src++];
  1126. y = (((y1 + y2 + 1) >> 1) & 0xff);
  1127. y1 = s_y[src++];
  1128. y |= (((y1 + y2 + 1) << 7) & 0xff00);
  1129. y2 = s_y[src++];
  1130. y |= (((y1 + y2 + 1) << 15) & 0xff0000);
  1131. y1 = s_y[src++];
  1132. y |= (((y1 + y2 + 1) << 23) & 0xff000000);
  1133. d_y[dest++] = y;
  1134. }
  1135. dest += scan >> 2; src += scan-1;
  1136. }
  1137. }
  1138. }
  1139. else {
  1140. if (odd_v) {
  1141. while (dest < last) {
  1142. for (x = 0; x < 4; x++) {
  1143. y = (((s_y[src] + s_y[src+width] + 1) >> 1) & 0xff); src++;
  1144. y |= (((s_y[src] + s_y[src+width] + 1) << 7) & 0xff00); src++;
  1145. y |= (((s_y[src] + s_y[src+width] + 1) << 15) & 0xff0000); src++;
  1146. y |= (((s_y[src] + s_y[src+width] + 1) << 23) & 0xff000000); src++;
  1147. d_y[dest++] = y;
  1148. }
  1149. dest += scan >> 2; src += scan;
  1150. }
  1151. }
  1152. else {
  1153. while (dest < last) {
  1154. for (x = 0; x < 4; x++) {
  1155. y = s_y[src]; src++;
  1156. y |= s_y[src] << 8; src++;
  1157. y |= s_y[src] << 16; src++;
  1158. y |= s_y[src] << 24; src++;
  1159. d_y[dest++] = y;
  1160. }
  1161. dest += scan >> 2; src += scan;
  1162. }
  1163. }
  1164. }
  1165. // Chrominance
  1166. width = self->half_width;
  1167. scan = width - 8;
  1168. H = (motion_h/2) >> 1;
  1169. V = (motion_v/2) >> 1;
  1170. odd_h = ((motion_h/2) & 1) == 1;
  1171. odd_v = ((motion_v/2) & 1) == 1;
  1172. src = ((self->mb_row << 3) + V) * width + (self->mb_col << 3) + H;
  1173. dest = (self->mb_row * width + self->mb_col) << 1;
  1174. last = dest + (width << 1);
  1175. int cr1, cr2, cr,
  1176. cb1, cb2, cb;
  1177. if (odd_h) {
  1178. if (odd_v) {
  1179. while (dest < last) {
  1180. cr1 = s_cr[src] + s_cr[src+width];
  1181. cb1 = s_cb[src] + s_cb[src+width];
  1182. src++;
  1183. for (x = 0; x < 2; x++) {
  1184. cr2 = s_cr[src] + s_cr[src+width];
  1185. cb2 = s_cb[src] + s_cb[src+width]; src++;
  1186. cr = (((cr1 + cr2 + 2) >> 2) & 0xff);
  1187. cb = (((cb1 + cb2 + 2) >> 2) & 0xff);
  1188. cr1 = s_cr[src] + s_cr[src+width];
  1189. cb1 = s_cb[src] + s_cb[src+width]; src++;
  1190. cr |= (((cr1 + cr2 + 2) << 6) & 0xff00);
  1191. cb |= (((cb1 + cb2 + 2) << 6) & 0xff00);
  1192. cr2 = s_cr[src] + s_cr[src+width];
  1193. cb2 = s_cb[src] + s_cb[src+width]; src++;
  1194. cr |= (((cr1 + cr2 + 2) << 14) & 0xff0000);
  1195. cb |= (((cb1 + cb2 + 2) << 14) & 0xff0000);
  1196. cr1 = s_cr[src] + s_cr[src+width];
  1197. cb1 = s_cb[src] + s_cb[src+width]; src++;
  1198. cr |= (((cr1 + cr2 + 2) << 22) & 0xff000000);
  1199. cb |= (((cb1 + cb2 + 2) << 22) & 0xff000000);
  1200. d_cr[dest] = cr;
  1201. d_cb[dest] = cb;
  1202. dest++;
  1203. }
  1204. dest += scan >> 2; src += scan-1;
  1205. }
  1206. }
  1207. else {
  1208. while (dest < last) {
  1209. cr1 = s_cr[src];
  1210. cb1 = s_cb[src];
  1211. src++;
  1212. for (x = 0; x < 2; x++) {
  1213. cr2 = s_cr[src];
  1214. cb2 = s_cb[src++];
  1215. cr = (((cr1 + cr2 + 1) >> 1) & 0xff);
  1216. cb = (((cb1 + cb2 + 1) >> 1) & 0xff);
  1217. cr1 = s_cr[src];
  1218. cb1 = s_cb[src++];
  1219. cr |= (((cr1 + cr2 + 1) << 7) & 0xff00);
  1220. cb |= (((cb1 + cb2 + 1) << 7) & 0xff00);
  1221. cr2 = s_cr[src];
  1222. cb2 = s_cb[src++];
  1223. cr |= (((cr1 + cr2 + 1) << 15) & 0xff0000);
  1224. cb |= (((cb1 + cb2 + 1) << 15) & 0xff0000);
  1225. cr1 = s_cr[src];
  1226. cb1 = s_cb[src++];
  1227. cr |= (((cr1 + cr2 + 1) << 23) & 0xff000000);
  1228. cb |= (((cb1 + cb2 + 1) << 23) & 0xff000000);
  1229. d_cr[dest] = cr;
  1230. d_cb[dest] = cb;
  1231. dest++;
  1232. }
  1233. dest += scan >> 2; src += scan-1;
  1234. }
  1235. }
  1236. }
  1237. else {
  1238. if (odd_v) {
  1239. while (dest < last) {
  1240. for (x = 0; x < 2; x++) {
  1241. cr = (((s_cr[src] + s_cr[src+width] + 1) >> 1) & 0xff);
  1242. cb = (((s_cb[src] + s_cb[src+width] + 1) >> 1) & 0xff); src++;
  1243. cr |= (((s_cr[src] + s_cr[src+width] + 1) << 7) & 0xff00);
  1244. cb |= (((s_cb[src] + s_cb[src+width] + 1) << 7) & 0xff00); src++;
  1245. cr |= (((s_cr[src] + s_cr[src+width] + 1) << 15) & 0xff0000);
  1246. cb |= (((s_cb[src] + s_cb[src+width] + 1) << 15) & 0xff0000); src++;
  1247. cr |= (((s_cr[src] + s_cr[src+width] + 1) << 23) & 0xff000000);
  1248. cb |= (((s_cb[src] + s_cb[src+width] + 1) << 23) & 0xff000000); src++;
  1249. d_cr[dest] = cr;
  1250. d_cb[dest] = cb;
  1251. dest++;
  1252. }
  1253. dest += scan >> 2; src += scan;
  1254. }
  1255. }
  1256. else {
  1257. while (dest < last) {
  1258. for (x = 0; x < 2; x++) {
  1259. cr = s_cr[src];
  1260. cb = s_cb[src]; src++;
  1261. cr |= s_cr[src] << 8;
  1262. cb |= s_cb[src] << 8; src++;
  1263. cr |= s_cr[src] << 16;
  1264. cb |= s_cb[src] << 16; src++;
  1265. cr |= s_cr[src] << 24;
  1266. cb |= s_cb[src] << 24; src++;
  1267. d_cr[dest] = cr;
  1268. d_cb[dest] = cb;
  1269. dest++;
  1270. }
  1271. dest += scan >> 2; src += scan;
  1272. }
  1273. }
  1274. }
  1275. }
  1276. // Block layer
  1277. void decode_block(mpeg1_decoder_t *self, int block) {
  1278. int n = 0;
  1279. uint8_t *quant_matrix;
  1280. // Decode DC coefficient of intra-coded blocks
  1281. if (self->macroblock_intra) {
  1282. int predictor;
  1283. int dctSize;
  1284. // DC prediction
  1285. if (block < 4) {
  1286. predictor = self->dc_predictor_y;
  1287. dctSize = read_huffman(self->bits, DCT_DC_SIZE_LUMINANCE);
  1288. }
  1289. else {
  1290. predictor = (block == 4 ? self->dc_predictor_cr : self->dc_predictor_cb);
  1291. dctSize = read_huffman(self->bits, DCT_DC_SIZE_CHROMINANCE);
  1292. }
  1293. // Read DC coeff
  1294. if (dctSize > 0) {
  1295. int differential = bit_buffer_read(self->bits, dctSize);
  1296. if ((differential & (1 << (dctSize - 1))) != 0) {
  1297. self->block_data[0] = predictor + differential;
  1298. }
  1299. else {
  1300. self->block_data[0] = predictor + ((-1 << dctSize)|(differential+1));
  1301. }
  1302. }
  1303. else {
  1304. self->block_data[0] = predictor;
  1305. }
  1306. // Save predictor value
  1307. if (block < 4) {
  1308. self->dc_predictor_y = self->block_data[0];
  1309. }
  1310. else if (block == 4) {
  1311. self->dc_predictor_cr = self->block_data[0];
  1312. }
  1313. else {
  1314. self->dc_predictor_cb = self->block_data[0];
  1315. }
  1316. // Dequantize + premultiply
  1317. self->block_data[0] <<= (3 + 5);
  1318. quant_matrix = self->intra_quant_matrix;
  1319. n = 1;
  1320. }
  1321. else {
  1322. quant_matrix = self->non_intra_quant_matrix;
  1323. }
  1324. // Decode AC coefficients (+DC for non-intra)
  1325. int level = 0;
  1326. while (true) {
  1327. int run = 0;
  1328. int coeff = read_huffman(self->bits, DCT_COEFF);
  1329. if ((coeff == 0x0001) && (n > 0) && (bit_buffer_read(self->bits, 1) == 0)) {
  1330. // end_of_block
  1331. break;
  1332. }
  1333. if (coeff == 0xffff) {
  1334. // escape
  1335. run = bit_buffer_read(self->bits, 6);
  1336. level = bit_buffer_read(self->bits, 8);
  1337. if (level == 0) {
  1338. level = bit_buffer_read(self->bits, 8);
  1339. }
  1340. else if (level == 128) {
  1341. level = bit_buffer_read(self->bits, 8) - 256;
  1342. }
  1343. else if (level > 128) {
  1344. level = level - 256;
  1345. }
  1346. }
  1347. else {
  1348. run = coeff >> 8;
  1349. level = coeff & 0xff;
  1350. if (bit_buffer_read(self->bits, 1)) {
  1351. level = -level;
  1352. }
  1353. }
  1354. n += run;
  1355. int dezigZagged = ZIG_ZAG[n];
  1356. n++;
  1357. // Dequantize, oddify, clip
  1358. level <<= 1;
  1359. if (!self->macroblock_intra) {
  1360. level += (level < 0 ? -1 : 1);
  1361. }
  1362. level = (level * self->quantizer_scale * quant_matrix[dezigZagged]) >> 4;
  1363. if ((level & 1) == 0) {
  1364. level -= level > 0 ? 1 : -1;
  1365. }
  1366. if (level > 2047) {
  1367. level = 2047;
  1368. }
  1369. else if (level < -2048) {
  1370. level = -2048;
  1371. }
  1372. // Save premultiplied coefficient
  1373. self->block_data[dezigZagged] = level * PREMULTIPLIER_MATRIX[dezigZagged];
  1374. }
  1375. // Move block to its place
  1376. uint8_t *dest_array;
  1377. int dest_index;
  1378. int scan;
  1379. if (block < 4) {
  1380. dest_array = self->planes_current.y;
  1381. scan = self->coded_width - 8;
  1382. dest_index = (self->mb_row * self->coded_width + self->mb_col) << 4;
  1383. if ((block & 1) != 0) {
  1384. dest_index += 8;
  1385. }
  1386. if ((block & 2) != 0) {
  1387. dest_index += self->coded_width << 3;
  1388. }
  1389. }
  1390. else {
  1391. dest_array = (block == 4) ? self->planes_current.cb : self->planes_current.cr;
  1392. scan = (self->coded_width >> 1) - 8;
  1393. dest_index = ((self->mb_row * self->coded_width) << 2) + (self->mb_col << 3);
  1394. }
  1395. if (self->macroblock_intra) {
  1396. // Overwrite (no prediction)
  1397. if (n == 1) {
  1398. copy_value_to_destination((self->block_data[0] + 128) >> 8, dest_array, dest_index, scan);
  1399. self->block_data[0] = 0;
  1400. }
  1401. else {
  1402. idct(self->block_data);
  1403. copy_block_to_destination(self->block_data, dest_array, dest_index, scan);
  1404. zero_block_data(self);
  1405. }
  1406. }
  1407. else {
  1408. // Add data to the predicted macroblock
  1409. if (n == 1) {
  1410. add_value_to_destination((self->block_data[0] + 128) >> 8, dest_array, dest_index, scan);
  1411. self->block_data[0] = 0;
  1412. }
  1413. else {
  1414. idct(self->block_data);
  1415. add_block_to_destination(self->block_data, dest_array, dest_index, scan);
  1416. zero_block_data(self);
  1417. }
  1418. }
  1419. n = 0;
  1420. }
  1421. // -----------------------------------------------------------------------------
  1422. // Private functions
  1423. void zero_block_data(mpeg1_decoder_t *self) {
  1424. for (int i = 0; i < 64; i++) {
  1425. self->block_data[i] = 0;
  1426. }
  1427. }
  1428. inline uint8_t clamp_to_uint8(int n) {
  1429. return n > 255
  1430. ? 255
  1431. : (n < 0 ? 0 : n);
  1432. }
  1433. void copy_block_to_destination(int *block, uint8_t *dest, int index, int scan) {
  1434. for (int n = 0; n < 64; n += 8, index += scan+8) {
  1435. dest[index+0] = clamp_to_uint8(block[n+0]);
  1436. dest[index+1] = clamp_to_uint8(block[n+1]);
  1437. dest[index+2] = clamp_to_uint8(block[n+2]);
  1438. dest[index+3] = clamp_to_uint8(block[n+3]);
  1439. dest[index+4] = clamp_to_uint8(block[n+4]);
  1440. dest[index+5] = clamp_to_uint8(block[n+5]);
  1441. dest[index+6] = clamp_to_uint8(block[n+6]);
  1442. dest[index+7] = clamp_to_uint8(block[n+7]);
  1443. }
  1444. }
  1445. void add_block_to_destination(int *block, uint8_t *dest, int index, int scan) {
  1446. for (int n = 0; n < 64; n += 8, index += scan+8) {
  1447. dest[index+0] = clamp_to_uint8(dest[index+0] + block[n+0]);
  1448. dest[index+1] = clamp_to_uint8(dest[index+1] + block[n+1]);
  1449. dest[index+2] = clamp_to_uint8(dest[index+2] + block[n+2]);
  1450. dest[index+3] = clamp_to_uint8(dest[index+3] + block[n+3]);
  1451. dest[index+4] = clamp_to_uint8(dest[index+4] + block[n+4]);
  1452. dest[index+5] = clamp_to_uint8(dest[index+5] + block[n+5]);
  1453. dest[index+6] = clamp_to_uint8(dest[index+6] + block[n+6]);
  1454. dest[index+7] = clamp_to_uint8(dest[index+7] + block[n+7]);
  1455. }
  1456. }
  1457. void copy_value_to_destination(int value, uint8_t *dest, int index, int scan) {
  1458. value = clamp_to_uint8(value);
  1459. for (int n = 0; n < 64; n += 8, index += scan+8) {
  1460. dest[index+0] = value;
  1461. dest[index+1] = value;
  1462. dest[index+2] = value;
  1463. dest[index+3] = value;
  1464. dest[index+4] = value;
  1465. dest[index+5] = value;
  1466. dest[index+6] = value;
  1467. dest[index+7] = value;
  1468. }
  1469. }
  1470. void add_value_to_destination(int value, uint8_t *dest, int index, int scan) {
  1471. for (int n = 0; n < 64; n += 8, index += scan+8) {
  1472. dest[index+0] = clamp_to_uint8(dest[index+0] + value);
  1473. dest[index+1] = clamp_to_uint8(dest[index+1] + value);
  1474. dest[index+2] = clamp_to_uint8(dest[index+2] + value);
  1475. dest[index+3] = clamp_to_uint8(dest[index+3] + value);
  1476. dest[index+4] = clamp_to_uint8(dest[index+4] + value);
  1477. dest[index+5] = clamp_to_uint8(dest[index+5] + value);
  1478. dest[index+6] = clamp_to_uint8(dest[index+6] + value);
  1479. dest[index+7] = clamp_to_uint8(dest[index+7] + value);
  1480. }
  1481. }
  1482. void idct(int *block) {
  1483. // See http://vsr.informatik.tu-chemnitz.de/~jan/MPEG/HTML/IDCT.html
  1484. // for more info.
  1485. int
  1486. b1, b3, b4, b6, b7, tmp1, tmp2, m0,
  1487. x0, x1, x2, x3, x4, y3, y4, y5, y6, y7;
  1488. // Transform columns
  1489. for (int i = 0; i < 8; ++i) {
  1490. b1 = block[4*8+i];
  1491. b3 = block[2*8+i] + block[6*8+i];
  1492. b4 = block[5*8+i] - block[3*8+i];
  1493. tmp1 = block[1*8+i] + block[7*8+i];
  1494. tmp2 = block[3*8+i] + block[5*8+i];
  1495. b6 = block[1*8+i] - block[7*8+i];
  1496. b7 = tmp1 + tmp2;
  1497. m0 = block[0*8+i];
  1498. x4 = ((b6*473 - b4*196 + 128) >> 8) - b7;
  1499. x0 = x4 - (((tmp1 - tmp2)*362 + 128) >> 8);
  1500. x1 = m0 - b1;
  1501. x2 = (((block[2*8+i] - block[6*8+i])*362 + 128) >> 8) - b3;
  1502. x3 = m0 + b1;
  1503. y3 = x1 + x2;
  1504. y4 = x3 + b3;
  1505. y5 = x1 - x2;
  1506. y6 = x3 - b3;
  1507. y7 = -x0 - ((b4*473 + b6*196 + 128) >> 8);
  1508. block[0*8+i] = b7 + y4;
  1509. block[1*8+i] = x4 + y3;
  1510. block[2*8+i] = y5 - x0;
  1511. block[3*8+i] = y6 - y7;
  1512. block[4*8+i] = y6 + y7;
  1513. block[5*8+i] = x0 + y5;
  1514. block[6*8+i] = y3 - x4;
  1515. block[7*8+i] = y4 - b7;
  1516. }
  1517. // Transform rows
  1518. for (int i = 0; i < 64; i += 8) {
  1519. b1 = block[4+i];
  1520. b3 = block[2+i] + block[6+i];
  1521. b4 = block[5+i] - block[3+i];
  1522. tmp1 = block[1+i] + block[7+i];
  1523. tmp2 = block[3+i] + block[5+i];
  1524. b6 = block[1+i] - block[7+i];
  1525. b7 = tmp1 + tmp2;
  1526. m0 = block[0+i];
  1527. x4 = ((b6*473 - b4*196 + 128) >> 8) - b7;
  1528. x0 = x4 - (((tmp1 - tmp2)*362 + 128) >> 8);
  1529. x1 = m0 - b1;
  1530. x2 = (((block[2+i] - block[6+i])*362 + 128) >> 8) - b3;
  1531. x3 = m0 + b1;
  1532. y3 = x1 + x2;
  1533. y4 = x3 + b3;
  1534. y5 = x1 - x2;
  1535. y6 = x3 - b3;
  1536. y7 = -x0 - ((b4*473 + b6*196 + 128) >> 8);
  1537. block[0+i] = (b7 + y4 + 128) >> 8;
  1538. block[1+i] = (x4 + y3 + 128) >> 8;
  1539. block[2+i] = (y5 - x0 + 128) >> 8;
  1540. block[3+i] = (y6 - y7 + 128) >> 8;
  1541. block[4+i] = (y6 + y7 + 128) >> 8;
  1542. block[5+i] = (x0 + y5 + 128) >> 8;
  1543. block[6+i] = (y3 - x4 + 128) >> 8;
  1544. block[7+i] = (y4 - b7 + 128) >> 8;
  1545. }
  1546. }
  1547. int read_huffman(bit_buffer_t *bits, const int *code_table) {
  1548. int state = 0;
  1549. do {
  1550. state = code_table[state + bit_buffer_read(bits, 1)];
  1551. } while (state >= 0 && code_table[state] != 0);
  1552. return code_table[state+2];
  1553. }