00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "avformat.h"
00028 #include "internal.h"
00029 #include "smjpeg.h"
00030
00031 typedef struct SMJPEGMuxContext {
00032 uint32_t duration;
00033 } SMJPEGMuxContext;
00034
00035 static int smjpeg_write_header(AVFormatContext *s)
00036 {
00037 AVDictionaryEntry *t = NULL;
00038 AVIOContext *pb = s->pb;
00039 int n, tag;
00040
00041 if (s->nb_streams > 2) {
00042 av_log(s, AV_LOG_ERROR, "more than >2 streams are not supported\n");
00043 return AVERROR(EINVAL);
00044 }
00045 avio_write(pb, SMJPEG_MAGIC, 8);
00046 avio_wb32(pb, 0);
00047 avio_wb32(pb, 0);
00048
00049 while ((t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX))) {
00050 avio_wl32(pb, SMJPEG_TXT);
00051 avio_wb32(pb, strlen(t->key) + strlen(t->value) + 3);
00052 avio_write(pb, t->key, strlen(t->key));
00053 avio_write(pb, " = ", 3);
00054 avio_write(pb, t->value, strlen(t->value));
00055 }
00056
00057 for (n = 0; n < s->nb_streams; n++) {
00058 AVStream *st = s->streams[n];
00059 AVCodecContext *codec = st->codec;
00060 if (codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00061 tag = ff_codec_get_tag(ff_codec_smjpeg_audio_tags, codec->codec_id);
00062 if (!tag) {
00063 av_log(s, AV_LOG_ERROR, "unsupported audio codec\n");
00064 return AVERROR(EINVAL);
00065 }
00066 avio_wl32(pb, SMJPEG_SND);
00067 avio_wb32(pb, 8);
00068 avio_wb16(pb, codec->sample_rate);
00069 avio_w8(pb, codec->bits_per_coded_sample);
00070 avio_w8(pb, codec->channels);
00071 avio_wl32(pb, tag);
00072 avpriv_set_pts_info(st, 32, 1, 1000);
00073 } else if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
00074 tag = ff_codec_get_tag(ff_codec_smjpeg_video_tags, codec->codec_id);
00075 if (!tag) {
00076 av_log(s, AV_LOG_ERROR, "unsupported video codec\n");
00077 return AVERROR(EINVAL);
00078 }
00079 avio_wl32(pb, SMJPEG_VID);
00080 avio_wb32(pb, 12);
00081 avio_wb32(pb, 0);
00082 avio_wb16(pb, codec->width);
00083 avio_wb16(pb, codec->height);
00084 avio_wl32(pb, tag);
00085 avpriv_set_pts_info(st, 32, 1, 1000);
00086 }
00087 }
00088
00089 avio_wl32(pb, SMJPEG_HEND);
00090 avio_flush(pb);
00091
00092 return 0;
00093 }
00094
00095 static int smjpeg_write_packet(AVFormatContext *s, AVPacket *pkt)
00096 {
00097 SMJPEGMuxContext *smc = s->priv_data;
00098 AVIOContext *pb = s->pb;
00099 AVStream *st = s->streams[pkt->stream_index];
00100 AVCodecContext *codec = st->codec;
00101
00102 if (codec->codec_type == AVMEDIA_TYPE_AUDIO)
00103 avio_wl32(pb, SMJPEG_SNDD);
00104 else if (codec->codec_type == AVMEDIA_TYPE_VIDEO)
00105 avio_wl32(pb, SMJPEG_VIDD);
00106 else
00107 return 0;
00108
00109 avio_wb32(pb, pkt->pts);
00110 avio_wb32(pb, pkt->size);
00111 avio_write(pb, pkt->data, pkt->size);
00112 avio_flush(pb);
00113
00114 smc->duration = FFMAX(smc->duration, pkt->pts + pkt->duration);
00115 return 0;
00116 }
00117
00118 static int smjpeg_write_trailer(AVFormatContext *s)
00119 {
00120 SMJPEGMuxContext *smc = s->priv_data;
00121 AVIOContext *pb = s->pb;
00122 int64_t currentpos;
00123
00124 if (pb->seekable) {
00125 currentpos = avio_tell(pb);
00126 avio_seek(pb, 12, SEEK_SET);
00127 avio_wb32(pb, smc->duration);
00128 avio_seek(pb, currentpos, SEEK_SET);
00129 }
00130
00131 avio_wl32(pb, SMJPEG_DONE);
00132
00133 return 0;
00134 }
00135
00136 AVOutputFormat ff_smjpeg_muxer = {
00137 .name = "smjpeg",
00138 .long_name = NULL_IF_CONFIG_SMALL("Loki SDL MJPEG"),
00139 .priv_data_size = sizeof(SMJPEGMuxContext),
00140 .audio_codec = AV_CODEC_ID_PCM_S16LE,
00141 .video_codec = AV_CODEC_ID_MJPEG,
00142 .write_header = smjpeg_write_header,
00143 .write_packet = smjpeg_write_packet,
00144 .write_trailer = smjpeg_write_trailer,
00145 .flags = AVFMT_GLOBALHEADER | AVFMT_TS_NONSTRICT,
00146 .codec_tag = (const AVCodecTag *const []){ ff_codec_smjpeg_video_tags, ff_codec_smjpeg_audio_tags, 0 },
00147 };