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 {
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->codec->codec_type = AVMEDIA_TYPE_VIDEO;
52  video_st->codec->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->codec->codec_type = AVMEDIA_TYPE_AUDIO;
59  audio_st->codec->codec_id = AV_CODEC_ID_PCM_ALAW;
60  audio_st->codec->channels = 1;
62  audio_st->codec->sample_rate = 8000;
63  audio_st->codec->bits_per_coded_sample = 8;
64  audio_st->codec->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 #if FF_API_DESTRUCT_PACKET
174  pkt->destruct = NULL;
176 #endif
177  pkt->buf = NULL;
178  pkt->size = mxg->buffer_ptr - mxg->soi_ptr;
179  pkt->data = mxg->soi_ptr;
180 
181  if (mxg->soi_ptr - mxg->buffer > mxg->cache_size) {
182  if (mxg->cache_size > 0) {
183  memcpy(mxg->buffer, mxg->buffer_ptr, mxg->cache_size);
184  }
185 
186  mxg->buffer_ptr = mxg->buffer;
187  }
188  mxg->soi_ptr = 0;
189 
190  return pkt->size;
191  } else if ( (SOF0 <= marker && marker <= SOF15) ||
192  (SOS <= marker && marker <= COM) ) {
193  /* all other markers that start marker segment also contain
194  length value (see specification for JPEG Annex B.1) */
195  size = AV_RB16(mxg->buffer_ptr);
196  if (size < 2)
197  return AVERROR(EINVAL);
198 
199  if (mxg->cache_size < size) {
200  ret = mxg_update_cache(s, size);
201  if (ret < 0)
202  return ret;
203  startmarker_ptr = mxg->buffer_ptr - 2;
204  mxg->cache_size = 0;
205  } else {
206  mxg->cache_size -= size;
207  }
208 
209  mxg->buffer_ptr += size;
210 
211  if (marker == APP13 && size >= 16) { /* audio data */
212  /* time (GMT) of first sample in usec since 1970, little-endian */
213  pkt->pts = pkt->dts = AV_RL64(startmarker_ptr + 8);
214  pkt->stream_index = 1;
215 #if FF_API_DESTRUCT_PACKET
217  pkt->destruct = NULL;
219 #endif
220  pkt->buf = NULL;
221  pkt->size = size - 14;
222  pkt->data = startmarker_ptr + 16;
223 
224  if (startmarker_ptr - mxg->buffer > mxg->cache_size) {
225  if (mxg->cache_size > 0) {
226  memcpy(mxg->buffer, mxg->buffer_ptr, mxg->cache_size);
227  }
228  mxg->buffer_ptr = mxg->buffer;
229  }
230 
231  return pkt->size;
232  } else if (marker == COM && size >= 18 &&
233  !strncmp(startmarker_ptr + 4, "MXF", 3)) {
234  /* time (GMT) of video frame in usec since 1970, little-endian */
235  mxg->dts = AV_RL64(startmarker_ptr + 12);
236  }
237  }
238  } else {
239  /* start marker not found */
240  mxg->buffer_ptr = search_end;
241  mxg->cache_size = OVERREAD_SIZE;
242  }
243  }
244 
245  return AVERROR_EOF;
246 }
247 
248 static int mxg_close(struct AVFormatContext *s)
249 {
250  MXGContext *mxg = s->priv_data;
251  av_freep(&mxg->buffer);
252  return 0;
253 }
254 
256  .name = "mxg",
257  .long_name = NULL_IF_CONFIG_SMALL("MxPEG clip"),
258  .priv_data_size = sizeof(MXGContext),
262  .extensions = "mxg",
263 };
Definition: mjpeg.h:116
Buffered I/O operations.
int size
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
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:2829
int size
Definition: avcodec.h:974
Definition: mjpeg.h:75
MJPEG encoder and decoder.
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:1844
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:198
Format I/O context.
Definition: avformat.h:922
#define AV_RL64
Definition: intreadwrite.h:173
uint8_t
attribute_deprecated void(* destruct)(struct AVPacket *)
Definition: avcodec.h:994
Definition: mjpeg.h:97
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2521
uint8_t * data
Definition: avcodec.h:973
#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
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2523
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:452
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:369
#define AV_RB16
Definition: intreadwrite.h:53
#define AVERROR(e)
Definition: error.h:43
Definition: mjpeg.h:44
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:956
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
uint8_t * soi_ptr
Definition: mxg.c:36
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1868
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
static int mxg_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mxg.c:130
common internal API header
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:531
audio channel layout utility functions
int64_t dts
Definition: mxg.c:38
Definition: mjpeg.h:77
uint8_t * buffer_ptr
Definition: mxg.c:35
#define DEFAULT_PACKET_SIZE
Definition: mxg.c:30
AVInputFormat ff_mxg_demuxer
Definition: mxg.c:255
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
Definition: mjpeg.h:76
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:544
Stream structure.
Definition: avformat.h:699
NULL
Definition: eval.c:55
enum AVMediaType codec_type
Definition: avcodec.h:1058
enum AVCodecID codec_id
Definition: avcodec.h:1067
int sample_rate
samples per second
Definition: avcodec.h:1807
AVIOContext * pb
I/O context.
Definition: avformat.h:964
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
Definition: mjpeg.h:59
int error
contains the error code or 0 if no error happened
Definition: avio.h:102
Main libavformat public API header.
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:76
static int mxg_close(struct AVFormatContext *s)
Definition: mxg.c:248
int eof_reached
true if eof reached
Definition: avio.h:96
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:77
int channels
number of audio channels
Definition: avcodec.h:1808
void * priv_data
Format private data.
Definition: avformat.h:950
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:972
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
unsigned int buffer_size
Definition: mxg.c:37
int stream_index
Definition: avcodec.h:975
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
Definition: avcodec.h:950
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:966
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:228
static int mxg_read_header(AVFormatContext *s)
Definition: mxg.c:42