• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * MxPEG clip file demuxer
3  * Copyright (c) 2010 Anatoly Nenashev
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 #include "libavutil/channel_layout.h"
23 #include "libavutil/internal.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavcodec/mjpeg.h"
26 #include "avformat.h"
27 #include "internal.h"
28 #include "avio.h"
29 
30 #define DEFAULT_PACKET_SIZE 1024
31 #define OVERREAD_SIZE 3
32 
33 typedef struct MXGContext {
34     uint8_t *buffer;
35     uint8_t *buffer_ptr;
36     uint8_t *soi_ptr;
37     unsigned int buffer_size;
38     int64_t dts;
39     unsigned int cache_size;
40 } MXGContext;
41 
mxg_read_header(AVFormatContext * s)42 static int mxg_read_header(AVFormatContext *s)
43 {
44     AVStream *video_st, *audio_st;
45     MXGContext *mxg = s->priv_data;
46 
47     /* video parameters will be extracted from the compressed bitstream */
48     video_st = avformat_new_stream(s, NULL);
49     if (!video_st)
50         return AVERROR(ENOMEM);
51     video_st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
52     video_st->codecpar->codec_id = AV_CODEC_ID_MXPEG;
53     avpriv_set_pts_info(video_st, 64, 1, 1000000);
54 
55     audio_st = avformat_new_stream(s, NULL);
56     if (!audio_st)
57         return AVERROR(ENOMEM);
58     audio_st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
59     audio_st->codecpar->codec_id = AV_CODEC_ID_PCM_ALAW;
60     audio_st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
61     audio_st->codecpar->sample_rate = 8000;
62     audio_st->codecpar->bits_per_coded_sample = 8;
63     audio_st->codecpar->block_align = 1;
64     avpriv_set_pts_info(audio_st, 64, 1, 1000000);
65 
66     mxg->soi_ptr = mxg->buffer_ptr = mxg->buffer = 0;
67     mxg->buffer_size = 0;
68     mxg->dts = AV_NOPTS_VALUE;
69     mxg->cache_size = 0;
70 
71     return 0;
72 }
73 
mxg_find_startmarker(uint8_t * p,uint8_t * end)74 static uint8_t* mxg_find_startmarker(uint8_t *p, uint8_t *end)
75 {
76     for (; p < end - 3; p += 4) {
77         uint32_t x = AV_RN32(p);
78 
79         if (x & (~(x+0x01010101)) & 0x80808080) {
80             if (p[0] == 0xff) {
81                 return p;
82             } else if (p[1] == 0xff) {
83                 return p+1;
84             } else if (p[2] == 0xff) {
85                 return p+2;
86             } else if (p[3] == 0xff) {
87                 return p+3;
88             }
89         }
90     }
91 
92     for (; p < end; ++p) {
93         if (*p == 0xff) return p;
94     }
95 
96     return end;
97 }
98 
mxg_update_cache(AVFormatContext * s,unsigned int cache_size)99 static int mxg_update_cache(AVFormatContext *s, unsigned int cache_size)
100 {
101     MXGContext *mxg = s->priv_data;
102     unsigned int current_pos = mxg->buffer_ptr - mxg->buffer;
103     unsigned int soi_pos;
104     uint8_t *buffer;
105     int ret;
106 
107     /* reallocate internal buffer */
108     if (current_pos > current_pos + cache_size)
109         return AVERROR(ENOMEM);
110     soi_pos = mxg->soi_ptr - mxg->buffer;
111     buffer = av_fast_realloc(mxg->buffer, &mxg->buffer_size,
112                              current_pos + cache_size +
113                              AV_INPUT_BUFFER_PADDING_SIZE);
114     if (!buffer)
115         return AVERROR(ENOMEM);
116     mxg->buffer = buffer;
117     mxg->buffer_ptr = mxg->buffer + current_pos;
118     if (mxg->soi_ptr) mxg->soi_ptr = mxg->buffer + soi_pos;
119 
120     /* get data */
121     ret = avio_read(s->pb, mxg->buffer_ptr + mxg->cache_size,
122                      cache_size - mxg->cache_size);
123     if (ret < 0)
124         return ret;
125 
126     mxg->cache_size += ret;
127 
128     return ret;
129 }
130 
mxg_read_packet(AVFormatContext * s,AVPacket * pkt)131 static int mxg_read_packet(AVFormatContext *s, AVPacket *pkt)
132 {
133     int ret;
134     unsigned int size;
135     uint8_t *startmarker_ptr, *end, *search_end, marker;
136     MXGContext *mxg = s->priv_data;
137 
138     while (!avio_feof(s->pb) && !s->pb->error){
139         if (mxg->cache_size <= OVERREAD_SIZE) {
140             /* update internal buffer */
141             ret = mxg_update_cache(s, DEFAULT_PACKET_SIZE + OVERREAD_SIZE);
142             if (ret < 0)
143                 return ret;
144         }
145         end = mxg->buffer_ptr + mxg->cache_size;
146 
147         /* find start marker - 0xff */
148         if (mxg->cache_size > OVERREAD_SIZE) {
149             search_end = end - OVERREAD_SIZE;
150             startmarker_ptr = mxg_find_startmarker(mxg->buffer_ptr, search_end);
151         } else {
152             search_end = end;
153             startmarker_ptr = mxg_find_startmarker(mxg->buffer_ptr, search_end);
154             if (startmarker_ptr >= search_end - 1 ||
155                 *(startmarker_ptr + 1) != EOI) break;
156         }
157 
158         if (startmarker_ptr != search_end) { /* start marker found */
159             marker = *(startmarker_ptr + 1);
160             mxg->buffer_ptr = startmarker_ptr + 2;
161             mxg->cache_size = end - mxg->buffer_ptr;
162 
163             if (marker == SOI) {
164                 mxg->soi_ptr = startmarker_ptr;
165             } else if (marker == EOI) {
166                 if (!mxg->soi_ptr) {
167                     av_log(s, AV_LOG_WARNING, "Found EOI before SOI, skipping\n");
168                     continue;
169                 }
170 
171                 size = mxg->buffer_ptr - mxg->soi_ptr;
172                 ret = av_new_packet(pkt, size);
173                 if (ret < 0)
174                     return ret;
175                 memcpy(pkt->data, mxg->soi_ptr, size);
176 
177                 pkt->pts = pkt->dts = mxg->dts;
178                 pkt->stream_index = 0;
179 
180                 if (mxg->soi_ptr - mxg->buffer > mxg->cache_size) {
181                     if (mxg->cache_size > 0) {
182                         memmove(mxg->buffer, mxg->buffer_ptr, mxg->cache_size);
183                     }
184 
185                     mxg->buffer_ptr = mxg->buffer;
186                 }
187                 mxg->soi_ptr = 0;
188 
189                 return pkt->size;
190             } else if ( (SOF0 <= marker && marker <= SOF15) ||
191                         (SOS  <= marker && marker <= COM) ) {
192                 /* all other markers that start marker segment also contain
193                    length value (see specification for JPEG Annex B.1) */
194                 size = AV_RB16(mxg->buffer_ptr);
195                 if (size < 2)
196                     return AVERROR(EINVAL);
197 
198                 if (mxg->cache_size < size) {
199                     ret = mxg_update_cache(s, size);
200                     if (ret < 0)
201                         return ret;
202                     startmarker_ptr = mxg->buffer_ptr - 2;
203                     mxg->cache_size = 0;
204                 } else {
205                     mxg->cache_size -= size;
206                 }
207 
208                 mxg->buffer_ptr += size;
209 
210                 if (marker == APP13 && size >= 16) { /* audio data */
211                     ret = av_new_packet(pkt, size - 14);
212                     if (ret < 0)
213                         return ret;
214                     memcpy(pkt->data, startmarker_ptr + 16, size - 14);
215 
216                     /* time (GMT) of first sample in usec since 1970, little-endian */
217                     pkt->pts = pkt->dts = AV_RL64(startmarker_ptr + 8);
218                     pkt->stream_index = 1;
219 
220                     if (startmarker_ptr - mxg->buffer > mxg->cache_size) {
221                         if (mxg->cache_size > 0) {
222                             memcpy(mxg->buffer, mxg->buffer_ptr, mxg->cache_size);
223                         }
224                         mxg->buffer_ptr = mxg->buffer;
225                     }
226 
227                     return pkt->size;
228                 } else if (marker == COM && size >= 18 &&
229                            !strncmp(startmarker_ptr + 4, "MXF", 3)) {
230                     /* time (GMT) of video frame in usec since 1970, little-endian */
231                     mxg->dts = AV_RL64(startmarker_ptr + 12);
232                 }
233             }
234         } else {
235             /* start marker not found */
236             mxg->buffer_ptr = search_end;
237             mxg->cache_size = OVERREAD_SIZE;
238         }
239     }
240 
241     return AVERROR_EOF;
242 }
243 
mxg_close(struct AVFormatContext * s)244 static int mxg_close(struct AVFormatContext *s)
245 {
246     MXGContext *mxg = s->priv_data;
247     av_freep(&mxg->buffer);
248     return 0;
249 }
250 
251 const AVInputFormat ff_mxg_demuxer = {
252     .name           = "mxg",
253     .long_name      = NULL_IF_CONFIG_SMALL("MxPEG clip"),
254     .priv_data_size = sizeof(MXGContext),
255     .read_header    = mxg_read_header,
256     .read_packet    = mxg_read_packet,
257     .read_close     = mxg_close,
258     .extensions     = "mxg",
259 };
260