wvenc.c
Go to the documentation of this file.
1 /*
2  * WavPack muxer
3  * Copyright (c) 2012 Paul B Mahol
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 
22 #include "libavutil/intreadwrite.h"
23 #include "avformat.h"
24 #include "internal.h"
25 #include "avio_internal.h"
26 #include "apetag.h"
27 
28 #define WV_EXTRA_SIZE 12
29 #define WV_END_BLOCK 0x1000
30 
31 typedef struct{
32  uint32_t duration;
33 } WVMuxContext;
34 
36 {
37  AVCodecContext *codec = s->streams[0]->codec;
38 
39  if (s->nb_streams > 1) {
40  av_log(s, AV_LOG_ERROR, "only one stream is supported\n");
41  return AVERROR(EINVAL);
42  }
43  if (codec->codec_id != AV_CODEC_ID_WAVPACK) {
44  av_log(s, AV_LOG_ERROR, "unsupported codec\n");
45  return AVERROR(EINVAL);
46  }
47  if (codec->extradata_size > 0) {
48  avpriv_report_missing_feature(s, "remuxing from matroska container");
49  return AVERROR_PATCHWELCOME;
50  }
51  avpriv_set_pts_info(s->streams[0], 64, 1, codec->sample_rate);
52 
53  return 0;
54 }
55 
57 {
58  WVMuxContext *wc = s->priv_data;
59  AVCodecContext *codec = s->streams[0]->codec;
60  AVIOContext *pb = s->pb;
61  uint64_t size;
62  uint32_t flags;
63  uint32_t left = pkt->size;
64  uint8_t *ptr = pkt->data;
65  int off = codec->channels > 2 ? 4 : 0;
66 
67  /* FIXME: Simplify decoder/demuxer so bellow code can support midstream
68  * change of stream parameters */
69  wc->duration += pkt->duration;
70  ffio_wfourcc(pb, "wvpk");
71  if (off) {
72  size = AV_RL32(pkt->data);
73  if (size <= 12)
74  return AVERROR_INVALIDDATA;
75  size -= 12;
76  } else {
77  size = pkt->size;
78  }
79 
80  if (size + off > left)
81  return AVERROR_INVALIDDATA;
82 
83  avio_wl32(pb, size + 12);
84  avio_wl16(pb, 0x410);
85  avio_w8(pb, 0);
86  avio_w8(pb, 0);
87  avio_wl32(pb, -1);
88  avio_wl32(pb, pkt->pts);
89  ptr += off; left -= off;
90  flags = AV_RL32(ptr + 4);
91  avio_write(pb, ptr, size);
92  ptr += size; left -= size;
93 
94  while (!(flags & WV_END_BLOCK) &&
95  (left >= 4 + WV_EXTRA_SIZE)) {
96  ffio_wfourcc(pb, "wvpk");
97  size = AV_RL32(ptr);
98  ptr += 4; left -= 4;
99  if (size < 24 || size - 24 > left)
100  return AVERROR_INVALIDDATA;
101  avio_wl32(pb, size);
102  avio_wl16(pb, 0x410);
103  avio_w8(pb, 0);
104  avio_w8(pb, 0);
105  avio_wl32(pb, -1);
106  avio_wl32(pb, pkt->pts);
107  flags = AV_RL32(ptr + 4);
108  avio_write(pb, ptr, WV_EXTRA_SIZE);
109  ptr += WV_EXTRA_SIZE; left -= WV_EXTRA_SIZE;
110  avio_write(pb, ptr, size - 24);
111  ptr += size - 24; left -= size - 24;
112  }
113 
114  return 0;
115 }
116 
118 {
119  WVMuxContext *wc = s->priv_data;
120  AVIOContext *pb = s->pb;
121 
122  ff_ape_write(s);
123 
124  if (pb->seekable) {
125  avio_seek(pb, 12, SEEK_SET);
126  avio_wl32(pb, wc->duration);
127  avio_flush(pb);
128  }
129 
130  return 0;
131 }
132 
134  .name = "wv",
135  .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
136  .priv_data_size = sizeof(WVMuxContext),
137  .extensions = "wv",
138  .audio_codec = AV_CODEC_ID_WAVPACK,
139  .video_codec = AV_CODEC_ID_NONE,
143 };
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:367
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
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.
#define WV_END_BLOCK
Definition: wvenc.c:29
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:199
void ff_ape_write(AVFormatContext *s)
Write an APEv2 tag.
Definition: apetagenc.c:33
#define WV_EXTRA_SIZE
Definition: wvenc.c:28
Format I/O context.
Definition: avformat.h:944
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:291
uint8_t
static AVPacket pkt
Definition: demuxing.c:56
AVStream ** streams
Definition: avformat.h:992
uint8_t * data
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:173
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:50
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static int write_trailer(AVFormatContext *s)
Definition: wvenc.c:117
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
int size
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
unsigned int nb_streams
A list of all streams in the file.
Definition: avformat.h:991
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:117
int void avio_flush(AVIOContext *s)
Force flushing of buffered data to the output s.
Definition: aviobuf.c:193
const char * name
Definition: avformat.h:378
#define AV_RL32
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
enum AVCodecID codec_id
int sample_rate
samples per second
AVIOContext * pb
I/O context.
Definition: avformat.h:977
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:151
main external API structure.
AVOutputFormat ff_wv_muxer
Definition: wvenc.c:133
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: wvenc.c:56
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static int flags
Definition: cpu.c:23
uint32_t duration
Definition: wvenc.c:32
Main libavformat public API header.
int channels
number of audio channels
void * priv_data
Format private data.
Definition: avformat.h:964
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
static int write_header(AVFormatContext *s)
Definition: wvenc.c:35