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.

345 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_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. s->bitmask_size = 0;
  72. av_freep(&s->mxm_bitmask);
  73. s->mxm_bitmask = av_malloc(bitmask_size);
  74. if (!s->mxm_bitmask) {
  75. av_log(s->jpg.avctx, AV_LOG_ERROR,
  76. "MXM bitmask memory allocation error\n");
  77. return AVERROR(ENOMEM);
  78. }
  79. av_freep(&s->completion_bitmask);
  80. s->completion_bitmask = av_mallocz(bitmask_size);
  81. if (!s->completion_bitmask) {
  82. av_log(s->jpg.avctx, AV_LOG_ERROR,
  83. "Completion bitmask memory allocation error\n");
  84. return AVERROR(ENOMEM);
  85. }
  86. s->bitmask_size = bitmask_size;
  87. }
  88. memcpy(s->mxm_bitmask, buf_ptr + 12, bitmask_size);
  89. s->got_mxm_bitmask = 1;
  90. if (!s->has_complete_frame) {
  91. uint8_t completion_check = 0xFF;
  92. for (i = 0; i < bitmask_size; ++i) {
  93. s->completion_bitmask[i] |= s->mxm_bitmask[i];
  94. completion_check &= s->completion_bitmask[i];
  95. }
  96. s->has_complete_frame = !(completion_check ^ 0xFF);
  97. }
  98. return 0;
  99. }
  100. static int mxpeg_decode_com(MXpegDecodeContext *s,
  101. const uint8_t *buf_ptr, int buf_size)
  102. {
  103. int len, ret = 0;
  104. if (buf_size < 2)
  105. return 0;
  106. len = AV_RB16(buf_ptr);
  107. if (len > 14 && len <= buf_size && !strncmp(buf_ptr + 2, "MXM", 3)) {
  108. ret = mxpeg_decode_mxm(s, buf_ptr + 2, len - 2);
  109. }
  110. skip_bits(&s->jpg.gb, 8*FFMIN(len,buf_size));
  111. return ret;
  112. }
  113. static int mxpeg_check_dimensions(MXpegDecodeContext *s, MJpegDecodeContext *jpg,
  114. AVFrame *reference_ptr)
  115. {
  116. if ((jpg->width + 0x0F)>>4 != s->mb_width ||
  117. (jpg->height + 0x0F)>>4 != s->mb_height) {
  118. av_log(jpg->avctx, AV_LOG_ERROR,
  119. "Picture dimensions stored in SOF and MXM mismatch\n");
  120. return AVERROR(EINVAL);
  121. }
  122. if (reference_ptr->data[0]) {
  123. int i;
  124. for (i = 0; i < MAX_COMPONENTS; ++i) {
  125. if ( (!reference_ptr->data[i] ^ !jpg->picture_ptr->data[i]) ||
  126. reference_ptr->linesize[i] != jpg->picture_ptr->linesize[i]) {
  127. av_log(jpg->avctx, AV_LOG_ERROR,
  128. "Dimensions of current and reference picture mismatch\n");
  129. return AVERROR(EINVAL);
  130. }
  131. }
  132. }
  133. return 0;
  134. }
  135. static int mxpeg_decode_frame(AVCodecContext *avctx,
  136. void *data, int *got_frame,
  137. AVPacket *avpkt)
  138. {
  139. const uint8_t *buf = avpkt->data;
  140. int buf_size = avpkt->size;
  141. MXpegDecodeContext *s = avctx->priv_data;
  142. MJpegDecodeContext *jpg = &s->jpg;
  143. const uint8_t *buf_end, *buf_ptr;
  144. const uint8_t *unescaped_buf_ptr;
  145. int unescaped_buf_size;
  146. int start_code;
  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. av_frame_unref(jpg->picture_ptr);
  225. if (ff_get_buffer(avctx, jpg->picture_ptr,
  226. AV_GET_BUFFER_FLAG_REF) < 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. ff_get_buffer(avctx, reference_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. ret = ff_mjpeg_decode_sos(jpg, s->mxm_bitmask, reference_ptr);
  249. if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
  250. return ret;
  251. } else {
  252. ret = ff_mjpeg_decode_sos(jpg, NULL, NULL);
  253. if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
  254. return ret;
  255. }
  256. break;
  257. }
  258. buf_ptr += (get_bits_count(&jpg->gb)+7) >> 3;
  259. }
  260. }
  261. the_end:
  262. if (jpg->got_picture) {
  263. int ret = av_frame_ref(data, jpg->picture_ptr);
  264. if (ret < 0)
  265. return ret;
  266. *got_frame = 1;
  267. s->picture_index ^= 1;
  268. jpg->picture_ptr = &s->picture[s->picture_index];
  269. if (!s->has_complete_frame) {
  270. if (!s->got_mxm_bitmask)
  271. s->has_complete_frame = 1;
  272. else
  273. *got_frame = 0;
  274. }
  275. }
  276. return buf_ptr - buf;
  277. }
  278. static av_cold int mxpeg_decode_end(AVCodecContext *avctx)
  279. {
  280. MXpegDecodeContext *s = avctx->priv_data;
  281. MJpegDecodeContext *jpg = &s->jpg;
  282. int i;
  283. jpg->picture_ptr = NULL;
  284. ff_mjpeg_decode_end(avctx);
  285. for (i = 0; i < 2; ++i)
  286. av_frame_unref(&s->picture[i]);
  287. av_freep(&s->mxm_bitmask);
  288. av_freep(&s->completion_bitmask);
  289. return 0;
  290. }
  291. AVCodec ff_mxpeg_decoder = {
  292. .name = "mxpeg",
  293. .long_name = NULL_IF_CONFIG_SMALL("Mobotix MxPEG video"),
  294. .type = AVMEDIA_TYPE_VIDEO,
  295. .id = AV_CODEC_ID_MXPEG,
  296. .priv_data_size = sizeof(MXpegDecodeContext),
  297. .init = mxpeg_decode_init,
  298. .close = mxpeg_decode_end,
  299. .decode = mxpeg_decode_frame,
  300. .capabilities = CODEC_CAP_DR1,
  301. .max_lowres = 3,
  302. };