mjpeg.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677
  1. /*
  2. * MJPEG encoder and decoder
  3. * Copyright (c) 2000, 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * Support for external huffman table, various fixes (AVID workaround),
  20. * aspecting, new decode_frame mechanism and apple mjpeg-b support
  21. * by Alex Beregszaszi <alex@naxine.org>
  22. */
  23. //#define DEBUG
  24. #include "avcodec.h"
  25. #include "dsputil.h"
  26. #include "mpegvideo.h"
  27. /* use two quantizer tables (one for luminance and one for chrominance) */
  28. /* not yet working */
  29. #undef TWOMATRIXES
  30. typedef struct MJpegContext {
  31. UINT8 huff_size_dc_luminance[12];
  32. UINT16 huff_code_dc_luminance[12];
  33. UINT8 huff_size_dc_chrominance[12];
  34. UINT16 huff_code_dc_chrominance[12];
  35. UINT8 huff_size_ac_luminance[256];
  36. UINT16 huff_code_ac_luminance[256];
  37. UINT8 huff_size_ac_chrominance[256];
  38. UINT16 huff_code_ac_chrominance[256];
  39. } MJpegContext;
  40. /* JPEG marker codes */
  41. typedef enum {
  42. /* start of frame */
  43. SOF0 = 0xc0, /* baseline */
  44. SOF1 = 0xc1, /* extended sequential, huffman */
  45. SOF2 = 0xc2, /* progressive, huffman */
  46. SOF3 = 0xc3, /* lossless, huffman */
  47. SOF5 = 0xc5, /* differential sequential, huffman */
  48. SOF6 = 0xc6, /* differential progressive, huffman */
  49. SOF7 = 0xc7, /* differential lossless, huffman */
  50. JPG = 0xc8, /* reserved for JPEG extension */
  51. SOF9 = 0xc9, /* extended sequential, arithmetic */
  52. SOF10 = 0xca, /* progressive, arithmetic */
  53. SOF11 = 0xcb, /* lossless, arithmetic */
  54. SOF13 = 0xcd, /* differential sequential, arithmetic */
  55. SOF14 = 0xce, /* differential progressive, arithmetic */
  56. SOF15 = 0xcf, /* differential lossless, arithmetic */
  57. DHT = 0xc4, /* define huffman tables */
  58. DAC = 0xcc, /* define arithmetic-coding conditioning */
  59. /* restart with modulo 8 count "m" */
  60. RST0 = 0xd0,
  61. RST1 = 0xd1,
  62. RST2 = 0xd2,
  63. RST3 = 0xd3,
  64. RST4 = 0xd4,
  65. RST5 = 0xd5,
  66. RST6 = 0xd6,
  67. RST7 = 0xd7,
  68. SOI = 0xd8, /* start of image */
  69. EOI = 0xd9, /* end of image */
  70. SOS = 0xda, /* start of scan */
  71. DQT = 0xdb, /* define quantization tables */
  72. DNL = 0xdc, /* define number of lines */
  73. DRI = 0xdd, /* define restart interval */
  74. DHP = 0xde, /* define hierarchical progression */
  75. EXP = 0xdf, /* expand reference components */
  76. APP0 = 0xe0,
  77. APP1 = 0xe1,
  78. APP2 = 0xe2,
  79. APP3 = 0xe3,
  80. APP4 = 0xe4,
  81. APP5 = 0xe5,
  82. APP6 = 0xe6,
  83. APP7 = 0xe7,
  84. APP8 = 0xe8,
  85. APP9 = 0xe9,
  86. APP10 = 0xea,
  87. APP11 = 0xeb,
  88. APP12 = 0xec,
  89. APP13 = 0xed,
  90. APP14 = 0xee,
  91. APP15 = 0xef,
  92. JPG0 = 0xf0,
  93. JPG1 = 0xf1,
  94. JPG2 = 0xf2,
  95. JPG3 = 0xf3,
  96. JPG4 = 0xf4,
  97. JPG5 = 0xf5,
  98. JPG6 = 0xf6,
  99. JPG7 = 0xf7,
  100. JPG8 = 0xf8,
  101. JPG9 = 0xf9,
  102. JPG10 = 0xfa,
  103. JPG11 = 0xfb,
  104. JPG12 = 0xfc,
  105. JPG13 = 0xfd,
  106. COM = 0xfe, /* comment */
  107. TEM = 0x01, /* temporary private use for arithmetic coding */
  108. /* 0x02 -> 0xbf reserved */
  109. } JPEG_MARKER;
  110. #if 0
  111. /* These are the sample quantization tables given in JPEG spec section K.1.
  112. * The spec says that the values given produce "good" quality, and
  113. * when divided by 2, "very good" quality.
  114. */
  115. static const unsigned char std_luminance_quant_tbl[64] = {
  116. 16, 11, 10, 16, 24, 40, 51, 61,
  117. 12, 12, 14, 19, 26, 58, 60, 55,
  118. 14, 13, 16, 24, 40, 57, 69, 56,
  119. 14, 17, 22, 29, 51, 87, 80, 62,
  120. 18, 22, 37, 56, 68, 109, 103, 77,
  121. 24, 35, 55, 64, 81, 104, 113, 92,
  122. 49, 64, 78, 87, 103, 121, 120, 101,
  123. 72, 92, 95, 98, 112, 100, 103, 99
  124. };
  125. static const unsigned char std_chrominance_quant_tbl[64] = {
  126. 17, 18, 24, 47, 99, 99, 99, 99,
  127. 18, 21, 26, 66, 99, 99, 99, 99,
  128. 24, 26, 56, 99, 99, 99, 99, 99,
  129. 47, 66, 99, 99, 99, 99, 99, 99,
  130. 99, 99, 99, 99, 99, 99, 99, 99,
  131. 99, 99, 99, 99, 99, 99, 99, 99,
  132. 99, 99, 99, 99, 99, 99, 99, 99,
  133. 99, 99, 99, 99, 99, 99, 99, 99
  134. };
  135. #endif
  136. /* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
  137. /* IMPORTANT: these are only valid for 8-bit data precision! */
  138. static const UINT8 bits_dc_luminance[17] =
  139. { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
  140. static const UINT8 val_dc_luminance[] =
  141. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  142. static const UINT8 bits_dc_chrominance[17] =
  143. { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
  144. static const UINT8 val_dc_chrominance[] =
  145. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  146. static const UINT8 bits_ac_luminance[17] =
  147. { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
  148. static const UINT8 val_ac_luminance[] =
  149. { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
  150. 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
  151. 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
  152. 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
  153. 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
  154. 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
  155. 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  156. 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  157. 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  158. 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  159. 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  160. 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  161. 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  162. 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  163. 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  164. 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
  165. 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
  166. 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
  167. 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
  168. 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  169. 0xf9, 0xfa
  170. };
  171. static const UINT8 bits_ac_chrominance[17] =
  172. { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
  173. static const UINT8 val_ac_chrominance[] =
  174. { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
  175. 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  176. 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
  177. 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
  178. 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
  179. 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
  180. 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
  181. 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
  182. 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
  183. 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  184. 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  185. 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  186. 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
  187. 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
  188. 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
  189. 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
  190. 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
  191. 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
  192. 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
  193. 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  194. 0xf9, 0xfa
  195. };
  196. /* isn't this function nicer than the one in the libjpeg ? */
  197. static void build_huffman_codes(UINT8 *huff_size, UINT16 *huff_code,
  198. const UINT8 *bits_table, const UINT8 *val_table)
  199. {
  200. int i, j, k,nb, code, sym;
  201. code = 0;
  202. k = 0;
  203. for(i=1;i<=16;i++) {
  204. nb = bits_table[i];
  205. for(j=0;j<nb;j++) {
  206. sym = val_table[k++];
  207. huff_size[sym] = i;
  208. huff_code[sym] = code;
  209. code++;
  210. }
  211. code <<= 1;
  212. }
  213. }
  214. int mjpeg_init(MpegEncContext *s)
  215. {
  216. MJpegContext *m;
  217. m = av_malloc(sizeof(MJpegContext));
  218. if (!m)
  219. return -1;
  220. s->min_qcoeff=-1023;
  221. s->max_qcoeff= 1023;
  222. s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x
  223. /* build all the huffman tables */
  224. build_huffman_codes(m->huff_size_dc_luminance,
  225. m->huff_code_dc_luminance,
  226. bits_dc_luminance,
  227. val_dc_luminance);
  228. build_huffman_codes(m->huff_size_dc_chrominance,
  229. m->huff_code_dc_chrominance,
  230. bits_dc_chrominance,
  231. val_dc_chrominance);
  232. build_huffman_codes(m->huff_size_ac_luminance,
  233. m->huff_code_ac_luminance,
  234. bits_ac_luminance,
  235. val_ac_luminance);
  236. build_huffman_codes(m->huff_size_ac_chrominance,
  237. m->huff_code_ac_chrominance,
  238. bits_ac_chrominance,
  239. val_ac_chrominance);
  240. s->mjpeg_ctx = m;
  241. return 0;
  242. }
  243. void mjpeg_close(MpegEncContext *s)
  244. {
  245. av_free(s->mjpeg_ctx);
  246. }
  247. static inline void put_marker(PutBitContext *p, int code)
  248. {
  249. put_bits(p, 8, 0xff);
  250. put_bits(p, 8, code);
  251. }
  252. /* table_class: 0 = DC coef, 1 = AC coefs */
  253. static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
  254. const UINT8 *bits_table, const UINT8 *value_table)
  255. {
  256. PutBitContext *p = &s->pb;
  257. int n, i;
  258. put_bits(p, 4, table_class);
  259. put_bits(p, 4, table_id);
  260. n = 0;
  261. for(i=1;i<=16;i++) {
  262. n += bits_table[i];
  263. put_bits(p, 8, bits_table[i]);
  264. }
  265. for(i=0;i<n;i++)
  266. put_bits(p, 8, value_table[i]);
  267. return n + 17;
  268. }
  269. static void jpeg_table_header(MpegEncContext *s)
  270. {
  271. PutBitContext *p = &s->pb;
  272. int i, j, size;
  273. UINT8 *ptr;
  274. /* quant matrixes */
  275. put_marker(p, DQT);
  276. #ifdef TWOMATRIXES
  277. put_bits(p, 16, 2 + 2 * (1 + 64));
  278. #else
  279. put_bits(p, 16, 2 + 1 * (1 + 64));
  280. #endif
  281. put_bits(p, 4, 0); /* 8 bit precision */
  282. put_bits(p, 4, 0); /* table 0 */
  283. for(i=0;i<64;i++) {
  284. j = s->intra_scantable.permutated[i];
  285. put_bits(p, 8, s->intra_matrix[j]);
  286. }
  287. #ifdef TWOMATRIXES
  288. put_bits(p, 4, 0); /* 8 bit precision */
  289. put_bits(p, 4, 1); /* table 1 */
  290. for(i=0;i<64;i++) {
  291. j = s->intra_scantable.permutated[i];
  292. put_bits(p, 8, s->chroma_intra_matrix[j]);
  293. }
  294. #endif
  295. /* huffman table */
  296. put_marker(p, DHT);
  297. flush_put_bits(p);
  298. ptr = pbBufPtr(p);
  299. put_bits(p, 16, 0); /* patched later */
  300. size = 2;
  301. size += put_huffman_table(s, 0, 0, bits_dc_luminance, val_dc_luminance);
  302. size += put_huffman_table(s, 0, 1, bits_dc_chrominance, val_dc_chrominance);
  303. size += put_huffman_table(s, 1, 0, bits_ac_luminance, val_ac_luminance);
  304. size += put_huffman_table(s, 1, 1, bits_ac_chrominance, val_ac_chrominance);
  305. ptr[0] = size >> 8;
  306. ptr[1] = size;
  307. }
  308. static void jpeg_put_comments(MpegEncContext *s)
  309. {
  310. PutBitContext *p = &s->pb;
  311. int size;
  312. UINT8 *ptr;
  313. if (s->aspect_ratio_info)
  314. {
  315. /* JFIF header */
  316. put_marker(p, APP0);
  317. put_bits(p, 16, 16);
  318. put_string(p, "JFIF"); /* this puts the trailing zero-byte too */
  319. put_bits(p, 16, 0x0201); /* v 1.02 */
  320. put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
  321. switch(s->aspect_ratio_info)
  322. {
  323. case FF_ASPECT_4_3_625:
  324. case FF_ASPECT_4_3_525:
  325. put_bits(p, 16, 4);
  326. put_bits(p, 16, 3);
  327. break;
  328. case FF_ASPECT_16_9_625:
  329. case FF_ASPECT_16_9_525:
  330. put_bits(p, 16, 16);
  331. put_bits(p, 16, 9);
  332. break;
  333. case FF_ASPECT_EXTENDED:
  334. put_bits(p, 16, s->aspected_width);
  335. put_bits(p, 16, s->aspected_height);
  336. break;
  337. case FF_ASPECT_SQUARE:
  338. default:
  339. put_bits(p, 16, 1); /* aspect: 1:1 */
  340. put_bits(p, 16, 1);
  341. break;
  342. }
  343. put_bits(p, 8, 0); /* thumbnail width */
  344. put_bits(p, 8, 0); /* thumbnail height */
  345. }
  346. /* comment */
  347. if(!ff_bit_exact){
  348. put_marker(p, COM);
  349. flush_put_bits(p);
  350. ptr = pbBufPtr(p);
  351. put_bits(p, 16, 0); /* patched later */
  352. #define MJPEG_VERSION "FFmpeg" LIBAVCODEC_VERSION "b" LIBAVCODEC_BUILD_STR
  353. put_string(p, MJPEG_VERSION);
  354. size = strlen(MJPEG_VERSION)+3;
  355. #undef MJPEG_VERSION
  356. ptr[0] = size >> 8;
  357. ptr[1] = size;
  358. }
  359. }
  360. void mjpeg_picture_header(MpegEncContext *s)
  361. {
  362. put_marker(&s->pb, SOI);
  363. if (!s->mjpeg_data_only_frames)
  364. {
  365. jpeg_put_comments(s);
  366. if (s->mjpeg_write_tables) jpeg_table_header(s);
  367. put_marker(&s->pb, SOF0);
  368. put_bits(&s->pb, 16, 17);
  369. put_bits(&s->pb, 8, 8); /* 8 bits/component */
  370. put_bits(&s->pb, 16, s->height);
  371. put_bits(&s->pb, 16, s->width);
  372. put_bits(&s->pb, 8, 3); /* 3 components */
  373. /* Y component */
  374. put_bits(&s->pb, 8, 1); /* component number */
  375. put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */
  376. put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */
  377. put_bits(&s->pb, 8, 0); /* select matrix */
  378. /* Cb component */
  379. put_bits(&s->pb, 8, 2); /* component number */
  380. put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */
  381. put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */
  382. #ifdef TWOMATRIXES
  383. put_bits(&s->pb, 8, 1); /* select matrix */
  384. #else
  385. put_bits(&s->pb, 8, 0); /* select matrix */
  386. #endif
  387. /* Cr component */
  388. put_bits(&s->pb, 8, 3); /* component number */
  389. put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */
  390. put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */
  391. #ifdef TWOMATRIXES
  392. put_bits(&s->pb, 8, 1); /* select matrix */
  393. #else
  394. put_bits(&s->pb, 8, 0); /* select matrix */
  395. #endif
  396. }
  397. /* scan header */
  398. put_marker(&s->pb, SOS);
  399. put_bits(&s->pb, 16, 12); /* length */
  400. put_bits(&s->pb, 8, 3); /* 3 components */
  401. /* Y component */
  402. put_bits(&s->pb, 8, 1); /* index */
  403. put_bits(&s->pb, 4, 0); /* DC huffman table index */
  404. put_bits(&s->pb, 4, 0); /* AC huffman table index */
  405. /* Cb component */
  406. put_bits(&s->pb, 8, 2); /* index */
  407. put_bits(&s->pb, 4, 1); /* DC huffman table index */
  408. put_bits(&s->pb, 4, 1); /* AC huffman table index */
  409. /* Cr component */
  410. put_bits(&s->pb, 8, 3); /* index */
  411. put_bits(&s->pb, 4, 1); /* DC huffman table index */
  412. put_bits(&s->pb, 4, 1); /* AC huffman table index */
  413. put_bits(&s->pb, 8, 0); /* Ss (not used) */
  414. put_bits(&s->pb, 8, 63); /* Se (not used) */
  415. put_bits(&s->pb, 8, 0); /* Ah/Al (not used) */
  416. }
  417. static void escape_FF(MpegEncContext *s, int start)
  418. {
  419. int size= get_bit_count(&s->pb) - start*8;
  420. int i, ff_count;
  421. uint8_t *buf= s->pb.buf + start;
  422. int align= (-(int)(buf))&3;
  423. assert((size&7) == 0);
  424. size >>= 3;
  425. ff_count=0;
  426. for(i=0; i<size && i<align; i++){
  427. if(buf[i]==0xFF) ff_count++;
  428. }
  429. for(; i<size-15; i+=16){
  430. int acc, v;
  431. v= *(uint32_t*)(&buf[i]);
  432. acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  433. v= *(uint32_t*)(&buf[i+4]);
  434. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  435. v= *(uint32_t*)(&buf[i+8]);
  436. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  437. v= *(uint32_t*)(&buf[i+12]);
  438. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  439. acc>>=4;
  440. acc+= (acc>>16);
  441. acc+= (acc>>8);
  442. ff_count+= acc&0xFF;
  443. }
  444. for(; i<size; i++){
  445. if(buf[i]==0xFF) ff_count++;
  446. }
  447. if(ff_count==0) return;
  448. /* skip put bits */
  449. for(i=0; i<ff_count-3; i+=4)
  450. put_bits(&s->pb, 32, 0);
  451. put_bits(&s->pb, (ff_count-i)*8, 0);
  452. flush_put_bits(&s->pb);
  453. for(i=size-1; ff_count; i--){
  454. int v= buf[i];
  455. if(v==0xFF){
  456. //printf("%d %d\n", i, ff_count);
  457. buf[i+ff_count]= 0;
  458. ff_count--;
  459. }
  460. buf[i+ff_count]= v;
  461. }
  462. }
  463. void mjpeg_picture_trailer(MpegEncContext *s)
  464. {
  465. int pad= (-get_bit_count(&s->pb))&7;
  466. put_bits(&s->pb, pad,0xFF>>(8-pad));
  467. flush_put_bits(&s->pb);
  468. assert((s->header_bits&7)==0);
  469. escape_FF(s, s->header_bits>>3);
  470. put_marker(&s->pb, EOI);
  471. }
  472. static inline void mjpeg_encode_dc(MpegEncContext *s, int val,
  473. UINT8 *huff_size, UINT16 *huff_code)
  474. {
  475. int mant, nbits;
  476. if (val == 0) {
  477. put_bits(&s->pb, huff_size[0], huff_code[0]);
  478. } else {
  479. mant = val;
  480. if (val < 0) {
  481. val = -val;
  482. mant--;
  483. }
  484. /* compute the log (XXX: optimize) */
  485. nbits = 0;
  486. while (val != 0) {
  487. val = val >> 1;
  488. nbits++;
  489. }
  490. put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
  491. put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
  492. }
  493. }
  494. static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
  495. {
  496. int mant, nbits, code, i, j;
  497. int component, dc, run, last_index, val;
  498. MJpegContext *m = s->mjpeg_ctx;
  499. UINT8 *huff_size_ac;
  500. UINT16 *huff_code_ac;
  501. /* DC coef */
  502. component = (n <= 3 ? 0 : n - 4 + 1);
  503. dc = block[0]; /* overflow is impossible */
  504. val = dc - s->last_dc[component];
  505. if (n < 4) {
  506. mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
  507. huff_size_ac = m->huff_size_ac_luminance;
  508. huff_code_ac = m->huff_code_ac_luminance;
  509. } else {
  510. mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  511. huff_size_ac = m->huff_size_ac_chrominance;
  512. huff_code_ac = m->huff_code_ac_chrominance;
  513. }
  514. s->last_dc[component] = dc;
  515. /* AC coefs */
  516. run = 0;
  517. last_index = s->block_last_index[n];
  518. for(i=1;i<=last_index;i++) {
  519. j = s->intra_scantable.permutated[i];
  520. val = block[j];
  521. if (val == 0) {
  522. run++;
  523. } else {
  524. while (run >= 16) {
  525. put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
  526. run -= 16;
  527. }
  528. mant = val;
  529. if (val < 0) {
  530. val = -val;
  531. mant--;
  532. }
  533. /* compute the log (XXX: optimize) */
  534. nbits = 0;
  535. while (val != 0) {
  536. val = val >> 1;
  537. nbits++;
  538. }
  539. code = (run << 4) | nbits;
  540. put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
  541. put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));
  542. run = 0;
  543. }
  544. }
  545. /* output EOB only if not already 64 values */
  546. if (last_index < 63 || run != 0)
  547. put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
  548. }
  549. void mjpeg_encode_mb(MpegEncContext *s,
  550. DCTELEM block[6][64])
  551. {
  552. int i;
  553. for(i=0;i<6;i++) {
  554. encode_block(s, block[i], i);
  555. }
  556. }
  557. /******************************************/
  558. /* decoding */
  559. #define MAX_COMPONENTS 4
  560. typedef struct MJpegDecodeContext {
  561. AVCodecContext *avctx;
  562. GetBitContext gb;
  563. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  564. int start_code; /* current start code */
  565. int buffer_size;
  566. UINT8 *buffer;
  567. INT16 quant_matrixes[4][64];
  568. VLC vlcs[2][4];
  569. int org_width, org_height; /* size given at codec init */
  570. int first_picture; /* true if decoding first picture */
  571. int interlaced; /* true if interlaced */
  572. int bottom_field; /* true if bottom field */
  573. int width, height;
  574. int nb_components;
  575. int component_id[MAX_COMPONENTS];
  576. int h_count[MAX_COMPONENTS]; /* horizontal and vertical count for each component */
  577. int v_count[MAX_COMPONENTS];
  578. int h_max, v_max; /* maximum h and v counts */
  579. int quant_index[4]; /* quant table index for each component */
  580. int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */
  581. UINT8 *current_picture[MAX_COMPONENTS]; /* picture structure */
  582. int linesize[MAX_COMPONENTS];
  583. DCTELEM block[64] __align8;
  584. ScanTable scantable;
  585. void (*idct_put)(UINT8 *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);
  586. int restart_interval;
  587. int restart_count;
  588. int buggy_avid;
  589. int interlace_polarity;
  590. } MJpegDecodeContext;
  591. static int mjpeg_decode_dht(MJpegDecodeContext *s);
  592. static void build_vlc(VLC *vlc, const UINT8 *bits_table, const UINT8 *val_table,
  593. int nb_codes)
  594. {
  595. UINT8 huff_size[256];
  596. UINT16 huff_code[256];
  597. memset(huff_size, 0, sizeof(huff_size));
  598. build_huffman_codes(huff_size, huff_code, bits_table, val_table);
  599. init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2);
  600. }
  601. static int mjpeg_decode_init(AVCodecContext *avctx)
  602. {
  603. MJpegDecodeContext *s = avctx->priv_data;
  604. MpegEncContext s2;
  605. s->avctx = avctx;
  606. /* ugly way to get the idct & scantable */
  607. memset(&s2, 0, sizeof(MpegEncContext));
  608. s2.flags= avctx->flags;
  609. s2.avctx= avctx;
  610. // s2->out_format = FMT_MJPEG;
  611. s2.width = 8;
  612. s2.height = 8;
  613. if (MPV_common_init(&s2) < 0)
  614. return -1;
  615. s->scantable= s2.intra_scantable;
  616. s->idct_put= s2.idct_put;
  617. MPV_common_end(&s2);
  618. s->mpeg_enc_ctx_allocated = 0;
  619. s->buffer_size = 102400; /* smaller buffer should be enough,
  620. but photojpg files could ahive bigger sizes */
  621. s->buffer = av_malloc(s->buffer_size);
  622. s->start_code = -1;
  623. s->first_picture = 1;
  624. s->org_width = avctx->width;
  625. s->org_height = avctx->height;
  626. build_vlc(&s->vlcs[0][0], bits_dc_luminance, val_dc_luminance, 12);
  627. build_vlc(&s->vlcs[0][1], bits_dc_chrominance, val_dc_chrominance, 12);
  628. build_vlc(&s->vlcs[1][0], bits_ac_luminance, val_ac_luminance, 251);
  629. build_vlc(&s->vlcs[1][1], bits_ac_chrominance, val_ac_chrominance, 251);
  630. if (avctx->flags & CODEC_FLAG_EXTERN_HUFF)
  631. {
  632. printf("mjpeg: using external huffman table\n");
  633. init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size);
  634. mjpeg_decode_dht(s);
  635. /* should check for error - but dunno */
  636. }
  637. return 0;
  638. }
  639. /* quantize tables */
  640. static int mjpeg_decode_dqt(MJpegDecodeContext *s)
  641. {
  642. int len, index, i, j;
  643. len = get_bits(&s->gb, 16) - 2;
  644. while (len >= 65) {
  645. /* only 8 bit precision handled */
  646. if (get_bits(&s->gb, 4) != 0)
  647. {
  648. dprintf("dqt: 16bit precision\n");
  649. return -1;
  650. }
  651. index = get_bits(&s->gb, 4);
  652. if (index >= 4)
  653. return -1;
  654. dprintf("index=%d\n", index);
  655. /* read quant table */
  656. for(i=0;i<64;i++) {
  657. j = s->scantable.permutated[i];
  658. s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
  659. }
  660. len -= 65;
  661. }
  662. return 0;
  663. }
  664. /* decode huffman tables and build VLC decoders */
  665. static int mjpeg_decode_dht(MJpegDecodeContext *s)
  666. {
  667. int len, index, i, class, n, v, code_max;
  668. UINT8 bits_table[17];
  669. UINT8 val_table[256];
  670. len = get_bits(&s->gb, 16) - 2;
  671. while (len > 0) {
  672. if (len < 17)
  673. return -1;
  674. class = get_bits(&s->gb, 4);
  675. if (class >= 2)
  676. return -1;
  677. index = get_bits(&s->gb, 4);
  678. if (index >= 4)
  679. return -1;
  680. n = 0;
  681. for(i=1;i<=16;i++) {
  682. bits_table[i] = get_bits(&s->gb, 8);
  683. n += bits_table[i];
  684. }
  685. len -= 17;
  686. if (len < n || n > 256)
  687. return -1;
  688. code_max = 0;
  689. for(i=0;i<n;i++) {
  690. v = get_bits(&s->gb, 8);
  691. if (v > code_max)
  692. code_max = v;
  693. val_table[i] = v;
  694. }
  695. len -= n;
  696. /* build VLC and flush previous vlc if present */
  697. free_vlc(&s->vlcs[class][index]);
  698. dprintf("class=%d index=%d nb_codes=%d\n",
  699. class, index, code_max + 1);
  700. build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1);
  701. }
  702. return 0;
  703. }
  704. static int mjpeg_decode_sof0(MJpegDecodeContext *s)
  705. {
  706. int len, nb_components, i, width, height;
  707. /* XXX: verify len field validity */
  708. len = get_bits(&s->gb, 16);
  709. /* only 8 bits/component accepted */
  710. if (get_bits(&s->gb, 8) != 8)
  711. return -1;
  712. height = get_bits(&s->gb, 16);
  713. width = get_bits(&s->gb, 16);
  714. dprintf("sof0: picture: %dx%d\n", width, height);
  715. nb_components = get_bits(&s->gb, 8);
  716. if (nb_components <= 0 ||
  717. nb_components > MAX_COMPONENTS)
  718. return -1;
  719. s->nb_components = nb_components;
  720. s->h_max = 1;
  721. s->v_max = 1;
  722. for(i=0;i<nb_components;i++) {
  723. /* component id */
  724. s->component_id[i] = get_bits(&s->gb, 8) - 1;
  725. s->h_count[i] = get_bits(&s->gb, 4);
  726. s->v_count[i] = get_bits(&s->gb, 4);
  727. /* compute hmax and vmax (only used in interleaved case) */
  728. if (s->h_count[i] > s->h_max)
  729. s->h_max = s->h_count[i];
  730. if (s->v_count[i] > s->v_max)
  731. s->v_max = s->v_count[i];
  732. s->quant_index[i] = get_bits(&s->gb, 8);
  733. if (s->quant_index[i] >= 4)
  734. return -1;
  735. dprintf("component %d %d:%d id: %d quant:%d\n", i, s->h_count[i],
  736. s->v_count[i], s->component_id[i], s->quant_index[i]);
  737. }
  738. /* if different size, realloc/alloc picture */
  739. /* XXX: also check h_count and v_count */
  740. if (width != s->width || height != s->height) {
  741. for(i=0;i<MAX_COMPONENTS;i++)
  742. av_freep(&s->current_picture[i]);
  743. s->width = width;
  744. s->height = height;
  745. /* test interlaced mode */
  746. if (s->first_picture &&
  747. s->org_height != 0 &&
  748. s->height < ((s->org_height * 3) / 4)) {
  749. s->interlaced = 1;
  750. // s->bottom_field = (s->interlace_polarity) ? 1 : 0;
  751. s->bottom_field = 0;
  752. }
  753. for(i=0;i<nb_components;i++) {
  754. int w, h;
  755. w = (s->width + 8 * s->h_max - 1) / (8 * s->h_max);
  756. h = (s->height + 8 * s->v_max - 1) / (8 * s->v_max);
  757. w = w * 8 * s->h_count[i];
  758. h = h * 8 * s->v_count[i];
  759. if (s->interlaced)
  760. w *= 2;
  761. s->linesize[i] = w;
  762. /* memory test is done in mjpeg_decode_sos() */
  763. s->current_picture[i] = av_mallocz(w * h);
  764. }
  765. s->first_picture = 0;
  766. }
  767. if (len != (8+(3*nb_components)))
  768. {
  769. dprintf("decode_sof0: error, len(%d) mismatch\n", len);
  770. }
  771. return 0;
  772. }
  773. static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
  774. {
  775. int code, diff;
  776. #if 1
  777. code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
  778. #else
  779. code = get_vlc(&s->gb, &s->vlcs[0][dc_index]);
  780. #endif
  781. if (code < 0)
  782. {
  783. dprintf("mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index,
  784. &s->vlcs[0][dc_index]);
  785. return 0xffff;
  786. }
  787. if (code == 0) {
  788. diff = 0;
  789. } else {
  790. diff = get_bits(&s->gb, code);
  791. if ((diff & (1 << (code - 1))) == 0)
  792. diff = (-1 << code) | (diff + 1);
  793. }
  794. return diff;
  795. }
  796. /* decode block and dequantize */
  797. static int decode_block(MJpegDecodeContext *s, DCTELEM *block,
  798. int component, int dc_index, int ac_index, int quant_index)
  799. {
  800. int nbits, code, i, j, level;
  801. int run, val;
  802. VLC *ac_vlc;
  803. INT16 *quant_matrix;
  804. /* DC coef */
  805. val = mjpeg_decode_dc(s, dc_index);
  806. if (val == 0xffff) {
  807. dprintf("error dc\n");
  808. return -1;
  809. }
  810. quant_matrix = s->quant_matrixes[quant_index];
  811. val = val * quant_matrix[0] + s->last_dc[component];
  812. s->last_dc[component] = val;
  813. block[0] = val;
  814. /* AC coefs */
  815. ac_vlc = &s->vlcs[1][ac_index];
  816. i = 1;
  817. for(;;) {
  818. #if 1
  819. code = get_vlc2(&s->gb, s->vlcs[1][ac_index].table, 9, 2);
  820. #else
  821. code = get_vlc(&s->gb, ac_vlc);
  822. #endif
  823. if (code < 0) {
  824. dprintf("error ac\n");
  825. return -1;
  826. }
  827. /* EOB */
  828. if (code == 0)
  829. break;
  830. if (code == 0xf0) {
  831. i += 16;
  832. } else {
  833. run = code >> 4;
  834. nbits = code & 0xf;
  835. level = get_bits(&s->gb, nbits);
  836. if ((level & (1 << (nbits - 1))) == 0)
  837. level = (-1 << nbits) | (level + 1);
  838. i += run;
  839. if (i >= 64) {
  840. dprintf("error count: %d\n", i);
  841. return -1;
  842. }
  843. j = s->scantable.permutated[i];
  844. block[j] = level * quant_matrix[j];
  845. i++;
  846. if (i >= 64)
  847. break;
  848. }
  849. }
  850. return 0;
  851. }
  852. static int mjpeg_decode_sos(MJpegDecodeContext *s)
  853. {
  854. int len, nb_components, i, j, n, h, v, ret;
  855. int mb_width, mb_height, mb_x, mb_y, vmax, hmax, index, id;
  856. int comp_index[4];
  857. int dc_index[4];
  858. int ac_index[4];
  859. int nb_blocks[4];
  860. int h_count[4];
  861. int v_count[4];
  862. /* XXX: verify len field validity */
  863. len = get_bits(&s->gb, 16);
  864. nb_components = get_bits(&s->gb, 8);
  865. if (len != 6+2*nb_components)
  866. {
  867. dprintf("decode_sos: invalid len (%d)\n", len);
  868. return -1;
  869. }
  870. /* XXX: only interleaved scan accepted */
  871. if (nb_components != 3)
  872. {
  873. dprintf("decode_sos: components(%d) mismatch\n", nb_components);
  874. return -1;
  875. }
  876. vmax = 0;
  877. hmax = 0;
  878. for(i=0;i<nb_components;i++) {
  879. id = get_bits(&s->gb, 8) - 1;
  880. dprintf("component: %d\n", id);
  881. /* find component index */
  882. for(index=0;index<s->nb_components;index++)
  883. if (id == s->component_id[index])
  884. break;
  885. if (index == s->nb_components)
  886. {
  887. dprintf("decode_sos: index(%d) out of components\n", index);
  888. return -1;
  889. }
  890. comp_index[i] = index;
  891. nb_blocks[i] = s->h_count[index] * s->v_count[index];
  892. h_count[i] = s->h_count[index];
  893. v_count[i] = s->v_count[index];
  894. dc_index[i] = get_bits(&s->gb, 4);
  895. ac_index[i] = get_bits(&s->gb, 4);
  896. if (dc_index[i] < 0 || ac_index[i] < 0 ||
  897. dc_index[i] >= 4 || ac_index[i] >= 4)
  898. goto out_of_range;
  899. switch(s->start_code)
  900. {
  901. case SOF0:
  902. if (dc_index[i] > 1 || ac_index[i] > 1)
  903. goto out_of_range;
  904. break;
  905. case SOF1:
  906. case SOF2:
  907. if (dc_index[i] > 3 || ac_index[i] > 3)
  908. goto out_of_range;
  909. break;
  910. case SOF3:
  911. if (dc_index[i] > 3 || ac_index[i] != 0)
  912. goto out_of_range;
  913. break;
  914. }
  915. }
  916. skip_bits(&s->gb, 8); /* Ss */
  917. skip_bits(&s->gb, 8); /* Se */
  918. skip_bits(&s->gb, 8); /* Ah and Al (each are 4 bits) */
  919. for(i=0;i<nb_components;i++)
  920. s->last_dc[i] = 1024;
  921. if (nb_components > 1) {
  922. /* interleaved stream */
  923. mb_width = (s->width + s->h_max * 8 - 1) / (s->h_max * 8);
  924. mb_height = (s->height + s->v_max * 8 - 1) / (s->v_max * 8);
  925. } else {
  926. h = s->h_max / s->h_count[comp_index[0]];
  927. v = s->v_max / s->v_count[comp_index[0]];
  928. mb_width = (s->width + h * 8 - 1) / (h * 8);
  929. mb_height = (s->height + v * 8 - 1) / (v * 8);
  930. nb_blocks[0] = 1;
  931. h_count[0] = 1;
  932. v_count[0] = 1;
  933. }
  934. for(mb_y = 0; mb_y < mb_height; mb_y++) {
  935. for(mb_x = 0; mb_x < mb_width; mb_x++) {
  936. for(i=0;i<nb_components;i++) {
  937. UINT8 *ptr;
  938. int x, y, c;
  939. n = nb_blocks[i];
  940. c = comp_index[i];
  941. h = h_count[i];
  942. v = v_count[i];
  943. x = 0;
  944. y = 0;
  945. if (s->restart_interval && !s->restart_count)
  946. s->restart_count = s->restart_interval;
  947. for(j=0;j<n;j++) {
  948. memset(s->block, 0, sizeof(s->block));
  949. if (decode_block(s, s->block, i,
  950. dc_index[i], ac_index[i],
  951. s->quant_index[c]) < 0) {
  952. dprintf("error y=%d x=%d\n", mb_y, mb_x);
  953. ret = -1;
  954. goto the_end;
  955. }
  956. // dprintf("mb: %d %d processed\n", mb_y, mb_x);
  957. ptr = s->current_picture[c] +
  958. (s->linesize[c] * (v * mb_y + y) * 8) +
  959. (h * mb_x + x) * 8;
  960. if (s->interlaced && s->bottom_field)
  961. ptr += s->linesize[c] >> 1;
  962. s->idct_put(ptr, s->linesize[c], s->block);
  963. if (++x == h) {
  964. x = 0;
  965. y++;
  966. }
  967. }
  968. }
  969. /* (< 1350) buggy workaround for Spectralfan.mov, should be fixed */
  970. if (s->restart_interval && (s->restart_interval < 1350) &&
  971. !--s->restart_count) {
  972. align_get_bits(&s->gb);
  973. skip_bits(&s->gb, 16); /* skip RSTn */
  974. for (j=0; j<nb_components; j++) /* reset dc */
  975. s->last_dc[j] = 1024;
  976. }
  977. }
  978. }
  979. ret = 0;
  980. the_end:
  981. emms_c();
  982. return ret;
  983. out_of_range:
  984. dprintf("decode_sos: ac/dc index out of range\n");
  985. return -1;
  986. }
  987. static int mjpeg_decode_dri(MJpegDecodeContext *s)
  988. {
  989. if (get_bits(&s->gb, 16) != 4)
  990. return -1;
  991. s->restart_interval = get_bits(&s->gb, 16);
  992. dprintf("restart interval: %d\n", s->restart_interval);
  993. return 0;
  994. }
  995. static int mjpeg_decode_app(MJpegDecodeContext *s)
  996. {
  997. int len, id;
  998. /* XXX: verify len field validity */
  999. len = get_bits(&s->gb, 16);
  1000. if (len < 5)
  1001. return -1;
  1002. id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
  1003. id = be2me_32(id);
  1004. len -= 6;
  1005. /* buggy AVID, it puts EOI only at every 10th frame */
  1006. /* also this fourcc is used by non-avid files too, it holds some
  1007. informations, but it's always present in AVID creates files */
  1008. if (id == ff_get_fourcc("AVI1"))
  1009. {
  1010. /* structure:
  1011. 4bytes AVI1
  1012. 1bytes polarity
  1013. 1bytes always zero
  1014. 4bytes field_size
  1015. 4bytes field_size_less_padding
  1016. */
  1017. s->buggy_avid = 1;
  1018. // if (s->first_picture)
  1019. // printf("mjpeg: workarounding buggy AVID\n");
  1020. s->interlace_polarity = get_bits(&s->gb, 8);
  1021. #if 0
  1022. skip_bits(&s->gb, 8);
  1023. skip_bits(&s->gb, 32);
  1024. skip_bits(&s->gb, 32);
  1025. len -= 10;
  1026. #endif
  1027. // if (s->interlace_polarity)
  1028. // printf("mjpeg: interlace polarity: %d\n", s->interlace_polarity);
  1029. goto out;
  1030. }
  1031. // len -= 2;
  1032. if (id == ff_get_fourcc("JFIF"))
  1033. {
  1034. int t_w, t_h;
  1035. skip_bits(&s->gb, 8); /* the trailing zero-byte */
  1036. printf("mjpeg: JFIF header found (version: %x.%x)\n",
  1037. get_bits(&s->gb, 8), get_bits(&s->gb, 8));
  1038. if (get_bits(&s->gb, 8) == 0)
  1039. {
  1040. s->avctx->aspect_ratio_info = FF_ASPECT_EXTENDED;
  1041. s->avctx->aspected_width = get_bits(&s->gb, 16);
  1042. s->avctx->aspected_height = get_bits(&s->gb, 16);
  1043. }
  1044. else
  1045. {
  1046. skip_bits(&s->gb, 16);
  1047. skip_bits(&s->gb, 16);
  1048. }
  1049. t_w = get_bits(&s->gb, 8);
  1050. t_h = get_bits(&s->gb, 8);
  1051. if (t_w && t_h)
  1052. {
  1053. /* skip thumbnail */
  1054. if (len-10-(t_w*t_h*3) > 0)
  1055. len -= t_w*t_h*3;
  1056. }
  1057. len -= 10;
  1058. goto out;
  1059. }
  1060. if (id == ff_get_fourcc("Adob") && (get_bits(&s->gb, 8) == 'e'))
  1061. {
  1062. printf("mjpeg: Adobe header found\n");
  1063. skip_bits(&s->gb, 16); /* version */
  1064. skip_bits(&s->gb, 16); /* flags0 */
  1065. skip_bits(&s->gb, 16); /* flags1 */
  1066. skip_bits(&s->gb, 8); /* transform */
  1067. len -= 7;
  1068. goto out;
  1069. }
  1070. /* Apple MJPEG-A */
  1071. if ((s->start_code == APP1) && (len > (0x28 - 8)))
  1072. {
  1073. id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
  1074. id = be2me_32(id);
  1075. len -= 4;
  1076. if (id == ff_get_fourcc("mjpg")) /* Apple MJPEG-A */
  1077. {
  1078. #if 0
  1079. skip_bits(&s->gb, 32); /* field size */
  1080. skip_bits(&s->gb, 32); /* pad field size */
  1081. skip_bits(&s->gb, 32); /* next off */
  1082. skip_bits(&s->gb, 32); /* quant off */
  1083. skip_bits(&s->gb, 32); /* huff off */
  1084. skip_bits(&s->gb, 32); /* image off */
  1085. skip_bits(&s->gb, 32); /* scan off */
  1086. skip_bits(&s->gb, 32); /* data off */
  1087. #endif
  1088. if (s->first_picture)
  1089. printf("mjpeg: Apple MJPEG-A header found\n");
  1090. }
  1091. }
  1092. out:
  1093. /* slow but needed for extreme adobe jpegs */
  1094. if (len < 0)
  1095. printf("mjpeg: error, decode_app parser read over the end\n");
  1096. while(--len > 0)
  1097. skip_bits(&s->gb, 8);
  1098. return 0;
  1099. }
  1100. static int mjpeg_decode_com(MJpegDecodeContext *s)
  1101. {
  1102. int len, i;
  1103. UINT8 *cbuf;
  1104. /* XXX: verify len field validity */
  1105. len = get_bits(&s->gb, 16)-2;
  1106. cbuf = av_malloc(len+1);
  1107. for (i = 0; i < len; i++)
  1108. cbuf[i] = get_bits(&s->gb, 8);
  1109. if (cbuf[i-1] == '\n')
  1110. cbuf[i-1] = 0;
  1111. else
  1112. cbuf[i] = 0;
  1113. printf("mjpeg comment: '%s'\n", cbuf);
  1114. /* buggy avid, it puts EOI only at every 10th frame */
  1115. if (!strcmp(cbuf, "AVID"))
  1116. {
  1117. s->buggy_avid = 1;
  1118. // if (s->first_picture)
  1119. // printf("mjpeg: workarounding buggy AVID\n");
  1120. }
  1121. av_free(cbuf);
  1122. return 0;
  1123. }
  1124. #if 0
  1125. static int valid_marker_list[] =
  1126. {
  1127. /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f */
  1128. /* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1129. /* 1 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1130. /* 2 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1131. /* 3 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1132. /* 4 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1133. /* 5 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1134. /* 6 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1135. /* 7 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1136. /* 8 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1137. /* 9 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1138. /* a */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1139. /* b */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1140. /* c */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1141. /* d */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1142. /* e */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1143. /* f */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
  1144. }
  1145. #endif
  1146. /* return the 8 bit start code value and update the search
  1147. state. Return -1 if no start code found */
  1148. static int find_marker(UINT8 **pbuf_ptr, UINT8 *buf_end)
  1149. {
  1150. UINT8 *buf_ptr;
  1151. unsigned int v, v2;
  1152. int val;
  1153. #ifdef DEBUG
  1154. int skipped=0;
  1155. #endif
  1156. buf_ptr = *pbuf_ptr;
  1157. while (buf_ptr < buf_end) {
  1158. v = *buf_ptr++;
  1159. v2 = *buf_ptr;
  1160. if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe)) {
  1161. val = *buf_ptr++;
  1162. goto found;
  1163. }
  1164. #ifdef DEBUG
  1165. skipped++;
  1166. #endif
  1167. }
  1168. val = -1;
  1169. found:
  1170. #ifdef DEBUG
  1171. dprintf("find_marker skipped %d bytes\n", skipped);
  1172. #endif
  1173. *pbuf_ptr = buf_ptr;
  1174. return val;
  1175. }
  1176. static int mjpeg_decode_frame(AVCodecContext *avctx,
  1177. void *data, int *data_size,
  1178. UINT8 *buf, int buf_size)
  1179. {
  1180. MJpegDecodeContext *s = avctx->priv_data;
  1181. UINT8 *buf_end, *buf_ptr;
  1182. int i, start_code;
  1183. AVPicture *picture = data;
  1184. *data_size = 0;
  1185. /* no supplementary picture */
  1186. if (buf_size == 0)
  1187. return 0;
  1188. buf_ptr = buf;
  1189. buf_end = buf + buf_size;
  1190. while (buf_ptr < buf_end) {
  1191. /* find start next marker */
  1192. start_code = find_marker(&buf_ptr, buf_end);
  1193. {
  1194. /* EOF */
  1195. if (start_code < 0) {
  1196. goto the_end;
  1197. } else {
  1198. dprintf("marker=%x avail_size_in_buf=%d\n", start_code, buf_end - buf_ptr);
  1199. if ((buf_end - buf_ptr) > s->buffer_size)
  1200. {
  1201. av_free(s->buffer);
  1202. s->buffer_size = buf_end-buf_ptr;
  1203. s->buffer = av_malloc(s->buffer_size);
  1204. dprintf("buffer too small, expanding to %d bytes\n",
  1205. s->buffer_size);
  1206. }
  1207. /* unescape buffer of SOS */
  1208. if (start_code == SOS)
  1209. {
  1210. UINT8 *src = buf_ptr;
  1211. UINT8 *dst = s->buffer;
  1212. while (src<buf_end)
  1213. {
  1214. UINT8 x = *(src++);
  1215. *(dst++) = x;
  1216. if (x == 0xff)
  1217. {
  1218. while(*src == 0xff) src++;
  1219. x = *(src++);
  1220. if (x >= 0xd0 && x <= 0xd7)
  1221. *(dst++) = x;
  1222. else if (x)
  1223. break;
  1224. }
  1225. }
  1226. init_get_bits(&s->gb, s->buffer, dst - s->buffer);
  1227. dprintf("escaping removed %d bytes\n",
  1228. (buf_end - buf_ptr) - (dst - s->buffer));
  1229. }
  1230. else
  1231. init_get_bits(&s->gb, buf_ptr, buf_end - buf_ptr);
  1232. s->start_code = start_code;
  1233. /* process markers */
  1234. if (start_code >= 0xd0 && start_code <= 0xd7) {
  1235. dprintf("restart marker: %d\n", start_code&0x0f);
  1236. } else if (s->first_picture) {
  1237. /* APP fields */
  1238. if (start_code >= 0xe0 && start_code <= 0xef)
  1239. mjpeg_decode_app(s);
  1240. /* Comment */
  1241. else if (start_code == COM)
  1242. mjpeg_decode_com(s);
  1243. }
  1244. switch(start_code) {
  1245. case SOI:
  1246. s->restart_interval = 0;
  1247. /* nothing to do on SOI */
  1248. break;
  1249. case DQT:
  1250. mjpeg_decode_dqt(s);
  1251. break;
  1252. case DHT:
  1253. mjpeg_decode_dht(s);
  1254. break;
  1255. case SOF0:
  1256. mjpeg_decode_sof0(s);
  1257. break;
  1258. case EOI:
  1259. eoi_parser:
  1260. {
  1261. int l;
  1262. if (s->interlaced) {
  1263. s->bottom_field ^= 1;
  1264. /* if not bottom field, do not output image yet */
  1265. if (s->bottom_field)
  1266. goto not_the_end;
  1267. }
  1268. for(i=0;i<3;i++) {
  1269. picture->data[i] = s->current_picture[i];
  1270. #if 1
  1271. l = s->linesize[i];
  1272. if (s->interlaced)
  1273. l >>= 1;
  1274. picture->linesize[i] = l;
  1275. #else
  1276. picture->linesize[i] = (s->interlaced) ?
  1277. s->linesize[i] >> 1 : s->linesize[i];
  1278. #endif
  1279. }
  1280. *data_size = sizeof(AVPicture);
  1281. avctx->height = s->height;
  1282. if (s->interlaced)
  1283. avctx->height *= 2;
  1284. avctx->width = s->width;
  1285. /* XXX: not complete test ! */
  1286. switch((s->h_count[0] << 4) | s->v_count[0]) {
  1287. case 0x11:
  1288. avctx->pix_fmt = PIX_FMT_YUV444P;
  1289. break;
  1290. case 0x21:
  1291. avctx->pix_fmt = PIX_FMT_YUV422P;
  1292. break;
  1293. default:
  1294. case 0x22:
  1295. avctx->pix_fmt = PIX_FMT_YUV420P;
  1296. break;
  1297. }
  1298. /* dummy quality */
  1299. /* XXX: infer it with matrix */
  1300. avctx->quality = 3;
  1301. goto the_end;
  1302. }
  1303. break;
  1304. case SOS:
  1305. mjpeg_decode_sos(s);
  1306. /* buggy avid puts EOI every 10-20th frame */
  1307. /* if restart period is over process EOI */
  1308. if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
  1309. goto eoi_parser;
  1310. break;
  1311. case DRI:
  1312. mjpeg_decode_dri(s);
  1313. break;
  1314. case SOF1:
  1315. case SOF2:
  1316. case SOF3:
  1317. case SOF5:
  1318. case SOF6:
  1319. case SOF7:
  1320. case SOF9:
  1321. case SOF10:
  1322. case SOF11:
  1323. case SOF13:
  1324. case SOF14:
  1325. case SOF15:
  1326. case JPG:
  1327. printf("mjpeg: unsupported coding type (%x)\n", start_code);
  1328. break;
  1329. // default:
  1330. // printf("mjpeg: unsupported marker (%x)\n", start_code);
  1331. // break;
  1332. }
  1333. not_the_end:
  1334. /* eof process start code */
  1335. buf_ptr += (get_bits_count(&s->gb)+7)/8;
  1336. dprintf("marker parser used %d bytes (%d bits)\n",
  1337. (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb));
  1338. }
  1339. }
  1340. }
  1341. the_end:
  1342. dprintf("mjpeg decode frame unused %d bytes\n", buf_end - buf_ptr);
  1343. // return buf_end - buf_ptr;
  1344. return buf_ptr - buf;
  1345. }
  1346. static int mjpegb_decode_frame(AVCodecContext *avctx,
  1347. void *data, int *data_size,
  1348. UINT8 *buf, int buf_size)
  1349. {
  1350. MJpegDecodeContext *s = avctx->priv_data;
  1351. UINT8 *buf_end, *buf_ptr;
  1352. int i;
  1353. AVPicture *picture = data;
  1354. GetBitContext hgb; /* for the header */
  1355. uint32_t dqt_offs, dht_offs, sof_offs, sos_offs, second_field_offs;
  1356. uint32_t field_size;
  1357. *data_size = 0;
  1358. /* no supplementary picture */
  1359. if (buf_size == 0)
  1360. return 0;
  1361. buf_ptr = buf;
  1362. buf_end = buf + buf_size;
  1363. read_header:
  1364. /* reset on every SOI */
  1365. s->restart_interval = 0;
  1366. init_get_bits(&hgb, buf_ptr, /*buf_size*/buf_end - buf_ptr);
  1367. skip_bits(&hgb, 32); /* reserved zeros */
  1368. if (get_bits(&hgb, 32) != be2me_32(ff_get_fourcc("mjpg")))
  1369. {
  1370. dprintf("not mjpeg-b (bad fourcc)\n");
  1371. return 0;
  1372. }
  1373. field_size = get_bits(&hgb, 32); /* field size */
  1374. dprintf("field size: 0x%x\n", field_size);
  1375. skip_bits(&hgb, 32); /* padded field size */
  1376. second_field_offs = get_bits(&hgb, 32);
  1377. dprintf("second field offs: 0x%x\n", second_field_offs);
  1378. if (second_field_offs)
  1379. s->interlaced = 1;
  1380. dqt_offs = get_bits(&hgb, 32);
  1381. dprintf("dqt offs: 0x%x\n", dqt_offs);
  1382. if (dqt_offs)
  1383. {
  1384. init_get_bits(&s->gb, buf+dqt_offs, buf_end - (buf+dqt_offs));
  1385. s->start_code = DQT;
  1386. mjpeg_decode_dqt(s);
  1387. }
  1388. dht_offs = get_bits(&hgb, 32);
  1389. dprintf("dht offs: 0x%x\n", dht_offs);
  1390. if (dht_offs)
  1391. {
  1392. init_get_bits(&s->gb, buf+dht_offs, buf_end - (buf+dht_offs));
  1393. s->start_code = DHT;
  1394. mjpeg_decode_dht(s);
  1395. }
  1396. sof_offs = get_bits(&hgb, 32);
  1397. dprintf("sof offs: 0x%x\n", sof_offs);
  1398. if (sof_offs)
  1399. {
  1400. init_get_bits(&s->gb, buf+sof_offs, buf_end - (buf+sof_offs));
  1401. s->start_code = SOF0;
  1402. mjpeg_decode_sof0(s);
  1403. }
  1404. sos_offs = get_bits(&hgb, 32);
  1405. dprintf("sos offs: 0x%x\n", sos_offs);
  1406. if (sos_offs)
  1407. {
  1408. // init_get_bits(&s->gb, buf+sos_offs, buf_end - (buf+sos_offs));
  1409. init_get_bits(&s->gb, buf+sos_offs, field_size);
  1410. s->start_code = SOS;
  1411. mjpeg_decode_sos(s);
  1412. }
  1413. skip_bits(&hgb, 32); /* start of data offset */
  1414. if (s->interlaced) {
  1415. s->bottom_field ^= 1;
  1416. /* if not bottom field, do not output image yet */
  1417. if (s->bottom_field && second_field_offs)
  1418. {
  1419. buf_ptr = buf + second_field_offs;
  1420. second_field_offs = 0;
  1421. goto read_header;
  1422. }
  1423. }
  1424. for(i=0;i<3;i++) {
  1425. picture->data[i] = s->current_picture[i];
  1426. picture->linesize[i] = (s->interlaced) ?
  1427. s->linesize[i] >> 1 : s->linesize[i];
  1428. }
  1429. *data_size = sizeof(AVPicture);
  1430. avctx->height = s->height;
  1431. if (s->interlaced)
  1432. avctx->height *= 2;
  1433. avctx->width = s->width;
  1434. /* XXX: not complete test ! */
  1435. switch((s->h_count[0] << 4) | s->v_count[0]) {
  1436. case 0x11:
  1437. avctx->pix_fmt = PIX_FMT_YUV444P;
  1438. break;
  1439. case 0x21:
  1440. avctx->pix_fmt = PIX_FMT_YUV422P;
  1441. break;
  1442. default:
  1443. case 0x22:
  1444. avctx->pix_fmt = PIX_FMT_YUV420P;
  1445. break;
  1446. }
  1447. /* dummy quality */
  1448. /* XXX: infer it with matrix */
  1449. avctx->quality = 3;
  1450. return buf_ptr - buf;
  1451. }
  1452. static int mjpeg_decode_end(AVCodecContext *avctx)
  1453. {
  1454. MJpegDecodeContext *s = avctx->priv_data;
  1455. int i, j;
  1456. av_free(s->buffer);
  1457. for(i=0;i<MAX_COMPONENTS;i++)
  1458. av_free(s->current_picture[i]);
  1459. for(i=0;i<2;i++) {
  1460. for(j=0;j<4;j++)
  1461. free_vlc(&s->vlcs[i][j]);
  1462. }
  1463. return 0;
  1464. }
  1465. AVCodec mjpeg_decoder = {
  1466. "mjpeg",
  1467. CODEC_TYPE_VIDEO,
  1468. CODEC_ID_MJPEG,
  1469. sizeof(MJpegDecodeContext),
  1470. mjpeg_decode_init,
  1471. NULL,
  1472. mjpeg_decode_end,
  1473. mjpeg_decode_frame,
  1474. 0,
  1475. NULL
  1476. };
  1477. AVCodec mjpegb_decoder = {
  1478. "mjpegb",
  1479. CODEC_TYPE_VIDEO,
  1480. CODEC_ID_MJPEGB,
  1481. sizeof(MJpegDecodeContext),
  1482. mjpeg_decode_init,
  1483. NULL,
  1484. mjpeg_decode_end,
  1485. mjpegb_decode_frame,
  1486. 0,
  1487. NULL
  1488. };