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.

147 lines
4.5KB

  1. /*
  2. * SMJPEG muxer
  3. * Copyright (c) 2012 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 muxer for Loki SDL Motion JPEG files
  24. */
  25. #include "avformat.h"
  26. #include "internal.h"
  27. #include "smjpeg.h"
  28. typedef struct SMJPEGMuxContext {
  29. uint32_t duration;
  30. } SMJPEGMuxContext;
  31. static int smjpeg_write_header(AVFormatContext *s)
  32. {
  33. AVDictionaryEntry *t = NULL;
  34. AVIOContext *pb = s->pb;
  35. int n, tag;
  36. if (s->nb_streams > 2) {
  37. av_log(s, AV_LOG_ERROR, "more than >2 streams are not supported\n");
  38. return AVERROR(EINVAL);
  39. }
  40. avio_write(pb, SMJPEG_MAGIC, 8);
  41. avio_wb32(pb, 0);
  42. avio_wb32(pb, 0);
  43. while ((t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX))) {
  44. avio_wl32(pb, SMJPEG_TXT);
  45. avio_wb32(pb, strlen(t->key) + strlen(t->value) + 3);
  46. avio_write(pb, t->key, strlen(t->key));
  47. avio_write(pb, " = ", 3);
  48. avio_write(pb, t->value, strlen(t->value));
  49. }
  50. for (n = 0; n < s->nb_streams; n++) {
  51. AVStream *st = s->streams[n];
  52. AVCodecContext *codec = st->codec;
  53. if (codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  54. tag = ff_codec_get_tag(ff_codec_smjpeg_audio_tags, codec->codec_id);
  55. if (!tag) {
  56. av_log(s, AV_LOG_ERROR, "unsupported audio codec\n");
  57. return AVERROR(EINVAL);
  58. }
  59. avio_wl32(pb, SMJPEG_SND);
  60. avio_wb32(pb, 8);
  61. avio_wb16(pb, codec->sample_rate);
  62. avio_w8(pb, codec->bits_per_coded_sample);
  63. avio_w8(pb, codec->channels);
  64. avio_wl32(pb, tag);
  65. avpriv_set_pts_info(st, 32, 1, 1000);
  66. } else if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  67. tag = ff_codec_get_tag(ff_codec_smjpeg_video_tags, codec->codec_id);
  68. if (!tag) {
  69. av_log(s, AV_LOG_ERROR, "unsupported video codec\n");
  70. return AVERROR(EINVAL);
  71. }
  72. avio_wl32(pb, SMJPEG_VID);
  73. avio_wb32(pb, 12);
  74. avio_wb32(pb, 0);
  75. avio_wb16(pb, codec->width);
  76. avio_wb16(pb, codec->height);
  77. avio_wl32(pb, tag);
  78. avpriv_set_pts_info(st, 32, 1, 1000);
  79. }
  80. }
  81. avio_wl32(pb, SMJPEG_HEND);
  82. avio_flush(pb);
  83. return 0;
  84. }
  85. static int smjpeg_write_packet(AVFormatContext *s, AVPacket *pkt)
  86. {
  87. SMJPEGMuxContext *smc = s->priv_data;
  88. AVIOContext *pb = s->pb;
  89. AVStream *st = s->streams[pkt->stream_index];
  90. AVCodecContext *codec = st->codec;
  91. if (codec->codec_type == AVMEDIA_TYPE_AUDIO)
  92. avio_wl32(pb, SMJPEG_SNDD);
  93. else if (codec->codec_type == AVMEDIA_TYPE_VIDEO)
  94. avio_wl32(pb, SMJPEG_VIDD);
  95. else
  96. return 0;
  97. avio_wb32(pb, pkt->pts);
  98. avio_wb32(pb, pkt->size);
  99. avio_write(pb, pkt->data, pkt->size);
  100. smc->duration = FFMAX(smc->duration, pkt->pts + pkt->duration);
  101. return 0;
  102. }
  103. static int smjpeg_write_trailer(AVFormatContext *s)
  104. {
  105. SMJPEGMuxContext *smc = s->priv_data;
  106. AVIOContext *pb = s->pb;
  107. int64_t currentpos;
  108. if (pb->seekable) {
  109. currentpos = avio_tell(pb);
  110. avio_seek(pb, 12, SEEK_SET);
  111. avio_wb32(pb, smc->duration);
  112. avio_seek(pb, currentpos, SEEK_SET);
  113. }
  114. avio_wl32(pb, SMJPEG_DONE);
  115. return 0;
  116. }
  117. AVOutputFormat ff_smjpeg_muxer = {
  118. .name = "smjpeg",
  119. .long_name = NULL_IF_CONFIG_SMALL("Loki SDL MJPEG"),
  120. .priv_data_size = sizeof(SMJPEGMuxContext),
  121. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  122. .video_codec = AV_CODEC_ID_MJPEG,
  123. .write_header = smjpeg_write_header,
  124. .write_packet = smjpeg_write_packet,
  125. .write_trailer = smjpeg_write_trailer,
  126. .flags = AVFMT_GLOBALHEADER | AVFMT_TS_NONSTRICT,
  127. .codec_tag = (const AVCodecTag *const []){ ff_codec_smjpeg_video_tags, ff_codec_smjpeg_audio_tags, 0 },
  128. };