| Line | Branch | Exec | Source | 
|---|---|---|---|
| 1 | /* | ||
| 2 | * RAW demuxers | ||
| 3 | * Copyright (c) 2001 Fabrice Bellard | ||
| 4 | * Copyright (c) 2005 Alex Beregszaszi | ||
| 5 | * | ||
| 6 | * This file is part of FFmpeg. | ||
| 7 | * | ||
| 8 | * FFmpeg is free software; you can redistribute it and/or | ||
| 9 | * modify it under the terms of the GNU Lesser General Public | ||
| 10 | * License as published by the Free Software Foundation; either | ||
| 11 | * version 2.1 of the License, or (at your option) any later version. | ||
| 12 | * | ||
| 13 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 16 | * Lesser General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU Lesser General Public | ||
| 19 | * License along with FFmpeg; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 21 | */ | ||
| 22 | |||
| 23 | #include "config_components.h" | ||
| 24 | |||
| 25 | #include "avformat.h" | ||
| 26 | #include "demux.h" | ||
| 27 | #include "internal.h" | ||
| 28 | #include "rawdec.h" | ||
| 29 | #include "libavutil/opt.h" | ||
| 30 | |||
| 31 | #define RAW_PACKET_SIZE 1024 | ||
| 32 | |||
| 33 | 546901 | int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt) | |
| 34 | { | ||
| 35 | 546901 | FFRawDemuxerContext *raw = s->priv_data; | |
| 36 | int ret, size; | ||
| 37 | |||
| 38 | 546901 | size = raw->raw_packet_size; | |
| 39 | |||
| 40 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 546901 times. 
         | 
      546901 | if ((ret = av_new_packet(pkt, size)) < 0) | 
| 41 | ✗ | return ret; | |
| 42 | |||
| 43 | 546901 | pkt->pos= avio_tell(s->pb); | |
| 44 | 546901 | pkt->stream_index = 0; | |
| 45 | 546901 | ret = avio_read_partial(s->pb, pkt->data, size); | |
| 46 | 
        2/2✓ Branch 0 taken 1849 times. 
          ✓ Branch 1 taken 545052 times. 
         | 
      546901 | if (ret < 0) { | 
| 47 | 1849 | av_packet_unref(pkt); | |
| 48 | 1849 | return ret; | |
| 49 | } | ||
| 50 | 545052 | av_shrink_packet(pkt, ret); | |
| 51 | 545052 | return ret; | |
| 52 | } | ||
| 53 | |||
| 54 | 42 | int ff_raw_audio_read_header(AVFormatContext *s) | |
| 55 | { | ||
| 56 | 42 | AVStream *st = avformat_new_stream(s, NULL); | |
| 57 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 42 times. 
         | 
      42 | if (!st) | 
| 58 | ✗ | return AVERROR(ENOMEM); | |
| 59 | 42 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
| 60 | 42 | st->codecpar->codec_id = ffifmt(s->iformat)->raw_codec_id; | |
| 61 | 42 | ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW; | |
| 62 | 42 | st->start_time = 0; | |
| 63 | /* the parameters will be extracted from the compressed bitstream */ | ||
| 64 | |||
| 65 | 42 | return 0; | |
| 66 | } | ||
| 67 | |||
| 68 | /* MPEG-1/H.263 input */ | ||
| 69 | 612 | int ff_raw_video_read_header(AVFormatContext *s) | |
| 70 | { | ||
| 71 | AVStream *st; | ||
| 72 | FFStream *sti; | ||
| 73 | 612 | FFRawVideoDemuxerContext *s1 = s->priv_data; | |
| 74 | 612 | int ret = 0; | |
| 75 | |||
| 76 | |||
| 77 | 612 | st = avformat_new_stream(s, NULL); | |
| 78 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 612 times. 
         | 
      612 | if (!st) { | 
| 79 | ✗ | ret = AVERROR(ENOMEM); | |
| 80 | ✗ | goto fail; | |
| 81 | } | ||
| 82 | 612 | sti = ffstream(st); | |
| 83 | |||
| 84 | 612 | st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; | |
| 85 | 612 | st->codecpar->codec_id = ffifmt(s->iformat)->raw_codec_id; | |
| 86 | 612 | sti->need_parsing = AVSTREAM_PARSE_FULL_RAW; | |
| 87 | |||
| 88 | 612 | st->avg_frame_rate = s1->framerate; | |
| 89 | 612 | avpriv_set_pts_info(st, 64, 1, 1200000); | |
| 90 | |||
| 91 | 612 | fail: | |
| 92 | 612 | return ret; | |
| 93 | } | ||
| 94 | |||
| 95 | ✗ | int ff_raw_subtitle_read_header(AVFormatContext *s) | |
| 96 | { | ||
| 97 | ✗ | AVStream *st = avformat_new_stream(s, NULL); | |
| 98 | ✗ | if (!st) | |
| 99 | ✗ | return AVERROR(ENOMEM); | |
| 100 | ✗ | st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE; | |
| 101 | ✗ | st->codecpar->codec_id = ffifmt(s->iformat)->raw_codec_id; | |
| 102 | ✗ | st->start_time = 0; | |
| 103 | ✗ | return 0; | |
| 104 | } | ||
| 105 | |||
| 106 | ✗ | static int raw_data_read_header(AVFormatContext *s) | |
| 107 | { | ||
| 108 | ✗ | AVStream *st = avformat_new_stream(s, NULL); | |
| 109 | ✗ | if (!st) | |
| 110 | ✗ | return AVERROR(ENOMEM); | |
| 111 | ✗ | st->codecpar->codec_type = AVMEDIA_TYPE_DATA; | |
| 112 | ✗ | st->codecpar->codec_id = ffifmt(s->iformat)->raw_codec_id; | |
| 113 | ✗ | st->start_time = 0; | |
| 114 | ✗ | return 0; | |
| 115 | } | ||
| 116 | |||
| 117 | /* Note: Do not forget to add new entries to the Makefile as well. */ | ||
| 118 | |||
| 119 | #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x) | ||
| 120 | #define DEC AV_OPT_FLAG_DECODING_PARAM | ||
| 121 | static const AVOption rawvideo_options[] = { | ||
| 122 | { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC}, | ||
| 123 | { "raw_packet_size", "", OFFSET(raw_packet_size), AV_OPT_TYPE_INT, {.i64 = RAW_PACKET_SIZE }, 1, INT_MAX, DEC}, | ||
| 124 | { NULL }, | ||
| 125 | }; | ||
| 126 | #undef OFFSET | ||
| 127 | |||
| 128 | const AVClass ff_rawvideo_demuxer_class = { | ||
| 129 | .class_name = "generic raw video demuxer", | ||
| 130 | .item_name = av_default_item_name, | ||
| 131 | .option = rawvideo_options, | ||
| 132 | .version = LIBAVUTIL_VERSION_INT, | ||
| 133 | }; | ||
| 134 | |||
| 135 | #define OFFSET(x) offsetof(FFRawDemuxerContext, x) | ||
| 136 | static const AVOption raw_options[] = { | ||
| 137 | { "raw_packet_size", "", OFFSET(raw_packet_size), AV_OPT_TYPE_INT, {.i64 = RAW_PACKET_SIZE }, 1, INT_MAX, DEC}, | ||
| 138 | { NULL }, | ||
| 139 | }; | ||
| 140 | |||
| 141 | const AVClass ff_raw_demuxer_class = { | ||
| 142 | .class_name = "generic raw demuxer", | ||
| 143 | .item_name = av_default_item_name, | ||
| 144 | .option = raw_options, | ||
| 145 | .version = LIBAVUTIL_VERSION_INT, | ||
| 146 | }; | ||
| 147 | |||
| 148 | #if CONFIG_DATA_DEMUXER | ||
| 149 | const FFInputFormat ff_data_demuxer = { | ||
| 150 | .p.name = "data", | ||
| 151 | .p.long_name = NULL_IF_CONFIG_SMALL("raw data"), | ||
| 152 | .p.flags = AVFMT_NOTIMESTAMPS, | ||
| 153 | .p.priv_class = &ff_raw_demuxer_class, | ||
| 154 | .read_header = raw_data_read_header, | ||
| 155 | .read_packet = ff_raw_read_partial_packet, | ||
| 156 | .raw_codec_id = AV_CODEC_ID_NONE, | ||
| 157 | .priv_data_size = sizeof(FFRawDemuxerContext),\ | ||
| 158 | }; | ||
| 159 | #endif | ||
| 160 | |||
| 161 | #if CONFIG_MJPEG_DEMUXER | ||
| 162 | 7282 | static int mjpeg_probe(const AVProbeData *p) | |
| 163 | { | ||
| 164 | int i; | ||
| 165 | 7282 | int state = -1; | |
| 166 | 7282 | int nb_invalid = 0; | |
| 167 | 7282 | int nb_frames = 0; | |
| 168 | |||
| 169 | 
        2/2✓ Branch 0 taken 391842975 times. 
          ✓ Branch 1 taken 7282 times. 
         | 
      391850257 | for (i = 0; i < p->buf_size - 1; i++) { | 
| 170 | int c; | ||
| 171 | 
        2/2✓ Branch 0 taken 385335144 times. 
          ✓ Branch 1 taken 6507831 times. 
         | 
      391842975 | if (p->buf[i] != 0xFF) | 
| 172 | 385335144 | continue; | |
| 173 | 6507831 | c = p->buf[i+1]; | |
| 174 | 
        5/5✓ Branch 0 taken 101051 times. 
          ✓ Branch 1 taken 196132 times. 
          ✓ Branch 2 taken 42134 times. 
          ✓ Branch 3 taken 46614 times. 
          ✓ Branch 4 taken 6121900 times. 
         | 
      6507831 | switch (c) { | 
| 175 | 101051 | case 0xD8: | |
| 176 | 101051 | state = 0xD8; | |
| 177 | 101051 | break; | |
| 178 | 196132 | case 0xC0: | |
| 179 | case 0xC1: | ||
| 180 | case 0xC2: | ||
| 181 | case 0xC3: | ||
| 182 | case 0xC5: | ||
| 183 | case 0xC6: | ||
| 184 | case 0xC7: | ||
| 185 | case 0xF7: | ||
| 186 | 
        2/2✓ Branch 0 taken 40316 times. 
          ✓ Branch 1 taken 155816 times. 
         | 
      196132 | if (state == 0xD8) { | 
| 187 | 40316 | state = 0xC0; | |
| 188 | } else | ||
| 189 | 155816 | nb_invalid++; | |
| 190 | 196132 | break; | |
| 191 | 42134 | case 0xDA: | |
| 192 | 
        2/2✓ Branch 0 taken 13094 times. 
          ✓ Branch 1 taken 29040 times. 
         | 
      42134 | if (state == 0xC0) { | 
| 193 | 13094 | state = 0xDA; | |
| 194 | } else | ||
| 195 | 29040 | nb_invalid++; | |
| 196 | 42134 | break; | |
| 197 | 46614 | case 0xD9: | |
| 198 | 
        2/2✓ Branch 0 taken 5212 times. 
          ✓ Branch 1 taken 41402 times. 
         | 
      46614 | if (state == 0xDA) { | 
| 199 | 5212 | state = 0xD9; | |
| 200 | 5212 | nb_frames++; | |
| 201 | } else | ||
| 202 | 41402 | nb_invalid++; | |
| 203 | 46614 | break; | |
| 204 | 6121900 | default: | |
| 205 | 
        4/4✓ Branch 0 taken 5882264 times. 
          ✓ Branch 1 taken 239636 times. 
          ✓ Branch 2 taken 2319628 times. 
          ✓ Branch 3 taken 3562636 times. 
         | 
      6121900 | if ( (c >= 0x02 && c <= 0xBF) | 
| 206 | 
        2/2✓ Branch 0 taken 22466 times. 
          ✓ Branch 1 taken 2536798 times. 
         | 
      2559264 | || c == 0xC8) { | 
| 207 | 3585102 | nb_invalid++; | |
| 208 | } | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 7282 times. 
         | 
      7282 | if (nb_invalid*4 + 1 < nb_frames) { | 
| 213 | static const char ct_jpeg[] = "\r\nContent-Type: image/jpeg\r\n"; | ||
| 214 | int i; | ||
| 215 | |||
| 216 | ✗ | for (i=0; i<FFMIN(p->buf_size - (int)sizeof(ct_jpeg), 100); i++) | |
| 217 | ✗ | if (!memcmp(p->buf + i, ct_jpeg, sizeof(ct_jpeg) - 1)) | |
| 218 | ✗ | return AVPROBE_SCORE_EXTENSION; | |
| 219 | |||
| 220 | ✗ | if (nb_invalid == 0 && nb_frames > 2) | |
| 221 | ✗ | return AVPROBE_SCORE_EXTENSION / 2; | |
| 222 | ✗ | return AVPROBE_SCORE_EXTENSION / 4; | |
| 223 | } | ||
| 224 | 
        3/4✓ Branch 0 taken 1492 times. 
          ✓ Branch 1 taken 5790 times. 
          ✗ Branch 2 not taken. 
          ✓ Branch 3 taken 1492 times. 
         | 
      7282 | if (!nb_invalid && nb_frames) | 
| 225 | ✗ | return AVPROBE_SCORE_EXTENSION / 4; | |
| 226 | |||
| 227 | 7282 | return 0; | |
| 228 | } | ||
| 229 | |||
| 230 | FF_DEF_RAWVIDEO_DEMUXER2(mjpeg, "raw MJPEG video", mjpeg_probe, "mjpg,mjpeg,mpo", AV_CODEC_ID_MJPEG, AVFMT_GENERIC_INDEX|AVFMT_NOTIMESTAMPS) | ||
| 231 | #endif | ||
| 232 |