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.

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