smjpegdec.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. av_log_ask_for_sample(s, "unknown version %d\n", version);
  50. duration = avio_rb32(pb); // in msec
  51. while (!pb->eof_reached) {
  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. av_log_ask_for_sample(s, "multiple audio streams not supported\n");
  73. return AVERROR_INVALIDDATA;
  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. av_log_ask_for_sample(s, "multiple video streams not supported\n");
  96. return AVERROR_INVALIDDATA;
  97. }
  98. hlength = avio_rb32(pb);
  99. if (hlength < 12)
  100. return AVERROR_INVALIDDATA;
  101. avio_skip(pb, 4); // number of frames
  102. vst = avformat_new_stream(s, 0);
  103. if (!vst)
  104. return AVERROR(ENOMEM);
  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, ret, size, timestamp;
  129. int64_t pos;
  130. if (s->pb->eof_reached)
  131. return AVERROR_EOF;
  132. pos = avio_tell(s->pb);
  133. dtype = avio_rl32(s->pb);
  134. switch (dtype) {
  135. case SMJPEG_SNDD:
  136. timestamp = avio_rb32(s->pb);
  137. size = avio_rb32(s->pb);
  138. ret = av_get_packet(s->pb, pkt, size);
  139. pkt->stream_index = sc->audio_stream_index;
  140. pkt->pts = timestamp;
  141. pkt->pos = pos;
  142. break;
  143. case SMJPEG_VIDD:
  144. timestamp = avio_rb32(s->pb);
  145. size = avio_rb32(s->pb);
  146. ret = av_get_packet(s->pb, pkt, size);
  147. pkt->stream_index = sc->video_stream_index;
  148. pkt->pts = timestamp;
  149. pkt->pos = pos;
  150. break;
  151. case SMJPEG_DONE:
  152. ret = AVERROR_EOF;
  153. break;
  154. default:
  155. av_log(s, AV_LOG_ERROR, "unknown chunk %x\n", dtype);
  156. ret = AVERROR_INVALIDDATA;
  157. break;
  158. }
  159. return ret;
  160. }
  161. AVInputFormat ff_smjpeg_demuxer = {
  162. .name = "smjpeg",
  163. .long_name = NULL_IF_CONFIG_SMALL("Loki SDL MJPEG"),
  164. .priv_data_size = sizeof(SMJPEGContext),
  165. .read_probe = smjpeg_probe,
  166. .read_header = smjpeg_read_header,
  167. .read_packet = smjpeg_read_packet,
  168. .extensions = "mjpg",
  169. .flags = AVFMT_GENERIC_INDEX,
  170. };