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.

188 lines
6.2KB

  1. /*
  2. * SMJPEG demuxer
  3. * Copyright (c) 2011 Paul B Mahol
  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. * This is a demuxer for Loki SDL Motion JPEG files
  24. */
  25. #include "avformat.h"
  26. #include "internal.h"
  27. #include "riff.h"
  28. static const AVCodecTag codec_smjpeg_video_tags[] = {
  29. { CODEC_ID_MJPEG, MKTAG('J', 'F', 'I', 'F') },
  30. { CODEC_ID_NONE, 0 },
  31. };
  32. static const AVCodecTag codec_smjpeg_audio_tags[] = {
  33. { CODEC_ID_ADPCM_IMA_SMJPEG, MKTAG('A', 'P', 'C', 'M') },
  34. { CODEC_ID_PCM_S16LE, MKTAG('N', 'O', 'N', 'E') },
  35. { CODEC_ID_NONE, 0 },
  36. };
  37. typedef struct SMJPEGContext {
  38. int audio_stream_index;
  39. int video_stream_index;
  40. } SMJPEGContext;
  41. static int smjpeg_probe(AVProbeData *p)
  42. {
  43. if (!memcmp(p->buf, "\x0\xaSMJPEG", 8))
  44. return AVPROBE_SCORE_MAX;
  45. return 0;
  46. }
  47. static int smjpeg_read_header(AVFormatContext *s, AVFormatParameters *ap)
  48. {
  49. SMJPEGContext *sc = s->priv_data;
  50. AVStream *ast = NULL, *vst = NULL;
  51. AVIOContext *pb = s->pb;
  52. uint32_t version, htype, hlength, duration;
  53. char *comment;
  54. avio_skip(pb, 8); // magic
  55. version = avio_rb32(pb);
  56. if (version)
  57. av_log_ask_for_sample(s, "unknown version %d\n", version);
  58. duration = avio_rb32(pb); // in msec
  59. while (!pb->eof_reached) {
  60. htype = avio_rl32(pb);
  61. switch (htype) {
  62. case MKTAG('_', 'T', 'X', 'T'):
  63. hlength = avio_rb32(pb);
  64. if (!hlength || hlength > 512)
  65. return AVERROR_INVALIDDATA;
  66. comment = av_malloc(hlength + 1);
  67. if (!comment)
  68. return AVERROR(ENOMEM);
  69. if (avio_read(pb, comment, hlength) != hlength) {
  70. av_freep(&comment);
  71. av_log(s, AV_LOG_ERROR, "error when reading comment\n");
  72. return AVERROR_INVALIDDATA;
  73. }
  74. comment[hlength] = 0;
  75. av_dict_set(&s->metadata, "comment", comment,
  76. AV_DICT_DONT_STRDUP_VAL);
  77. break;
  78. case MKTAG('_', 'S', 'N', 'D'):
  79. if (ast) {
  80. av_log_ask_for_sample(s, "multiple audio streams not supported\n");
  81. return AVERROR_INVALIDDATA;
  82. }
  83. hlength = avio_rb32(pb);
  84. if (hlength < 8)
  85. return AVERROR_INVALIDDATA;
  86. ast = avformat_new_stream(s, 0);
  87. if (!ast)
  88. return AVERROR(ENOMEM);
  89. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  90. ast->codec->sample_rate = avio_rb16(pb);
  91. ast->codec->bits_per_coded_sample = avio_r8(pb);
  92. ast->codec->channels = avio_r8(pb);
  93. ast->codec->codec_tag = avio_rl32(pb);
  94. ast->codec->codec_id = ff_codec_get_id(codec_smjpeg_audio_tags,
  95. ast->codec->codec_tag);
  96. ast->duration = duration;
  97. sc->audio_stream_index = ast->index;
  98. avpriv_set_pts_info(ast, 32, 1, 1000);
  99. avio_skip(pb, hlength - 8);
  100. break;
  101. case MKTAG('_', 'V', 'I', 'D'):
  102. if (vst) {
  103. av_log_ask_for_sample(s, "multiple video streams not supported\n");
  104. return AVERROR_INVALIDDATA;
  105. }
  106. hlength = avio_rb32(pb);
  107. if (hlength < 12)
  108. return AVERROR_INVALIDDATA;
  109. avio_skip(pb, 4); // number of frames
  110. vst = avformat_new_stream(s, 0);
  111. if (!vst)
  112. return AVERROR(ENOMEM);
  113. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  114. vst->codec->width = avio_rb16(pb);
  115. vst->codec->height = avio_rb16(pb);
  116. vst->codec->codec_tag = avio_rl32(pb);
  117. vst->codec->codec_id = ff_codec_get_id(codec_smjpeg_video_tags,
  118. vst->codec->codec_tag);
  119. vst->duration = duration;
  120. sc->video_stream_index = vst->index;
  121. avpriv_set_pts_info(vst, 32, 1, 1000);
  122. avio_skip(pb, hlength - 12);
  123. break;
  124. case MKTAG('H', 'E', 'N', 'D'):
  125. return 0;
  126. default:
  127. av_log(s, AV_LOG_ERROR, "unknown header %x\n", htype);
  128. return AVERROR_INVALIDDATA;
  129. }
  130. }
  131. return AVERROR_EOF;
  132. }
  133. static int smjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)
  134. {
  135. SMJPEGContext *sc = s->priv_data;
  136. uint32_t dtype, ret, size, timestamp;
  137. if (s->pb->eof_reached)
  138. return AVERROR_EOF;
  139. dtype = avio_rl32(s->pb);
  140. switch (dtype) {
  141. case MKTAG('s', 'n', 'd', 'D'):
  142. timestamp = avio_rb32(s->pb);
  143. size = avio_rb32(s->pb);
  144. ret = av_get_packet(s->pb, pkt, size);
  145. pkt->stream_index = sc->audio_stream_index;
  146. pkt->pts = timestamp;
  147. break;
  148. case MKTAG('v', 'i', 'd', 'D'):
  149. timestamp = avio_rb32(s->pb);
  150. size = avio_rb32(s->pb);
  151. ret = av_get_packet(s->pb, pkt, size);
  152. pkt->stream_index = sc->video_stream_index;
  153. pkt->pts = timestamp;
  154. break;
  155. case MKTAG('D', 'O', 'N', 'E'):
  156. ret = AVERROR_EOF;
  157. break;
  158. default:
  159. av_log(s, AV_LOG_ERROR, "unknown chunk %x\n", dtype);
  160. ret = AVERROR_INVALIDDATA;
  161. break;
  162. }
  163. return ret;
  164. }
  165. AVInputFormat ff_smjpeg_demuxer = {
  166. .name = "smjpeg",
  167. .long_name = NULL_IF_CONFIG_SMALL("Loki SDL MJPEG"),
  168. .priv_data_size = sizeof(SMJPEGContext),
  169. .read_probe = smjpeg_probe,
  170. .read_header = smjpeg_read_header,
  171. .read_packet = smjpeg_read_packet,
  172. .extensions = "mjpg",
  173. };