mxpegdec.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * MxPEG decoder
  3. * Copyright (c) 2011 Anatoly Nenashev
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * MxPEG decoder
  24. */
  25. #include "mjpeg.h"
  26. #include "mjpegdec.h"
  27. typedef struct MXpegDecodeContext {
  28. MJpegDecodeContext jpg;
  29. AVFrame picture[2]; /* pictures array */
  30. int picture_index; /* index of current picture */
  31. int got_sof_data; /* true if SOF data successfully parsed */
  32. int got_mxm_bitmask; /* true if MXM bitmask available */
  33. uint8_t *mxm_bitmask; /* bitmask buffer */
  34. unsigned bitmask_size; /* size of bitmask */
  35. int has_complete_frame; /* true if has complete frame */
  36. uint8_t *completion_bitmask; /* completion bitmask of macroblocks */
  37. unsigned mb_width, mb_height; /* size of picture in MB's from MXM header */
  38. } MXpegDecodeContext;
  39. static av_cold int mxpeg_decode_init(AVCodecContext *avctx)
  40. {
  41. MXpegDecodeContext *s = avctx->priv_data;
  42. s->picture[0].reference = s->picture[1].reference = 3;
  43. s->jpg.picture_ptr = &s->picture[0];
  44. ff_mjpeg_decode_init(avctx);
  45. return 0;
  46. }
  47. static int mxpeg_decode_app(MXpegDecodeContext *s,
  48. const uint8_t *buf_ptr, int buf_size)
  49. {
  50. int len;
  51. if (buf_size < 2)
  52. return 0;
  53. len = AV_RB16(buf_ptr);
  54. skip_bits(&s->jpg.gb, 8*FFMIN(len,buf_size));
  55. return 0;
  56. }
  57. static int mxpeg_decode_mxm(MXpegDecodeContext *s,
  58. const uint8_t *buf_ptr, int buf_size)
  59. {
  60. unsigned bitmask_size, mb_count;
  61. int i;
  62. s->mb_width = AV_RL16(buf_ptr+4);
  63. s->mb_height = AV_RL16(buf_ptr+6);
  64. mb_count = s->mb_width * s->mb_height;
  65. bitmask_size = (mb_count + 7) >> 3;
  66. if (bitmask_size > buf_size - 12) {
  67. av_log(s->jpg.avctx, AV_LOG_ERROR,
  68. "MXM bitmask is not complete\n");
  69. return AVERROR(EINVAL);
  70. }
  71. if (s->bitmask_size != bitmask_size) {
  72. av_freep(&s->mxm_bitmask);
  73. s->mxm_bitmask = av_malloc(bitmask_size);
  74. if (!s->mxm_bitmask) {
  75. av_log(s->jpg.avctx, AV_LOG_ERROR,
  76. "MXM bitmask memory allocation error\n");
  77. return AVERROR(ENOMEM);
  78. }
  79. av_freep(&s->completion_bitmask);
  80. s->completion_bitmask = av_mallocz(bitmask_size);
  81. if (!s->completion_bitmask) {
  82. av_log(s->jpg.avctx, AV_LOG_ERROR,
  83. "Completion bitmask memory allocation error\n");
  84. return AVERROR(ENOMEM);
  85. }
  86. s->bitmask_size = bitmask_size;
  87. }
  88. memcpy(s->mxm_bitmask, buf_ptr + 12, bitmask_size);
  89. s->got_mxm_bitmask = 1;
  90. if (!s->has_complete_frame) {
  91. uint8_t completion_check = 0xFF;
  92. for (i = 0; i < bitmask_size; ++i) {
  93. s->completion_bitmask[i] |= s->mxm_bitmask[i];
  94. completion_check &= s->completion_bitmask[i];
  95. }
  96. s->has_complete_frame = !(completion_check ^ 0xFF);
  97. }
  98. return 0;
  99. }
  100. static int mxpeg_decode_com(MXpegDecodeContext *s,
  101. const uint8_t *buf_ptr, int buf_size)
  102. {
  103. int len, ret = 0;
  104. if (buf_size < 2)
  105. return 0;
  106. len = AV_RB16(buf_ptr);
  107. if (len > 14 && len <= buf_size && !strncmp(buf_ptr + 2, "MXM", 3)) {
  108. ret = mxpeg_decode_mxm(s, buf_ptr + 2, len - 2);
  109. }
  110. skip_bits(&s->jpg.gb, 8*FFMIN(len,buf_size));
  111. return ret;
  112. }
  113. static int mxpeg_check_dimensions(MXpegDecodeContext *s, MJpegDecodeContext *jpg,
  114. AVFrame *reference_ptr)
  115. {
  116. if ((jpg->width + 0x0F)>>4 != s->mb_width ||
  117. (jpg->height + 0x0F)>>4 != s->mb_height) {
  118. av_log(jpg->avctx, AV_LOG_ERROR,
  119. "Picture dimensions stored in SOF and MXM mismatch\n");
  120. return AVERROR(EINVAL);
  121. }
  122. if (reference_ptr->data[0]) {
  123. int i;
  124. for (i = 0; i < MAX_COMPONENTS; ++i) {
  125. if ( (!reference_ptr->data[i] ^ !jpg->picture_ptr->data[i]) ||
  126. reference_ptr->linesize[i] != jpg->picture_ptr->linesize[i]) {
  127. av_log(jpg->avctx, AV_LOG_ERROR,
  128. "Dimensions of current and reference picture mismatch\n");
  129. return AVERROR(EINVAL);
  130. }
  131. }
  132. }
  133. return 0;
  134. }
  135. static int mxpeg_decode_frame(AVCodecContext *avctx,
  136. void *data, int *data_size,
  137. AVPacket *avpkt)
  138. {
  139. const uint8_t *buf = avpkt->data;
  140. int buf_size = avpkt->size;
  141. MXpegDecodeContext *s = avctx->priv_data;
  142. MJpegDecodeContext *jpg = &s->jpg;
  143. const uint8_t *buf_end, *buf_ptr;
  144. const uint8_t *unescaped_buf_ptr;
  145. int unescaped_buf_size;
  146. int start_code;
  147. AVFrame *picture = data;
  148. int ret;
  149. buf_ptr = buf;
  150. buf_end = buf + buf_size;
  151. jpg->got_picture = 0;
  152. s->got_mxm_bitmask = 0;
  153. while (buf_ptr < buf_end) {
  154. start_code = ff_mjpeg_find_marker(jpg, &buf_ptr, buf_end,
  155. &unescaped_buf_ptr, &unescaped_buf_size);
  156. if (start_code < 0)
  157. goto the_end;
  158. {
  159. init_get_bits(&jpg->gb, unescaped_buf_ptr, unescaped_buf_size*8);
  160. if (start_code >= APP0 && start_code <= APP15) {
  161. mxpeg_decode_app(s, unescaped_buf_ptr, unescaped_buf_size);
  162. }
  163. switch (start_code) {
  164. case SOI:
  165. if (jpg->got_picture) //emulating EOI
  166. goto the_end;
  167. break;
  168. case EOI:
  169. goto the_end;
  170. case DQT:
  171. ret = ff_mjpeg_decode_dqt(jpg);
  172. if (ret < 0) {
  173. av_log(avctx, AV_LOG_ERROR,
  174. "quantization table decode error\n");
  175. return ret;
  176. }
  177. break;
  178. case DHT:
  179. ret = ff_mjpeg_decode_dht(jpg);
  180. if (ret < 0) {
  181. av_log(avctx, AV_LOG_ERROR,
  182. "huffman table decode error\n");
  183. return ret;
  184. }
  185. break;
  186. case COM:
  187. ret = mxpeg_decode_com(s, unescaped_buf_ptr,
  188. unescaped_buf_size);
  189. if (ret < 0)
  190. return ret;
  191. break;
  192. case SOF0:
  193. s->got_sof_data = 0;
  194. ret = ff_mjpeg_decode_sof(jpg);
  195. if (ret < 0) {
  196. av_log(avctx, AV_LOG_ERROR,
  197. "SOF data decode error\n");
  198. return ret;
  199. }
  200. if (jpg->interlaced) {
  201. av_log(avctx, AV_LOG_ERROR,
  202. "Interlaced mode not supported in MxPEG\n");
  203. return AVERROR(EINVAL);
  204. }
  205. s->got_sof_data = 1;
  206. break;
  207. case SOS:
  208. if (!s->got_sof_data) {
  209. av_log(avctx, AV_LOG_WARNING,
  210. "Can not process SOS without SOF data, skipping\n");
  211. break;
  212. }
  213. if (!jpg->got_picture) {
  214. if (jpg->first_picture) {
  215. av_log(avctx, AV_LOG_WARNING,
  216. "First picture has no SOF, skipping\n");
  217. break;
  218. }
  219. if (!s->got_mxm_bitmask){
  220. av_log(avctx, AV_LOG_WARNING,
  221. "Non-key frame has no MXM, skipping\n");
  222. break;
  223. }
  224. /* use stored SOF data to allocate current picture */
  225. if (jpg->picture_ptr->data[0])
  226. avctx->release_buffer(avctx, jpg->picture_ptr);
  227. if (avctx->get_buffer(avctx, jpg->picture_ptr) < 0) {
  228. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  229. return AVERROR(ENOMEM);
  230. }
  231. jpg->picture_ptr->pict_type = FF_P_TYPE;
  232. jpg->picture_ptr->key_frame = 0;
  233. jpg->got_picture = 1;
  234. } else {
  235. jpg->picture_ptr->pict_type = FF_I_TYPE;
  236. jpg->picture_ptr->key_frame = 1;
  237. }
  238. if (s->got_mxm_bitmask) {
  239. AVFrame *reference_ptr = &s->picture[s->picture_index ^ 1];
  240. if (mxpeg_check_dimensions(s, jpg, reference_ptr) < 0)
  241. break;
  242. /* allocate dummy reference picture if needed */
  243. if (!reference_ptr->data[0] &&
  244. avctx->get_buffer(avctx, reference_ptr) < 0) {
  245. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  246. return AVERROR(ENOMEM);
  247. }
  248. ff_mjpeg_decode_sos(jpg, s->mxm_bitmask, reference_ptr);
  249. } else {
  250. ff_mjpeg_decode_sos(jpg, NULL, NULL);
  251. }
  252. break;
  253. }
  254. buf_ptr += (get_bits_count(&jpg->gb)+7) >> 3;
  255. }
  256. }
  257. the_end:
  258. if (jpg->got_picture) {
  259. *data_size = sizeof(AVFrame);
  260. *picture = *jpg->picture_ptr;
  261. s->picture_index ^= 1;
  262. jpg->picture_ptr = &s->picture[s->picture_index];
  263. if (!s->has_complete_frame) {
  264. if (!s->got_mxm_bitmask)
  265. s->has_complete_frame = 1;
  266. else
  267. *data_size = 0;
  268. }
  269. }
  270. return buf_ptr - buf;
  271. }
  272. static av_cold int mxpeg_decode_end(AVCodecContext *avctx)
  273. {
  274. MXpegDecodeContext *s = avctx->priv_data;
  275. MJpegDecodeContext *jpg = &s->jpg;
  276. int i;
  277. jpg->picture_ptr = NULL;
  278. ff_mjpeg_decode_end(avctx);
  279. for (i = 0; i < 2; ++i) {
  280. if (s->picture[i].data[0])
  281. avctx->release_buffer(avctx, &s->picture[i]);
  282. }
  283. av_freep(&s->mxm_bitmask);
  284. av_freep(&s->completion_bitmask);
  285. return 0;
  286. }
  287. AVCodec ff_mxpeg_decoder = {
  288. .name = "mxpeg",
  289. .long_name = NULL_IF_CONFIG_SMALL("Mobotix MxPEG video"),
  290. .type = AVMEDIA_TYPE_VIDEO,
  291. .id = CODEC_ID_MXPEG,
  292. .priv_data_size = sizeof(MXpegDecodeContext),
  293. .init = mxpeg_decode_init,
  294. .close = mxpeg_decode_end,
  295. .decode = mxpeg_decode_frame,
  296. .capabilities = CODEC_CAP_DR1,
  297. .max_lowres = 3
  298. };