FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mxg.c
Go to the documentation of this file.
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 
23 #include "libavutil/intreadwrite.h"
24 #include "libavcodec/mjpeg.h"
25 #include "avformat.h"
26 #include "internal.h"
27 #include "avio.h"
28 
29 #define DEFAULT_PACKET_SIZE 1024
30 #define OVERREAD_SIZE 3
31 
32 typedef struct MXGContext {
36  unsigned int buffer_size;
37  int64_t dts;
38  unsigned int cache_size;
39 } MXGContext;
40 
42 {
43  AVStream *video_st, *audio_st;
44  MXGContext *mxg = s->priv_data;
45 
46  /* video parameters will be extracted from the compressed bitstream */
47  video_st = avformat_new_stream(s, NULL);
48  if (!video_st)
49  return AVERROR(ENOMEM);
50  video_st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
51  video_st->codec->codec_id = AV_CODEC_ID_MXPEG;
52  avpriv_set_pts_info(video_st, 64, 1, 1000000);
53 
54  audio_st = avformat_new_stream(s, NULL);
55  if (!audio_st)
56  return AVERROR(ENOMEM);
57  audio_st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
58  audio_st->codec->codec_id = AV_CODEC_ID_PCM_ALAW;
59  audio_st->codec->channels = 1;
61  audio_st->codec->sample_rate = 8000;
62  audio_st->codec->bits_per_coded_sample = 8;
63  audio_st->codec->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 
75 {
76  for (; p < end - 3; p += 4) {
77  uint32_t x = *(uint32_t*)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 
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  int ret;
105 
106  /* reallocate internal buffer */
107  if (current_pos > current_pos + cache_size)
108  return AVERROR(ENOMEM);
109  soi_pos = mxg->soi_ptr - mxg->buffer;
110  mxg->buffer = av_fast_realloc(mxg->buffer, &mxg->buffer_size,
111  current_pos + cache_size +
113  if (!mxg->buffer)
114  return AVERROR(ENOMEM);
115  mxg->buffer_ptr = mxg->buffer + current_pos;
116  if (mxg->soi_ptr) mxg->soi_ptr = mxg->buffer + soi_pos;
117 
118  /* get data */
119  ret = avio_read(s->pb, mxg->buffer_ptr + mxg->cache_size,
120  cache_size - mxg->cache_size);
121  if (ret < 0)
122  return ret;
123 
124  mxg->cache_size += ret;
125 
126  return ret;
127 }
128 
130 {
131  int ret;
132  unsigned int size;
133  uint8_t *startmarker_ptr, *end, *search_end, marker;
134  MXGContext *mxg = s->priv_data;
135 
136  while (!url_feof(s->pb) && !s->pb->error){
137  if (mxg->cache_size <= OVERREAD_SIZE) {
138  /* update internal buffer */
140  if (ret < 0)
141  return ret;
142  }
143  end = mxg->buffer_ptr + mxg->cache_size;
144 
145  /* find start marker - 0xff */
146  if (mxg->cache_size > OVERREAD_SIZE) {
147  search_end = end - OVERREAD_SIZE;
148  startmarker_ptr = mxg_find_startmarker(mxg->buffer_ptr, search_end);
149  } else {
150  search_end = end;
151  startmarker_ptr = mxg_find_startmarker(mxg->buffer_ptr, search_end);
152  if (startmarker_ptr >= search_end - 1 ||
153  *(startmarker_ptr + 1) != EOI) break;
154  }
155 
156  if (startmarker_ptr != search_end) { /* start marker found */
157  marker = *(startmarker_ptr + 1);
158  mxg->buffer_ptr = startmarker_ptr + 2;
159  mxg->cache_size = end - mxg->buffer_ptr;
160 
161  if (marker == SOI) {
162  mxg->soi_ptr = startmarker_ptr;
163  } else if (marker == EOI) {
164  if (!mxg->soi_ptr) {
165  av_log(s, AV_LOG_WARNING, "Found EOI before SOI, skipping\n");
166  continue;
167  }
168 
169  pkt->pts = pkt->dts = mxg->dts;
170  pkt->stream_index = 0;
171  pkt->destruct = NULL;
172  pkt->size = mxg->buffer_ptr - mxg->soi_ptr;
173  pkt->data = mxg->soi_ptr;
174 
175  if (mxg->soi_ptr - mxg->buffer > mxg->cache_size) {
176  if (mxg->cache_size > 0) {
177  memcpy(mxg->buffer, mxg->buffer_ptr, mxg->cache_size);
178  }
179 
180  mxg->buffer_ptr = mxg->buffer;
181  }
182  mxg->soi_ptr = 0;
183 
184  return pkt->size;
185  } else if ( (SOF0 <= marker && marker <= SOF15) ||
186  (SOS <= marker && marker <= COM) ) {
187  /* all other markers that start marker segment also contain
188  length value (see specification for JPEG Annex B.1) */
189  size = AV_RB16(mxg->buffer_ptr);
190  if (size < 2)
191  return AVERROR(EINVAL);
192 
193  if (mxg->cache_size < size) {
194  ret = mxg_update_cache(s, size);
195  if (ret < 0)
196  return ret;
197  startmarker_ptr = mxg->buffer_ptr - 2;
198  mxg->cache_size = 0;
199  } else {
200  mxg->cache_size -= size;
201  }
202 
203  mxg->buffer_ptr += size;
204 
205  if (marker == APP13 && size >= 16) { /* audio data */
206  /* time (GMT) of first sample in usec since 1970, little-endian */
207  pkt->pts = pkt->dts = AV_RL64(startmarker_ptr + 8);
208  pkt->stream_index = 1;
209  pkt->destruct = NULL;
210  pkt->size = size - 14;
211  pkt->data = startmarker_ptr + 16;
212 
213  if (startmarker_ptr - mxg->buffer > mxg->cache_size) {
214  if (mxg->cache_size > 0) {
215  memcpy(mxg->buffer, mxg->buffer_ptr, mxg->cache_size);
216  }
217  mxg->buffer_ptr = mxg->buffer;
218  }
219 
220  return pkt->size;
221  } else if (marker == COM && size >= 18 &&
222  !strncmp(startmarker_ptr + 4, "MXF", 3)) {
223  /* time (GMT) of video frame in usec since 1970, little-endian */
224  mxg->dts = AV_RL64(startmarker_ptr + 12);
225  }
226  }
227  } else {
228  /* start marker not found */
229  mxg->buffer_ptr = search_end;
230  mxg->cache_size = OVERREAD_SIZE;
231  }
232  }
233 
234  return AVERROR_EOF;
235 }
236 
237 static int mxg_close(struct AVFormatContext *s)
238 {
239  MXGContext *mxg = s->priv_data;
240  av_freep(&mxg->buffer);
241  return 0;
242 }
243 
245  .name = "mxg",
246  .long_name = NULL_IF_CONFIG_SMALL("MxPEG clip"),
247  .priv_data_size = sizeof(MXGContext),
251  .extensions = "mxg",
252 };