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.

186 lines
6.0KB

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