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.

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