smjpegenc.c 4.6 KB

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