libnut.c
Go to the documentation of this file.
1 /*
2  * NUT (de)muxing via libnut
3  * copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
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 
28 #include "avformat.h"
29 #include "internal.h"
30 #include "riff.h"
31 #include <libnut.h>
32 
33 #define ID_STRING "nut/multimedia container"
34 #define ID_LENGTH (strlen(ID_STRING) + 1)
35 
36 typedef struct {
37  nut_context_tt * nut;
38  nut_stream_header_tt * s;
39 } NUTContext;
40 
41 static const AVCodecTag nut_tags[] = {
42  { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
43  { CODEC_ID_MP3, MKTAG('m', 'p', '3', ' ') },
44  { CODEC_ID_VORBIS, MKTAG('v', 'r', 'b', 's') },
45  { 0, 0 },
46 };
47 
48 #if CONFIG_LIBNUT_MUXER
49 static int av_write(void * h, size_t len, const uint8_t * buf) {
50  AVIOContext * bc = h;
51  avio_write(bc, buf, len);
52  //avio_flush(bc);
53  return len;
54 }
55 
56 static int nut_write_header(AVFormatContext * avf) {
57  NUTContext * priv = avf->priv_data;
58  AVIOContext * bc = avf->pb;
59  nut_muxer_opts_tt mopts = {
60  .output = {
61  .priv = bc,
62  .write = av_write,
63  },
64  .alloc = { av_malloc, av_realloc, av_free },
65  .write_index = 1,
66  .realtime_stream = 0,
67  .max_distance = 32768,
68  .fti = NULL,
69  };
70  nut_stream_header_tt * s;
71  int i;
72 
73  priv->s = s = av_mallocz((avf->nb_streams + 1) * sizeof*s);
74 
75  for (i = 0; i < avf->nb_streams; i++) {
76  AVCodecContext * codec = avf->streams[i]->codec;
77  int j;
78  int fourcc = 0;
79  int num, denom, ssize;
80 
81  s[i].type = codec->codec_type == AVMEDIA_TYPE_VIDEO ? NUT_VIDEO_CLASS : NUT_AUDIO_CLASS;
82 
83  if (codec->codec_tag) fourcc = codec->codec_tag;
84  else fourcc = ff_codec_get_tag(nut_tags, codec->codec_id);
85 
86  if (!fourcc) {
89  }
90 
91  s[i].fourcc_len = 4;
92  s[i].fourcc = av_malloc(s[i].fourcc_len);
93  for (j = 0; j < s[i].fourcc_len; j++) s[i].fourcc[j] = (fourcc >> (j*8)) & 0xFF;
94 
95  ff_parse_specific_params(codec, &num, &ssize, &denom);
96  avpriv_set_pts_info(avf->streams[i], 60, denom, num);
97 
98  s[i].time_base.num = denom;
99  s[i].time_base.den = num;
100 
101  s[i].fixed_fps = 0;
102  s[i].decode_delay = codec->has_b_frames;
103  s[i].codec_specific_len = codec->extradata_size;
104  s[i].codec_specific = codec->extradata;
105 
106  if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
107  s[i].width = codec->width;
108  s[i].height = codec->height;
109  s[i].sample_width = 0;
110  s[i].sample_height = 0;
111  s[i].colorspace_type = 0;
112  } else {
113  s[i].samplerate_num = codec->sample_rate;
114  s[i].samplerate_denom = 1;
115  s[i].channel_count = codec->channels;
116  }
117  }
118 
119  s[avf->nb_streams].type = -1;
120  priv->nut = nut_muxer_init(&mopts, s, NULL);
121 
122  return 0;
123 }
124 
125 static int nut_write_packet(AVFormatContext * avf, AVPacket * pkt) {
126  NUTContext * priv = avf->priv_data;
127  nut_packet_tt p;
128 
129  p.len = pkt->size;
130  p.stream = pkt->stream_index;
131  p.pts = pkt->pts;
132  p.flags = pkt->flags & AV_PKT_FLAG_KEY ? NUT_FLAG_KEY : 0;
133  p.next_pts = 0;
134 
135  nut_write_frame_reorder(priv->nut, &p, pkt->data);
136 
137  return 0;
138 }
139 
140 static int nut_write_trailer(AVFormatContext * avf) {
141  AVIOContext * bc = avf->pb;
142  NUTContext * priv = avf->priv_data;
143  int i;
144 
145  nut_muxer_uninit_reorder(priv->nut);
146  avio_flush(bc);
147 
148  for(i = 0; priv->s[i].type != -1; i++ ) av_freep(&priv->s[i].fourcc);
149  av_freep(&priv->s);
150 
151  return 0;
152 }
153 
154 AVOutputFormat ff_libnut_muxer = {
155  .name = "libnut",
156  .long_name = "nut format",
157  .mime_type = "video/x-nut",
158  .extensions = "nut",
159  .priv_data_size = sizeof(NUTContext),
160  .audio_codec = CODEC_ID_VORBIS,
161  .video_codec = CODEC_ID_MPEG4,
166 };
167 #endif /* CONFIG_LIBNUT_MUXER */
168 
169 static int nut_probe(AVProbeData *p) {
170  if (!memcmp(p->buf, ID_STRING, ID_LENGTH)) return AVPROBE_SCORE_MAX;
171 
172  return 0;
173 }
174 
175 static size_t av_read(void * h, size_t len, uint8_t * buf) {
176  AVIOContext * bc = h;
177  return avio_read(bc, buf, len);
178 }
179 
180 static off_t av_seek(void * h, long long pos, int whence) {
181  AVIOContext * bc = h;
182  if (whence == SEEK_END) {
183  pos = avio_size(bc) + pos;
184  whence = SEEK_SET;
185  }
186  return avio_seek(bc, pos, whence);
187 }
188 
190  NUTContext * priv = avf->priv_data;
191  AVIOContext * bc = avf->pb;
192  nut_demuxer_opts_tt dopts = {
193  .input = {
194  .priv = bc,
195  .seek = av_seek,
196  .read = av_read,
197  .eof = NULL,
198  .file_pos = 0,
199  },
200  .alloc = { av_malloc, av_realloc, av_free },
201  .read_index = 1,
202  .cache_syncpoints = 1,
203  };
204  nut_context_tt * nut = priv->nut = nut_demuxer_init(&dopts);
205  nut_stream_header_tt * s;
206  int ret, i;
207 
208  if ((ret = nut_read_headers(nut, &s, NULL))) {
209  av_log(avf, AV_LOG_ERROR, " NUT error: %s\n", nut_error(ret));
210  nut_demuxer_uninit(nut);
211  return -1;
212  }
213 
214  priv->s = s;
215 
216  for (i = 0; s[i].type != -1 && i < 2; i++) {
217  AVStream * st = avformat_new_stream(avf, NULL);
218  int j;
219 
220  for (j = 0; j < s[i].fourcc_len && j < 8; j++) st->codec->codec_tag |= s[i].fourcc[j]<<(j*8);
221 
222  st->codec->has_b_frames = s[i].decode_delay;
223 
224  st->codec->extradata_size = s[i].codec_specific_len;
225  if (st->codec->extradata_size) {
227  memcpy(st->codec->extradata, s[i].codec_specific, st->codec->extradata_size);
228  }
229 
230  avpriv_set_pts_info(avf->streams[i], 60, s[i].time_base.num, s[i].time_base.den);
231  st->start_time = 0;
232  st->duration = s[i].max_pts;
233 
234  st->codec->codec_id = ff_codec_get_id(nut_tags, st->codec->codec_tag);
235 
236  switch(s[i].type) {
237  case NUT_AUDIO_CLASS:
240 
241  st->codec->channels = s[i].channel_count;
242  st->codec->sample_rate = s[i].samplerate_num / s[i].samplerate_denom;
243  break;
244  case NUT_VIDEO_CLASS:
247 
248  st->codec->width = s[i].width;
249  st->codec->height = s[i].height;
250  st->sample_aspect_ratio.num = s[i].sample_width;
251  st->sample_aspect_ratio.den = s[i].sample_height;
252  break;
253  }
254  if (st->codec->codec_id == CODEC_ID_NONE) av_log(avf, AV_LOG_ERROR, "Unknown codec?!\n");
255  }
256 
257  return 0;
258 }
259 
260 static int nut_read_packet(AVFormatContext * avf, AVPacket * pkt) {
261  NUTContext * priv = avf->priv_data;
262  nut_packet_tt pd;
263  int ret;
264 
265  ret = nut_read_next_packet(priv->nut, &pd);
266 
267  if (ret || av_new_packet(pkt, pd.len) < 0) {
268  if (ret != NUT_ERR_EOF)
269  av_log(avf, AV_LOG_ERROR, " NUT error: %s\n", nut_error(ret));
270  return -1;
271  }
272 
273  if (pd.flags & NUT_FLAG_KEY) pkt->flags |= AV_PKT_FLAG_KEY;
274  pkt->pts = pd.pts;
275  pkt->stream_index = pd.stream;
276  pkt->pos = avio_tell(avf->pb);
277 
278  ret = nut_read_frame(priv->nut, &pd.len, pkt->data);
279 
280  return ret;
281 }
282 
283 static int nut_read_seek(AVFormatContext * avf, int stream_index, int64_t target_ts, int flags) {
284  NUTContext * priv = avf->priv_data;
285  int active_streams[] = { stream_index, -1 };
286  double time_pos = target_ts * priv->s[stream_index].time_base.num / (double)priv->s[stream_index].time_base.den;
287 
288  if (nut_seek(priv->nut, time_pos, 2*!(flags & AVSEEK_FLAG_BACKWARD), active_streams)) return -1;
289 
290  return 0;
291 }
292 
294  NUTContext * priv = s->priv_data;
295 
296  nut_demuxer_uninit(priv->nut);
297 
298  return 0;
299 }
300 
302  .name = "libnut",
303  .long_name = NULL_IF_CONFIG_SMALL("NUT format"),
304  .priv_data_size = sizeof(NUTContext),
310  .extensions = "nut",
311 };
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:1618
static int write_header(AVFormatContext *s)
Definition: assenc.c:28
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:154
Bytestream IO Context.
Definition: avio.h:68
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:281
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:933
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the pts for a given stream.
Definition: utils.c:3828
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:60
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: assenc.c:58
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:148
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:716
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:909
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:211
int64_t(* seek)(void *opaque, int64_t offset, int whence)
Definition: avio.h:95
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:117
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:954
static off_t av_seek(void *h, long long pos, int whence)
Definition: libnut.c:180
#define ID_LENGTH
Definition: libnut.c:34
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:147
Format I/O context.
Definition: avformat.h:863
static int nut_read_header(AVFormatContext *avf, AVFormatParameters *ap)
Definition: libnut.c:189
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1387
AVStream ** streams
Definition: avformat.h:908
static int read_header(FFV1Context *f)
Definition: ffv1.c:1513
static int nut_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: nutenc.c:694
uint8_t * data
Definition: avcodec.h:908
static int flags
Definition: log.c:34
void ff_parse_specific_params(AVCodecContext *stream, int *au_rate, int *au_ssize, int *au_scale)
Definition: riff.c:623
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:140
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:492
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:190
enum CodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:2206
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:67
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:652
#define ID_STRING
Definition: libnut.c:33
nut_stream_header_tt * s
Definition: libnut.c:38
static int nut_read_seek(AVFormatContext *avf, int stream_index, int64_t target_ts, int flags)
Definition: libnut.c:283
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1745
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:137
static int nut_write_header(AVFormatContext *s)
Definition: nutenc.c:581
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2776
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:289
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:914
static const AVCodecTag nut_tags[]
Definition: libnut.c:41
AVCodecContext * codec
codec context
Definition: avformat.h:623
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:341
unsigned int nb_streams
A list of all streams in the file.
Definition: avformat.h:907
int void avio_flush(AVIOContext *s)
Definition: aviobuf.c:205
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:31
int width
picture width / height.
Definition: avcodec.h:1408
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:374
const char * name
Definition: avformat.h:390
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
static int read_probe(AVProbeData *p)
Definition: img2.c:185
Stream structure.
Definition: avformat.h:620
NULL
Definition: eval.c:50
enum AVMediaType codec_type
Definition: avcodec.h:1574
int sample_rate
samples per second
Definition: avcodec.h:1456
AVIOContext * pb
Definition: avformat.h:896
main external API structure.
Definition: avcodec.h:1329
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1590
int extradata_size
Definition: avcodec.h:1388
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:109
static size_t av_read(void *h, size_t len, uint8_t *buf)
Definition: libnut.c:175
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:64
static int nut_write_trailer(AVFormatContext *s)
Definition: nutenc.c:848
This structure contains the data a format has to probe a file.
Definition: avformat.h:339
static int nut_probe(AVProbeData *p)
Definition: libnut.c:169
static int nut_read_close(AVFormatContext *s)
Definition: libnut.c:293
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum CodecID id)
Definition: utils.c:2196
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:684
#define AVPROBE_SCORE_MAX
maximum score, half of that is used for file-extension-based detection
Definition: avformat.h:345
Main libavformat public API header.
int64_t start_time
Decoding: pts of the first frame of the stream, in stream time base.
Definition: avformat.h:677
int den
denominator
Definition: rational.h:45
AVInputFormat ff_libnut_demuxer
Definition: libnut.c:301
int len
int channels
number of audio channels
Definition: avcodec.h:1457
void * priv_data
Format private data.
Definition: avformat.h:883
nut_context_tt * nut
Definition: libnut.c:37
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:460
int stream_index
Definition: avcodec.h:910
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:652
#define MKTAG(a, b, c, d)
Definition: common.h:231
static int nut_read_packet(AVFormatContext *avf, AVPacket *pkt)
Definition: libnut.c:260
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:901
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:336
enum CodecID codec_id
Definition: avcodec.h:1575