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.

193 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 <inttypes.h>
  26. #include "avformat.h"
  27. #include "internal.h"
  28. #include "riff.h"
  29. #include "smjpeg.h"
  30. typedef struct SMJPEGContext {
  31. int audio_stream_index;
  32. int video_stream_index;
  33. } SMJPEGContext;
  34. static int smjpeg_probe(AVProbeData *p)
  35. {
  36. if (!memcmp(p->buf, SMJPEG_MAGIC, 8))
  37. return AVPROBE_SCORE_MAX;
  38. return 0;
  39. }
  40. static int smjpeg_read_header(AVFormatContext *s)
  41. {
  42. SMJPEGContext *sc = s->priv_data;
  43. AVStream *ast = NULL, *vst = NULL;
  44. AVIOContext *pb = s->pb;
  45. uint32_t version, htype, hlength, duration;
  46. char *comment;
  47. sc->audio_stream_index =
  48. sc->video_stream_index = -1;
  49. avio_skip(pb, 8); // magic
  50. version = avio_rb32(pb);
  51. if (version)
  52. avpriv_request_sample(s, "Unknown version %"PRIu32, version);
  53. duration = avio_rb32(pb); // in msec
  54. while (!avio_feof(pb)) {
  55. htype = avio_rl32(pb);
  56. switch (htype) {
  57. case SMJPEG_TXT:
  58. hlength = avio_rb32(pb);
  59. if (!hlength || hlength > 512)
  60. return AVERROR_INVALIDDATA;
  61. comment = av_malloc(hlength + 1);
  62. if (!comment)
  63. return AVERROR(ENOMEM);
  64. if (avio_read(pb, comment, hlength) != hlength) {
  65. av_freep(&comment);
  66. av_log(s, AV_LOG_ERROR, "error when reading comment\n");
  67. return AVERROR_INVALIDDATA;
  68. }
  69. comment[hlength] = 0;
  70. av_dict_set(&s->metadata, "comment", comment,
  71. AV_DICT_DONT_STRDUP_VAL);
  72. break;
  73. case SMJPEG_SND:
  74. if (ast) {
  75. avpriv_request_sample(s, "Multiple audio streams");
  76. return AVERROR_PATCHWELCOME;
  77. }
  78. hlength = avio_rb32(pb);
  79. if (hlength < 8)
  80. return AVERROR_INVALIDDATA;
  81. ast = avformat_new_stream(s, 0);
  82. if (!ast)
  83. return AVERROR(ENOMEM);
  84. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  85. ast->codec->sample_rate = avio_rb16(pb);
  86. ast->codec->bits_per_coded_sample = avio_r8(pb);
  87. ast->codec->channels = avio_r8(pb);
  88. ast->codec->codec_tag = avio_rl32(pb);
  89. ast->codec->codec_id = ff_codec_get_id(ff_codec_smjpeg_audio_tags,
  90. ast->codec->codec_tag);
  91. ast->duration = duration;
  92. sc->audio_stream_index = ast->index;
  93. avpriv_set_pts_info(ast, 32, 1, 1000);
  94. avio_skip(pb, hlength - 8);
  95. break;
  96. case SMJPEG_VID:
  97. if (vst) {
  98. avpriv_request_sample(s, "Multiple video streams");
  99. return AVERROR_INVALIDDATA;
  100. }
  101. hlength = avio_rb32(pb);
  102. if (hlength < 12)
  103. return AVERROR_INVALIDDATA;
  104. vst = avformat_new_stream(s, 0);
  105. if (!vst)
  106. return AVERROR(ENOMEM);
  107. vst->nb_frames = avio_rb32(pb);
  108. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  109. vst->codec->width = avio_rb16(pb);
  110. vst->codec->height = avio_rb16(pb);
  111. vst->codec->codec_tag = avio_rl32(pb);
  112. vst->codec->codec_id = ff_codec_get_id(ff_codec_smjpeg_video_tags,
  113. vst->codec->codec_tag);
  114. vst->duration = duration;
  115. sc->video_stream_index = vst->index;
  116. avpriv_set_pts_info(vst, 32, 1, 1000);
  117. avio_skip(pb, hlength - 12);
  118. break;
  119. case SMJPEG_HEND:
  120. return 0;
  121. default:
  122. av_log(s, AV_LOG_ERROR, "unknown header %"PRIx32"\n", htype);
  123. return AVERROR_INVALIDDATA;
  124. }
  125. }
  126. return AVERROR_EOF;
  127. }
  128. static int smjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)
  129. {
  130. SMJPEGContext *sc = s->priv_data;
  131. uint32_t dtype, size, timestamp;
  132. int64_t pos;
  133. int ret;
  134. if (avio_feof(s->pb))
  135. return AVERROR_EOF;
  136. pos = avio_tell(s->pb);
  137. dtype = avio_rl32(s->pb);
  138. switch (dtype) {
  139. case SMJPEG_SNDD:
  140. if (sc->audio_stream_index < 0)
  141. return AVERROR_INVALIDDATA;
  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. pkt->pos = pos;
  148. break;
  149. case SMJPEG_VIDD:
  150. if (sc->video_stream_index < 0)
  151. return AVERROR_INVALIDDATA;
  152. timestamp = avio_rb32(s->pb);
  153. size = avio_rb32(s->pb);
  154. ret = av_get_packet(s->pb, pkt, size);
  155. pkt->stream_index = sc->video_stream_index;
  156. pkt->pts = timestamp;
  157. pkt->pos = pos;
  158. break;
  159. case SMJPEG_DONE:
  160. ret = AVERROR_EOF;
  161. break;
  162. default:
  163. av_log(s, AV_LOG_ERROR, "unknown chunk %"PRIx32"\n", dtype);
  164. ret = AVERROR_INVALIDDATA;
  165. break;
  166. }
  167. return ret;
  168. }
  169. AVInputFormat ff_smjpeg_demuxer = {
  170. .name = "smjpeg",
  171. .long_name = NULL_IF_CONFIG_SMALL("Loki SDL MJPEG"),
  172. .priv_data_size = sizeof(SMJPEGContext),
  173. .read_probe = smjpeg_probe,
  174. .read_header = smjpeg_read_header,
  175. .read_packet = smjpeg_read_packet,
  176. .extensions = "mjpg",
  177. .flags = AVFMT_GENERIC_INDEX,
  178. };