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.

184 lines
5.9KB

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