Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 = *(uint32_t*)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  int ret;
106 
107  /* reallocate internal buffer */
108  if (current_pos > current_pos + cache_size)
109  return AVERROR(ENOMEM);
110  if (mxg->soi_ptr) soi_pos = mxg->soi_ptr - mxg->buffer;
111  mxg->buffer = av_fast_realloc(mxg->buffer, &mxg->buffer_size,
112  current_pos + cache_size +
114  if (!mxg->buffer)
115  return AVERROR(ENOMEM);
116  mxg->buffer_ptr = mxg->buffer + current_pos;
117  if (mxg->soi_ptr) mxg->soi_ptr = mxg->buffer + soi_pos;
118 
119  /* get data */
120  ret = avio_read(s->pb, mxg->buffer_ptr + mxg->cache_size,
121  cache_size - mxg->cache_size);
122  if (ret < 0)
123  return ret;
124 
125  mxg->cache_size += ret;
126 
127  return ret;
128 }
129 
131 {
132  int ret;
133  unsigned int size;
134  uint8_t *startmarker_ptr, *end, *search_end, marker;
135  MXGContext *mxg = s->priv_data;
136 
137  while (!s->pb->eof_reached && !s->pb->error){
138  if (mxg->cache_size <= OVERREAD_SIZE) {
139  /* update internal buffer */
141  if (ret < 0)
142  return ret;
143  }
144  end = mxg->buffer_ptr + mxg->cache_size;
145 
146  /* find start marker - 0xff */
147  if (mxg->cache_size > OVERREAD_SIZE) {
148  search_end = end - OVERREAD_SIZE;
149  startmarker_ptr = mxg_find_startmarker(mxg->buffer_ptr, search_end);
150  } else {
151  search_end = end;
152  startmarker_ptr = mxg_find_startmarker(mxg->buffer_ptr, search_end);
153  if (startmarker_ptr >= search_end - 1 ||
154  *(startmarker_ptr + 1) != EOI) break;
155  }
156 
157  if (startmarker_ptr != search_end) { /* start marker found */
158  marker = *(startmarker_ptr + 1);
159  mxg->buffer_ptr = startmarker_ptr + 2;
160  mxg->cache_size = end - mxg->buffer_ptr;
161 
162  if (marker == SOI) {
163  mxg->soi_ptr = startmarker_ptr;
164  } else if (marker == EOI) {
165  if (!mxg->soi_ptr) {
166  av_log(s, AV_LOG_WARNING, "Found EOI before SOI, skipping\n");
167  continue;
168  }
169 
170  pkt->pts = pkt->dts = mxg->dts;
171  pkt->stream_index = 0;
172  pkt->buf = NULL;
173  pkt->size = mxg->buffer_ptr - mxg->soi_ptr;
174  pkt->data = mxg->soi_ptr;
175 
176  if (mxg->soi_ptr - mxg->buffer > mxg->cache_size) {
177  if (mxg->cache_size > 0) {
178  memcpy(mxg->buffer, mxg->buffer_ptr, mxg->cache_size);
179  }
180 
181  mxg->buffer_ptr = mxg->buffer;
182  }
183  mxg->soi_ptr = 0;
184 
185  return pkt->size;
186  } else if ( (SOF0 <= marker && marker <= SOF15) ||
187  (SOS <= marker && marker <= COM) ) {
188  /* all other markers that start marker segment also contain
189  length value (see specification for JPEG Annex B.1) */
190  size = AV_RB16(mxg->buffer_ptr);
191  if (size < 2)
192  return AVERROR(EINVAL);
193 
194  if (mxg->cache_size < size) {
195  ret = mxg_update_cache(s, size);
196  if (ret < 0)
197  return ret;
198  startmarker_ptr = mxg->buffer_ptr - 2;
199  mxg->cache_size = 0;
200  } else {
201  mxg->cache_size -= size;
202  }
203 
204  mxg->buffer_ptr += size;
205 
206  if (marker == APP13 && size >= 16) { /* audio data */
207  /* time (GMT) of first sample in usec since 1970, little-endian */
208  pkt->pts = pkt->dts = AV_RL64(startmarker_ptr + 8);
209  pkt->stream_index = 1;
210  pkt->buf = NULL;
211  pkt->size = size - 14;
212  pkt->data = startmarker_ptr + 16;
213 
214  if (startmarker_ptr - mxg->buffer > mxg->cache_size) {
215  if (mxg->cache_size > 0) {
216  memcpy(mxg->buffer, mxg->buffer_ptr, mxg->cache_size);
217  }
218  mxg->buffer_ptr = mxg->buffer;
219  }
220 
221  return pkt->size;
222  } else if (marker == COM && size >= 18 &&
223  !strncmp(startmarker_ptr + 4, "MXF", 3)) {
224  /* time (GMT) of video frame in usec since 1970, little-endian */
225  mxg->dts = AV_RL64(startmarker_ptr + 12);
226  }
227  }
228  } else {
229  /* start marker not found */
230  mxg->buffer_ptr = search_end;
231  mxg->cache_size = OVERREAD_SIZE;
232  }
233  }
234 
235  return AVERROR_EOF;
236 }
237 
238 static int mxg_close(struct AVFormatContext *s)
239 {
240  MXGContext *mxg = s->priv_data;
241  av_freep(&mxg->buffer);
242  return 0;
243 }
244 
246  .name = "mxg",
247  .long_name = NULL_IF_CONFIG_SMALL("MxPEG clip"),
248  .priv_data_size = sizeof(MXGContext),
252  .extensions = "mxg",
253 };
Buffered I/O operations.
int size
Definition: mjpeg.h:71
Definition: mjpeg.h:111
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
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:2986
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
int size
Definition: avcodec.h:1347
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
MJPEG encoder and decoder.
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
Format I/O context.
Definition: avformat.h:940
#define AV_RL64
Definition: intreadwrite.h:173
Definition: mjpeg.h:72
uint8_t
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2648
Definition: mjpeg.h:54
uint8_t * data
Definition: avcodec.h:1346
#define AVERROR_EOF
End of file.
Definition: error.h:51
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
unsigned int cache_size
Definition: mxg.c:39
uint64_t channel_layout
Audio only.
Definition: avcodec.h:3556
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:545
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:375
#define AV_RB16
Definition: intreadwrite.h:53
#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:148
AVStream * video_st
Definition: movenc.c:59
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3479
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1329
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:130
common internal API header
int block_align
Audio only.
Definition: avcodec.h:3571
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:245
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:546
Stream structure.
Definition: avformat.h:705
NULL
Definition: eval.c:55
AVIOContext * pb
I/O context.
Definition: avformat.h:982
AVStream * audio_st
Definition: movenc.c:59
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
int error
contains the error code or 0 if no error happened
Definition: avio.h:138
int sample_rate
Audio only.
Definition: avcodec.h:3564
Main libavformat public API header.
static int mxg_close(struct AVFormatContext *s)
Definition: mxg.c:238
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:638
int eof_reached
true if eof reached
Definition: avio.h:132
void * priv_data
Format private data.
Definition: avformat.h:968
int bits_per_coded_sample
Definition: avcodec.h:3514
int channels
Audio only.
Definition: avcodec.h:3560
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1345
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:529
unsigned int buffer_size
Definition: mxg.c:37
AVCodecParameters * codecpar
Definition: avformat.h:831
int stream_index
Definition: avcodec.h:1348
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
Definition: avcodec.h:1323
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1339
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:235
static int mxg_read_header(AVFormatContext *s)
Definition: mxg.c:42