FFmpeg
 All Data Structures Namespaces 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/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 {
37  unsigned int buffer_size;
38  int64_t dts;
39  unsigned int cache_size;
40 } MXGContext;
41 
43 {
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);
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);
60  audio_st->codecpar->channels = 1;
62  audio_st->codecpar->sample_rate = 8000;
63  audio_st->codecpar->bits_per_coded_sample = 8;
64  audio_st->codecpar->block_align = 1;
65  avpriv_set_pts_info(audio_st, 64, 1, 1000000);
66 
67  mxg->soi_ptr = mxg->buffer_ptr = mxg->buffer = 0;
68  mxg->buffer_size = 0;
69  mxg->dts = AV_NOPTS_VALUE;
70  mxg->cache_size = 0;
71 
72  return 0;
73 }
74 
76 {
77  for (; p < end - 3; p += 4) {
78  uint32_t x = AV_RN32(p);
79 
80  if (x & (~(x+0x01010101)) & 0x80808080) {
81  if (p[0] == 0xff) {
82  return p;
83  } else if (p[1] == 0xff) {
84  return p+1;
85  } else if (p[2] == 0xff) {
86  return p+2;
87  } else if (p[3] == 0xff) {
88  return p+3;
89  }
90  }
91  }
92 
93  for (; p < end; ++p) {
94  if (*p == 0xff) return p;
95  }
96 
97  return end;
98 }
99 
100 static int mxg_update_cache(AVFormatContext *s, unsigned int cache_size)
101 {
102  MXGContext *mxg = s->priv_data;
103  unsigned int current_pos = mxg->buffer_ptr - mxg->buffer;
104  unsigned int soi_pos;
105  uint8_t *buffer;
106  int ret;
107 
108  /* reallocate internal buffer */
109  if (current_pos > current_pos + cache_size)
110  return AVERROR(ENOMEM);
111  soi_pos = mxg->soi_ptr - mxg->buffer;
112  buffer = av_fast_realloc(mxg->buffer, &mxg->buffer_size,
113  current_pos + cache_size +
115  if (!buffer)
116  return AVERROR(ENOMEM);
117  mxg->buffer = buffer;
118  mxg->buffer_ptr = mxg->buffer + current_pos;
119  if (mxg->soi_ptr) mxg->soi_ptr = mxg->buffer + soi_pos;
120 
121  /* get data */
122  ret = avio_read(s->pb, mxg->buffer_ptr + mxg->cache_size,
123  cache_size - mxg->cache_size);
124  if (ret < 0)
125  return ret;
126 
127  mxg->cache_size += ret;
128 
129  return ret;
130 }
131 
133 {
134  int ret;
135  unsigned int size;
136  uint8_t *startmarker_ptr, *end, *search_end, marker;
137  MXGContext *mxg = s->priv_data;
138 
139  while (!avio_feof(s->pb) && !s->pb->error){
140  if (mxg->cache_size <= OVERREAD_SIZE) {
141  /* update internal buffer */
143  if (ret < 0)
144  return ret;
145  }
146  end = mxg->buffer_ptr + mxg->cache_size;
147 
148  /* find start marker - 0xff */
149  if (mxg->cache_size > OVERREAD_SIZE) {
150  search_end = end - OVERREAD_SIZE;
151  startmarker_ptr = mxg_find_startmarker(mxg->buffer_ptr, search_end);
152  } else {
153  search_end = end;
154  startmarker_ptr = mxg_find_startmarker(mxg->buffer_ptr, search_end);
155  if (startmarker_ptr >= search_end - 1 ||
156  *(startmarker_ptr + 1) != EOI) break;
157  }
158 
159  if (startmarker_ptr != search_end) { /* start marker found */
160  marker = *(startmarker_ptr + 1);
161  mxg->buffer_ptr = startmarker_ptr + 2;
162  mxg->cache_size = end - mxg->buffer_ptr;
163 
164  if (marker == SOI) {
165  mxg->soi_ptr = startmarker_ptr;
166  } else if (marker == EOI) {
167  if (!mxg->soi_ptr) {
168  av_log(s, AV_LOG_WARNING, "Found EOI before SOI, skipping\n");
169  continue;
170  }
171 
172  pkt->pts = pkt->dts = mxg->dts;
173  pkt->stream_index = 0;
174  pkt->buf = NULL;
175  pkt->size = mxg->buffer_ptr - mxg->soi_ptr;
176  pkt->data = mxg->soi_ptr;
177 
178  if (mxg->soi_ptr - mxg->buffer > mxg->cache_size) {
179  if (mxg->cache_size > 0) {
180  memmove(mxg->buffer, mxg->buffer_ptr, mxg->cache_size);
181  }
182 
183  mxg->buffer_ptr = mxg->buffer;
184  }
185  mxg->soi_ptr = 0;
186 
187  return pkt->size;
188  } else if ( (SOF0 <= marker && marker <= SOF15) ||
189  (SOS <= marker && marker <= COM) ) {
190  /* all other markers that start marker segment also contain
191  length value (see specification for JPEG Annex B.1) */
192  size = AV_RB16(mxg->buffer_ptr);
193  if (size < 2)
194  return AVERROR(EINVAL);
195 
196  if (mxg->cache_size < size) {
197  ret = mxg_update_cache(s, size);
198  if (ret < 0)
199  return ret;
200  startmarker_ptr = mxg->buffer_ptr - 2;
201  mxg->cache_size = 0;
202  } else {
203  mxg->cache_size -= size;
204  }
205 
206  mxg->buffer_ptr += size;
207 
208  if (marker == APP13 && size >= 16) { /* audio data */
209  /* time (GMT) of first sample in usec since 1970, little-endian */
210  pkt->pts = pkt->dts = AV_RL64(startmarker_ptr + 8);
211  pkt->stream_index = 1;
212  pkt->buf = NULL;
213  pkt->size = size - 14;
214  pkt->data = startmarker_ptr + 16;
215 
216  if (startmarker_ptr - mxg->buffer > mxg->cache_size) {
217  if (mxg->cache_size > 0) {
218  memcpy(mxg->buffer, mxg->buffer_ptr, mxg->cache_size);
219  }
220  mxg->buffer_ptr = mxg->buffer;
221  }
222 
223  return pkt->size;
224  } else if (marker == COM && size >= 18 &&
225  !strncmp(startmarker_ptr + 4, "MXF", 3)) {
226  /* time (GMT) of video frame in usec since 1970, little-endian */
227  mxg->dts = AV_RL64(startmarker_ptr + 12);
228  }
229  }
230  } else {
231  /* start marker not found */
232  mxg->buffer_ptr = search_end;
233  mxg->cache_size = OVERREAD_SIZE;
234  }
235  }
236 
237  return AVERROR_EOF;
238 }
239 
240 static int mxg_close(struct AVFormatContext *s)
241 {
242  MXGContext *mxg = s->priv_data;
243  av_freep(&mxg->buffer);
244  return 0;
245 }
246 
248  .name = "mxg",
249  .long_name = NULL_IF_CONFIG_SMALL("MxPEG clip"),
250  .priv_data_size = sizeof(MXGContext),
254  .extensions = "mxg",
255 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
Buffered I/O operations.
Definition: mjpeg.h:71
Definition: mjpeg.h:111
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
static uint8_t * mxg_find_startmarker(uint8_t *p, uint8_t *end)
Definition: mxg.c:75
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4405
AVStream * video_st
Definition: movenc-test.c:59
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3915
int size
Definition: avcodec.h:1578
uint64_t_TMPL AV_RL64
Definition: bytestream.h:87
static AVPacket pkt
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:87
MJPEG encoder and decoder.
Format I/O context.
Definition: avformat.h:1319
Definition: mjpeg.h:72
uint8_t
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4043
Definition: mjpeg.h:54
uint8_t * data
Definition: avcodec.h:1577
#define AVERROR_EOF
End of file.
Definition: error.h:55
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
unsigned int cache_size
Definition: mxg.c:39
ptrdiff_t size
Definition: opengl_enc.c:101
uint64_t channel_layout
Audio only.
Definition: avcodec.h:4017
#define av_log(a,...)
uint8_t * buffer
Definition: mxg.c:34
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:550
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given block if it is not large enough, otherwise do nothing.
Definition: mem.c:480
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3911
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1560
uint8_t * soi_ptr
Definition: mxg.c:36
Definition: mjpeg.h:39
Definition: mjpeg.h:70
static int mxg_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mxg.c:132
common internal API header
int block_align
Audio only.
Definition: avcodec.h:4032
audio channel layout utility functions
int64_t dts
Definition: mxg.c:38
uint8_t * buffer_ptr
Definition: mxg.c:35
#define DEFAULT_PACKET_SIZE
Definition: mxg.c:30
AVInputFormat ff_mxg_demuxer
Definition: mxg.c:247
static int mxg_update_cache(AVFormatContext *s, unsigned int cache_size)
Definition: mxg.c:100
Definition: mxg.c:33
#define OVERREAD_SIZE
Definition: mxg.c:31
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:638
Stream structure.
Definition: avformat.h:876
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVIOContext * pb
I/O context.
Definition: avformat.h:1361
int error
contains the error code or 0 if no error happened
Definition: avio.h:192
#define AV_RN32(p)
Definition: intreadwrite.h:364
int sample_rate
Audio only.
Definition: avcodec.h:4025
Main libavformat public API header.
static int mxg_close(struct AVFormatContext *s)
Definition: mxg.c:240
AVStream * audio_st
Definition: movenc-test.c:59
Definition: mjpeg.h:92
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:728
void * priv_data
Format private data.
Definition: avformat.h:1347
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: avcodec.h:3957
int channels
Audio only.
Definition: avcodec.h:4021
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1576
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:660
unsigned int buffer_size
Definition: mxg.c:37
AVCodecParameters * codecpar
Definition: avformat.h:1006
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:311
int stream_index
Definition: avcodec.h:1579
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
Definition: avcodec.h:1554
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1570
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:240
GLuint buffer
Definition: opengl_enc.c:102
static int mxg_read_header(AVFormatContext *s)
Definition: mxg.c:42