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.

355 lines
12KB

  1. /*
  2. * MxPEG decoder
  3. * Copyright (c) 2011 Anatoly Nenashev
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. s->bitmask_size = 0;
  50. av_freep(&s->mxm_bitmask);
  51. av_freep(&s->completion_bitmask);
  52. return 0;
  53. }
  54. static av_cold int mxpeg_decode_init(AVCodecContext *avctx)
  55. {
  56. MXpegDecodeContext *s = avctx->priv_data;
  57. s->picture[0] = av_frame_alloc();
  58. s->picture[1] = av_frame_alloc();
  59. if (!s->picture[0] || !s->picture[1])
  60. return AVERROR(ENOMEM);
  61. s->jpg.picture_ptr = s->picture[0];
  62. return ff_mjpeg_decode_init(avctx);
  63. }
  64. static int mxpeg_decode_app(MXpegDecodeContext *s,
  65. const uint8_t *buf_ptr, int buf_size)
  66. {
  67. int len;
  68. if (buf_size < 2)
  69. return 0;
  70. len = AV_RB16(buf_ptr);
  71. skip_bits(&s->jpg.gb, 8*FFMIN(len,buf_size));
  72. return 0;
  73. }
  74. static int mxpeg_decode_mxm(MXpegDecodeContext *s,
  75. const uint8_t *buf_ptr, int buf_size)
  76. {
  77. unsigned bitmask_size, mb_count;
  78. int i;
  79. s->mb_width = AV_RL16(buf_ptr+4);
  80. s->mb_height = AV_RL16(buf_ptr+6);
  81. mb_count = s->mb_width * s->mb_height;
  82. bitmask_size = (mb_count + 7) >> 3;
  83. if (bitmask_size > buf_size - 12) {
  84. av_log(s->jpg.avctx, AV_LOG_ERROR,
  85. "MXM bitmask is not complete\n");
  86. return AVERROR(EINVAL);
  87. }
  88. if (s->bitmask_size != bitmask_size) {
  89. s->bitmask_size = 0;
  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. s->got_sof_data = !!s->got_sof_data;
  171. while (buf_ptr < buf_end) {
  172. start_code = ff_mjpeg_find_marker(jpg, &buf_ptr, buf_end,
  173. &unescaped_buf_ptr, &unescaped_buf_size);
  174. if (start_code < 0)
  175. goto the_end;
  176. {
  177. init_get_bits(&jpg->gb, unescaped_buf_ptr, unescaped_buf_size*8);
  178. if (start_code >= APP0 && start_code <= APP15) {
  179. mxpeg_decode_app(s, unescaped_buf_ptr, unescaped_buf_size);
  180. }
  181. switch (start_code) {
  182. case SOI:
  183. if (jpg->got_picture) //emulating EOI
  184. goto the_end;
  185. break;
  186. case EOI:
  187. goto the_end;
  188. case DQT:
  189. ret = ff_mjpeg_decode_dqt(jpg);
  190. if (ret < 0) {
  191. av_log(avctx, AV_LOG_ERROR,
  192. "quantization table decode error\n");
  193. return ret;
  194. }
  195. break;
  196. case DHT:
  197. ret = ff_mjpeg_decode_dht(jpg);
  198. if (ret < 0) {
  199. av_log(avctx, AV_LOG_ERROR,
  200. "huffman table decode error\n");
  201. return ret;
  202. }
  203. break;
  204. case COM:
  205. ret = mxpeg_decode_com(s, unescaped_buf_ptr,
  206. unescaped_buf_size);
  207. if (ret < 0)
  208. return ret;
  209. break;
  210. case SOF0:
  211. if (s->got_sof_data > 1) {
  212. av_log(avctx, AV_LOG_ERROR,
  213. "Multiple SOF in a frame\n");
  214. return AVERROR_INVALIDDATA;
  215. }
  216. ret = ff_mjpeg_decode_sof(jpg);
  217. if (ret < 0) {
  218. av_log(avctx, AV_LOG_ERROR,
  219. "SOF data decode error\n");
  220. s->got_sof_data = 0;
  221. return ret;
  222. }
  223. if (jpg->interlaced) {
  224. av_log(avctx, AV_LOG_ERROR,
  225. "Interlaced mode not supported in MxPEG\n");
  226. s->got_sof_data = 0;
  227. return AVERROR(EINVAL);
  228. }
  229. s->got_sof_data ++;
  230. break;
  231. case SOS:
  232. if (!s->got_sof_data) {
  233. av_log(avctx, AV_LOG_WARNING,
  234. "Can not process SOS without SOF data, skipping\n");
  235. break;
  236. }
  237. if (!jpg->got_picture) {
  238. if (jpg->first_picture) {
  239. av_log(avctx, AV_LOG_WARNING,
  240. "First picture has no SOF, skipping\n");
  241. break;
  242. }
  243. if (!s->got_mxm_bitmask){
  244. av_log(avctx, AV_LOG_WARNING,
  245. "Non-key frame has no MXM, skipping\n");
  246. break;
  247. }
  248. /* use stored SOF data to allocate current picture */
  249. av_frame_unref(jpg->picture_ptr);
  250. if ((ret = ff_get_buffer(avctx, jpg->picture_ptr,
  251. AV_GET_BUFFER_FLAG_REF)) < 0)
  252. return ret;
  253. jpg->picture_ptr->pict_type = AV_PICTURE_TYPE_P;
  254. jpg->picture_ptr->key_frame = 0;
  255. jpg->got_picture = 1;
  256. } else {
  257. jpg->picture_ptr->pict_type = AV_PICTURE_TYPE_I;
  258. jpg->picture_ptr->key_frame = 1;
  259. }
  260. if (s->got_mxm_bitmask) {
  261. AVFrame *reference_ptr = s->picture[s->picture_index ^ 1];
  262. if (mxpeg_check_dimensions(s, jpg, reference_ptr) < 0)
  263. break;
  264. /* allocate dummy reference picture if needed */
  265. if (!reference_ptr->data[0] &&
  266. (ret = ff_get_buffer(avctx, reference_ptr,
  267. AV_GET_BUFFER_FLAG_REF)) < 0)
  268. return ret;
  269. ret = ff_mjpeg_decode_sos(jpg, s->mxm_bitmask, s->bitmask_size, reference_ptr);
  270. if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
  271. return ret;
  272. } else {
  273. ret = ff_mjpeg_decode_sos(jpg, NULL, 0, NULL);
  274. if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
  275. return ret;
  276. }
  277. break;
  278. }
  279. buf_ptr += (get_bits_count(&jpg->gb)+7) >> 3;
  280. }
  281. }
  282. the_end:
  283. if (jpg->got_picture) {
  284. int ret = av_frame_ref(data, jpg->picture_ptr);
  285. if (ret < 0)
  286. return ret;
  287. *got_frame = 1;
  288. s->picture_index ^= 1;
  289. jpg->picture_ptr = s->picture[s->picture_index];
  290. if (!s->has_complete_frame) {
  291. if (!s->got_mxm_bitmask)
  292. s->has_complete_frame = 1;
  293. else
  294. *got_frame = 0;
  295. }
  296. }
  297. return buf_ptr - buf;
  298. }
  299. AVCodec ff_mxpeg_decoder = {
  300. .name = "mxpeg",
  301. .long_name = NULL_IF_CONFIG_SMALL("Mobotix MxPEG video"),
  302. .type = AVMEDIA_TYPE_VIDEO,
  303. .id = AV_CODEC_ID_MXPEG,
  304. .priv_data_size = sizeof(MXpegDecodeContext),
  305. .init = mxpeg_decode_init,
  306. .close = mxpeg_decode_end,
  307. .decode = mxpeg_decode_frame,
  308. .capabilities = AV_CODEC_CAP_DR1,
  309. .max_lowres = 3,
  310. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
  311. };