You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

342 lines
11KB

  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. return ff_mjpeg_decode_init(avctx);
  45. }
  46. static int mxpeg_decode_app(MXpegDecodeContext *s,
  47. const uint8_t *buf_ptr, int buf_size)
  48. {
  49. int len;
  50. if (buf_size < 2)
  51. return 0;
  52. len = AV_RB16(buf_ptr);
  53. skip_bits(&s->jpg.gb, 8*FFMIN(len,buf_size));
  54. return 0;
  55. }
  56. static int mxpeg_decode_mxm(MXpegDecodeContext *s,
  57. const uint8_t *buf_ptr, int buf_size)
  58. {
  59. unsigned bitmask_size, mb_count;
  60. int i;
  61. s->mb_width = AV_RL16(buf_ptr+4);
  62. s->mb_height = AV_RL16(buf_ptr+6);
  63. mb_count = s->mb_width * s->mb_height;
  64. bitmask_size = (mb_count + 7) >> 3;
  65. if (bitmask_size > buf_size - 12) {
  66. av_log(s->jpg.avctx, AV_LOG_ERROR,
  67. "MXM bitmask is not complete\n");
  68. return AVERROR(EINVAL);
  69. }
  70. if (s->bitmask_size != bitmask_size) {
  71. av_freep(&s->mxm_bitmask);
  72. s->mxm_bitmask = av_malloc(bitmask_size);
  73. if (!s->mxm_bitmask) {
  74. av_log(s->jpg.avctx, AV_LOG_ERROR,
  75. "MXM bitmask memory allocation error\n");
  76. return AVERROR(ENOMEM);
  77. }
  78. av_freep(&s->completion_bitmask);
  79. s->completion_bitmask = av_mallocz(bitmask_size);
  80. if (!s->completion_bitmask) {
  81. av_log(s->jpg.avctx, AV_LOG_ERROR,
  82. "Completion bitmask memory allocation error\n");
  83. return AVERROR(ENOMEM);
  84. }
  85. s->bitmask_size = bitmask_size;
  86. }
  87. memcpy(s->mxm_bitmask, buf_ptr + 12, bitmask_size);
  88. s->got_mxm_bitmask = 1;
  89. if (!s->has_complete_frame) {
  90. uint8_t completion_check = 0xFF;
  91. for (i = 0; i < bitmask_size; ++i) {
  92. s->completion_bitmask[i] |= s->mxm_bitmask[i];
  93. completion_check &= s->completion_bitmask[i];
  94. }
  95. s->has_complete_frame = !(completion_check ^ 0xFF);
  96. }
  97. return 0;
  98. }
  99. static int mxpeg_decode_com(MXpegDecodeContext *s,
  100. const uint8_t *buf_ptr, int buf_size)
  101. {
  102. int len, ret = 0;
  103. if (buf_size < 2)
  104. return 0;
  105. len = AV_RB16(buf_ptr);
  106. if (len > 14 && len <= buf_size && !strncmp(buf_ptr + 2, "MXM", 3)) {
  107. ret = mxpeg_decode_mxm(s, buf_ptr + 2, len - 2);
  108. }
  109. skip_bits(&s->jpg.gb, 8*FFMIN(len,buf_size));
  110. return ret;
  111. }
  112. static int mxpeg_check_dimensions(MXpegDecodeContext *s, MJpegDecodeContext *jpg,
  113. AVFrame *reference_ptr)
  114. {
  115. if ((jpg->width + 0x0F)>>4 != s->mb_width ||
  116. (jpg->height + 0x0F)>>4 != s->mb_height) {
  117. av_log(jpg->avctx, AV_LOG_ERROR,
  118. "Picture dimensions stored in SOF and MXM mismatch\n");
  119. return AVERROR(EINVAL);
  120. }
  121. if (reference_ptr->data[0]) {
  122. int i;
  123. for (i = 0; i < MAX_COMPONENTS; ++i) {
  124. if ( (!reference_ptr->data[i] ^ !jpg->picture_ptr->data[i]) ||
  125. reference_ptr->linesize[i] != jpg->picture_ptr->linesize[i]) {
  126. av_log(jpg->avctx, AV_LOG_ERROR,
  127. "Dimensions of current and reference picture mismatch\n");
  128. return AVERROR(EINVAL);
  129. }
  130. }
  131. }
  132. return 0;
  133. }
  134. static int mxpeg_decode_frame(AVCodecContext *avctx,
  135. void *data, int *data_size,
  136. AVPacket *avpkt)
  137. {
  138. const uint8_t *buf = avpkt->data;
  139. int buf_size = avpkt->size;
  140. MXpegDecodeContext *s = avctx->priv_data;
  141. MJpegDecodeContext *jpg = &s->jpg;
  142. const uint8_t *buf_end, *buf_ptr;
  143. const uint8_t *unescaped_buf_ptr;
  144. int unescaped_buf_size;
  145. int start_code;
  146. AVFrame *picture = data;
  147. int ret;
  148. buf_ptr = buf;
  149. buf_end = buf + buf_size;
  150. jpg->got_picture = 0;
  151. s->got_mxm_bitmask = 0;
  152. while (buf_ptr < buf_end) {
  153. start_code = ff_mjpeg_find_marker(jpg, &buf_ptr, buf_end,
  154. &unescaped_buf_ptr, &unescaped_buf_size);
  155. if (start_code < 0)
  156. goto the_end;
  157. {
  158. init_get_bits(&jpg->gb, unescaped_buf_ptr, unescaped_buf_size*8);
  159. if (start_code >= APP0 && start_code <= APP15) {
  160. mxpeg_decode_app(s, unescaped_buf_ptr, unescaped_buf_size);
  161. }
  162. switch (start_code) {
  163. case SOI:
  164. if (jpg->got_picture) //emulating EOI
  165. goto the_end;
  166. break;
  167. case EOI:
  168. goto the_end;
  169. case DQT:
  170. ret = ff_mjpeg_decode_dqt(jpg);
  171. if (ret < 0) {
  172. av_log(avctx, AV_LOG_ERROR,
  173. "quantization table decode error\n");
  174. return ret;
  175. }
  176. break;
  177. case DHT:
  178. ret = ff_mjpeg_decode_dht(jpg);
  179. if (ret < 0) {
  180. av_log(avctx, AV_LOG_ERROR,
  181. "huffman table decode error\n");
  182. return ret;
  183. }
  184. break;
  185. case COM:
  186. ret = mxpeg_decode_com(s, unescaped_buf_ptr,
  187. unescaped_buf_size);
  188. if (ret < 0)
  189. return ret;
  190. break;
  191. case SOF0:
  192. s->got_sof_data = 0;
  193. ret = ff_mjpeg_decode_sof(jpg);
  194. if (ret < 0) {
  195. av_log(avctx, AV_LOG_ERROR,
  196. "SOF data decode error\n");
  197. return ret;
  198. }
  199. if (jpg->interlaced) {
  200. av_log(avctx, AV_LOG_ERROR,
  201. "Interlaced mode not supported in MxPEG\n");
  202. return AVERROR(EINVAL);
  203. }
  204. s->got_sof_data = 1;
  205. break;
  206. case SOS:
  207. if (!s->got_sof_data) {
  208. av_log(avctx, AV_LOG_WARNING,
  209. "Can not process SOS without SOF data, skipping\n");
  210. break;
  211. }
  212. if (!jpg->got_picture) {
  213. if (jpg->first_picture) {
  214. av_log(avctx, AV_LOG_WARNING,
  215. "First picture has no SOF, skipping\n");
  216. break;
  217. }
  218. if (!s->got_mxm_bitmask){
  219. av_log(avctx, AV_LOG_WARNING,
  220. "Non-key frame has no MXM, skipping\n");
  221. break;
  222. }
  223. /* use stored SOF data to allocate current picture */
  224. if (jpg->picture_ptr->data[0])
  225. avctx->release_buffer(avctx, jpg->picture_ptr);
  226. if (avctx->get_buffer(avctx, jpg->picture_ptr) < 0) {
  227. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  228. return AVERROR(ENOMEM);
  229. }
  230. jpg->picture_ptr->pict_type = AV_PICTURE_TYPE_P;
  231. jpg->picture_ptr->key_frame = 0;
  232. jpg->got_picture = 1;
  233. } else {
  234. jpg->picture_ptr->pict_type = AV_PICTURE_TYPE_I;
  235. jpg->picture_ptr->key_frame = 1;
  236. }
  237. if (s->got_mxm_bitmask) {
  238. AVFrame *reference_ptr = &s->picture[s->picture_index ^ 1];
  239. if (mxpeg_check_dimensions(s, jpg, reference_ptr) < 0)
  240. break;
  241. /* allocate dummy reference picture if needed */
  242. if (!reference_ptr->data[0] &&
  243. avctx->get_buffer(avctx, reference_ptr) < 0) {
  244. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  245. return AVERROR(ENOMEM);
  246. }
  247. ret = ff_mjpeg_decode_sos(jpg, s->mxm_bitmask, reference_ptr);
  248. if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
  249. return ret;
  250. } else {
  251. ret = ff_mjpeg_decode_sos(jpg, NULL, NULL);
  252. if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
  253. return ret;
  254. }
  255. break;
  256. }
  257. buf_ptr += (get_bits_count(&jpg->gb)+7) >> 3;
  258. }
  259. }
  260. the_end:
  261. if (jpg->got_picture) {
  262. *data_size = sizeof(AVFrame);
  263. *picture = *jpg->picture_ptr;
  264. s->picture_index ^= 1;
  265. jpg->picture_ptr = &s->picture[s->picture_index];
  266. if (!s->has_complete_frame) {
  267. if (!s->got_mxm_bitmask)
  268. s->has_complete_frame = 1;
  269. else
  270. *data_size = 0;
  271. }
  272. }
  273. return buf_ptr - buf;
  274. }
  275. static av_cold int mxpeg_decode_end(AVCodecContext *avctx)
  276. {
  277. MXpegDecodeContext *s = avctx->priv_data;
  278. MJpegDecodeContext *jpg = &s->jpg;
  279. int i;
  280. jpg->picture_ptr = NULL;
  281. ff_mjpeg_decode_end(avctx);
  282. for (i = 0; i < 2; ++i) {
  283. if (s->picture[i].data[0])
  284. avctx->release_buffer(avctx, &s->picture[i]);
  285. }
  286. av_freep(&s->mxm_bitmask);
  287. av_freep(&s->completion_bitmask);
  288. return 0;
  289. }
  290. AVCodec ff_mxpeg_decoder = {
  291. .name = "mxpeg",
  292. .long_name = NULL_IF_CONFIG_SMALL("Mobotix MxPEG video"),
  293. .type = AVMEDIA_TYPE_VIDEO,
  294. .id = CODEC_ID_MXPEG,
  295. .priv_data_size = sizeof(MXpegDecodeContext),
  296. .init = mxpeg_decode_init,
  297. .close = mxpeg_decode_end,
  298. .decode = mxpeg_decode_frame,
  299. .capabilities = CODEC_CAP_DR1,
  300. };