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.

343 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 "internal.h"
  26. #include "mjpeg.h"
  27. #include "mjpegdec.h"
  28. typedef struct MXpegDecodeContext {
  29. MJpegDecodeContext jpg;
  30. AVFrame picture[2]; /* pictures array */
  31. int picture_index; /* index of current picture */
  32. int got_sof_data; /* true if SOF data successfully parsed */
  33. int got_mxm_bitmask; /* true if MXM bitmask available */
  34. uint8_t *mxm_bitmask; /* bitmask buffer */
  35. unsigned bitmask_size; /* size of bitmask */
  36. int has_complete_frame; /* true if has complete frame */
  37. uint8_t *completion_bitmask; /* completion bitmask of macroblocks */
  38. unsigned mb_width, mb_height; /* size of picture in MB's from MXM header */
  39. } MXpegDecodeContext;
  40. static av_cold int mxpeg_decode_init(AVCodecContext *avctx)
  41. {
  42. MXpegDecodeContext *s = avctx->priv_data;
  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 *got_frame,
  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. int ret;
  147. buf_ptr = buf;
  148. buf_end = buf + buf_size;
  149. jpg->got_picture = 0;
  150. s->got_mxm_bitmask = 0;
  151. while (buf_ptr < buf_end) {
  152. start_code = ff_mjpeg_find_marker(jpg, &buf_ptr, buf_end,
  153. &unescaped_buf_ptr, &unescaped_buf_size);
  154. if (start_code < 0)
  155. goto the_end;
  156. {
  157. init_get_bits(&jpg->gb, unescaped_buf_ptr, unescaped_buf_size*8);
  158. if (start_code >= APP0 && start_code <= APP15) {
  159. mxpeg_decode_app(s, unescaped_buf_ptr, unescaped_buf_size);
  160. }
  161. switch (start_code) {
  162. case SOI:
  163. if (jpg->got_picture) //emulating EOI
  164. goto the_end;
  165. break;
  166. case EOI:
  167. goto the_end;
  168. case DQT:
  169. ret = ff_mjpeg_decode_dqt(jpg);
  170. if (ret < 0) {
  171. av_log(avctx, AV_LOG_ERROR,
  172. "quantization table decode error\n");
  173. return ret;
  174. }
  175. break;
  176. case DHT:
  177. ret = ff_mjpeg_decode_dht(jpg);
  178. if (ret < 0) {
  179. av_log(avctx, AV_LOG_ERROR,
  180. "huffman table decode error\n");
  181. return ret;
  182. }
  183. break;
  184. case COM:
  185. ret = mxpeg_decode_com(s, unescaped_buf_ptr,
  186. unescaped_buf_size);
  187. if (ret < 0)
  188. return ret;
  189. break;
  190. case SOF0:
  191. s->got_sof_data = 0;
  192. ret = ff_mjpeg_decode_sof(jpg);
  193. if (ret < 0) {
  194. av_log(avctx, AV_LOG_ERROR,
  195. "SOF data decode error\n");
  196. return ret;
  197. }
  198. if (jpg->interlaced) {
  199. av_log(avctx, AV_LOG_ERROR,
  200. "Interlaced mode not supported in MxPEG\n");
  201. return AVERROR(EINVAL);
  202. }
  203. s->got_sof_data = 1;
  204. break;
  205. case SOS:
  206. if (!s->got_sof_data) {
  207. av_log(avctx, AV_LOG_WARNING,
  208. "Can not process SOS without SOF data, skipping\n");
  209. break;
  210. }
  211. if (!jpg->got_picture) {
  212. if (jpg->first_picture) {
  213. av_log(avctx, AV_LOG_WARNING,
  214. "First picture has no SOF, skipping\n");
  215. break;
  216. }
  217. if (!s->got_mxm_bitmask){
  218. av_log(avctx, AV_LOG_WARNING,
  219. "Non-key frame has no MXM, skipping\n");
  220. break;
  221. }
  222. /* use stored SOF data to allocate current picture */
  223. av_frame_unref(jpg->picture_ptr);
  224. if (ff_get_buffer(avctx, jpg->picture_ptr,
  225. AV_GET_BUFFER_FLAG_REF) < 0) {
  226. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  227. return AVERROR(ENOMEM);
  228. }
  229. jpg->picture_ptr->pict_type = AV_PICTURE_TYPE_P;
  230. jpg->picture_ptr->key_frame = 0;
  231. jpg->got_picture = 1;
  232. } else {
  233. jpg->picture_ptr->pict_type = AV_PICTURE_TYPE_I;
  234. jpg->picture_ptr->key_frame = 1;
  235. }
  236. if (s->got_mxm_bitmask) {
  237. AVFrame *reference_ptr = &s->picture[s->picture_index ^ 1];
  238. if (mxpeg_check_dimensions(s, jpg, reference_ptr) < 0)
  239. break;
  240. /* allocate dummy reference picture if needed */
  241. if (!reference_ptr->data[0] &&
  242. ff_get_buffer(avctx, reference_ptr,
  243. AV_GET_BUFFER_FLAG_REF) < 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. int ret = av_frame_ref(data, jpg->picture_ptr);
  263. if (ret < 0)
  264. return ret;
  265. *got_frame = 1;
  266. s->picture_index ^= 1;
  267. jpg->picture_ptr = &s->picture[s->picture_index];
  268. if (!s->has_complete_frame) {
  269. if (!s->got_mxm_bitmask)
  270. s->has_complete_frame = 1;
  271. else
  272. *got_frame = 0;
  273. }
  274. }
  275. return buf_ptr - buf;
  276. }
  277. static av_cold int mxpeg_decode_end(AVCodecContext *avctx)
  278. {
  279. MXpegDecodeContext *s = avctx->priv_data;
  280. MJpegDecodeContext *jpg = &s->jpg;
  281. int i;
  282. jpg->picture_ptr = NULL;
  283. ff_mjpeg_decode_end(avctx);
  284. for (i = 0; i < 2; ++i)
  285. av_frame_unref(&s->picture[i]);
  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 = AV_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. };