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.

350 lines
11KB

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