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.

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