| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | /** | ||
| 23 | * @file | ||
| 24 | * This is a muxer for Loki SDL Motion JPEG files | ||
| 25 | */ | ||
| 26 | |||
| 27 | #include "avformat.h" | ||
| 28 | #include "internal.h" | ||
| 29 | #include "mux.h" | ||
| 30 | #include "smjpeg.h" | ||
| 31 | |||
| 32 | typedef struct SMJPEGMuxContext { | ||
| 33 | uint32_t duration; | ||
| 34 | } SMJPEGMuxContext; | ||
| 35 | |||
| 36 | 1 | static int smjpeg_write_header(AVFormatContext *s) | |
| 37 | { | ||
| 38 | 1 | const AVDictionaryEntry *t = NULL; | |
| 39 | 1 | AVIOContext *pb = s->pb; | |
| 40 | int n, tag; | ||
| 41 | |||
| 42 | 1 | avio_write(pb, SMJPEG_MAGIC, 8); | |
| 43 | 1 | avio_wb32(pb, 0); | |
| 44 | 1 | avio_wb32(pb, 0); | |
| 45 | |||
| 46 | 1 | ff_standardize_creation_time(s); | |
| 47 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
|
2 | while ((t = av_dict_iterate(s->metadata, t))) { |
| 48 | 1 | avio_wl32(pb, SMJPEG_TXT); | |
| 49 | 1 | avio_wb32(pb, strlen(t->key) + strlen(t->value) + 3); | |
| 50 | 1 | avio_write(pb, t->key, strlen(t->key)); | |
| 51 | 1 | avio_write(pb, " = ", 3); | |
| 52 | 1 | avio_write(pb, t->value, strlen(t->value)); | |
| 53 | } | ||
| 54 | |||
| 55 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | for (n = 0; n < s->nb_streams; n++) { |
| 56 | 2 | AVStream *st = s->streams[n]; | |
| 57 | 2 | AVCodecParameters *par = st->codecpar; | |
| 58 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (par->codec_type == AVMEDIA_TYPE_AUDIO) { |
| 59 | 1 | tag = ff_codec_get_tag(ff_codec_smjpeg_audio_tags, par->codec_id); | |
| 60 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!tag) { |
| 61 | ✗ | av_log(s, AV_LOG_ERROR, "unsupported audio codec\n"); | |
| 62 | ✗ | return AVERROR(EINVAL); | |
| 63 | } | ||
| 64 | 1 | avio_wl32(pb, SMJPEG_SND); | |
| 65 | 1 | avio_wb32(pb, 8); | |
| 66 | 1 | avio_wb16(pb, par->sample_rate); | |
| 67 | 1 | avio_w8(pb, par->bits_per_coded_sample); | |
| 68 | 1 | avio_w8(pb, par->ch_layout.nb_channels); | |
| 69 | 1 | avio_wl32(pb, tag); | |
| 70 | 1 | avpriv_set_pts_info(st, 32, 1, 1000); | |
| 71 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | } else if (par->codec_type == AVMEDIA_TYPE_VIDEO) { |
| 72 | 1 | tag = ff_codec_get_tag(ff_codec_smjpeg_video_tags, par->codec_id); | |
| 73 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!tag) { |
| 74 | ✗ | av_log(s, AV_LOG_ERROR, "unsupported video codec\n"); | |
| 75 | ✗ | return AVERROR(EINVAL); | |
| 76 | } | ||
| 77 | 1 | avio_wl32(pb, SMJPEG_VID); | |
| 78 | 1 | avio_wb32(pb, 12); | |
| 79 | 1 | avio_wb32(pb, 0); | |
| 80 | 1 | avio_wb16(pb, par->width); | |
| 81 | 1 | avio_wb16(pb, par->height); | |
| 82 | 1 | avio_wl32(pb, tag); | |
| 83 | 1 | avpriv_set_pts_info(st, 32, 1, 1000); | |
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | 1 | avio_wl32(pb, SMJPEG_HEND); | |
| 88 | |||
| 89 | 1 | return 0; | |
| 90 | } | ||
| 91 | |||
| 92 | 36 | static int smjpeg_write_packet(AVFormatContext *s, AVPacket *pkt) | |
| 93 | { | ||
| 94 | 36 | SMJPEGMuxContext *smc = s->priv_data; | |
| 95 | 36 | AVIOContext *pb = s->pb; | |
| 96 | 36 | AVStream *st = s->streams[pkt->stream_index]; | |
| 97 | 36 | AVCodecParameters *par = st->codecpar; | |
| 98 | |||
| 99 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 25 times.
|
36 | if (par->codec_type == AVMEDIA_TYPE_AUDIO) |
| 100 | 11 | avio_wl32(pb, SMJPEG_SNDD); | |
| 101 |
1/2✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
|
25 | else if (par->codec_type == AVMEDIA_TYPE_VIDEO) |
| 102 | 25 | avio_wl32(pb, SMJPEG_VIDD); | |
| 103 | else | ||
| 104 | ✗ | return 0; | |
| 105 | |||
| 106 | 36 | avio_wb32(pb, pkt->pts); | |
| 107 | 36 | avio_wb32(pb, pkt->size); | |
| 108 | 36 | avio_write(pb, pkt->data, pkt->size); | |
| 109 | |||
| 110 | 36 | smc->duration = FFMAX(smc->duration, pkt->pts + pkt->duration); | |
| 111 | 36 | return 0; | |
| 112 | } | ||
| 113 | |||
| 114 | 1 | static int smjpeg_write_trailer(AVFormatContext *s) | |
| 115 | { | ||
| 116 | 1 | SMJPEGMuxContext *smc = s->priv_data; | |
| 117 | 1 | AVIOContext *pb = s->pb; | |
| 118 | int64_t currentpos; | ||
| 119 | |||
| 120 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (pb->seekable & AVIO_SEEKABLE_NORMAL) { |
| 121 | 1 | currentpos = avio_tell(pb); | |
| 122 | 1 | avio_seek(pb, 12, SEEK_SET); | |
| 123 | 1 | avio_wb32(pb, smc->duration); | |
| 124 | 1 | avio_seek(pb, currentpos, SEEK_SET); | |
| 125 | } | ||
| 126 | |||
| 127 | 1 | avio_wl32(pb, SMJPEG_DONE); | |
| 128 | |||
| 129 | 1 | return 0; | |
| 130 | } | ||
| 131 | |||
| 132 | const FFOutputFormat ff_smjpeg_muxer = { | ||
| 133 | .p.name = "smjpeg", | ||
| 134 | .p.long_name = NULL_IF_CONFIG_SMALL("Loki SDL MJPEG"), | ||
| 135 | .priv_data_size = sizeof(SMJPEGMuxContext), | ||
| 136 | .p.audio_codec = AV_CODEC_ID_PCM_S16LE, | ||
| 137 | .p.video_codec = AV_CODEC_ID_MJPEG, | ||
| 138 | .p.subtitle_codec = AV_CODEC_ID_NONE, | ||
| 139 | .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH, | ||
| 140 | .write_header = smjpeg_write_header, | ||
| 141 | .write_packet = smjpeg_write_packet, | ||
| 142 | .write_trailer = smjpeg_write_trailer, | ||
| 143 | .p.flags = AVFMT_GLOBALHEADER | AVFMT_TS_NONSTRICT, | ||
| 144 | .p.codec_tag = (const AVCodecTag *const []){ ff_codec_smjpeg_video_tags, ff_codec_smjpeg_audio_tags, 0 }, | ||
| 145 | }; | ||
| 146 |