Libav
wvenc.c
Go to the documentation of this file.
1 /*
2  * This file is part of Libav.
3  *
4  * Libav is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * Libav is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with Libav; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "libavutil/attributes.h"
20 
21 #include "apetag.h"
22 #include "avformat.h"
23 #include "wv.h"
24 
25 typedef struct WvMuxContext {
26  int64_t samples;
27 } WvMuxContext;
28 
30 {
31  if (ctx->nb_streams > 1 ||
33  av_log(ctx, AV_LOG_ERROR, "This muxer only supports a single WavPack stream.\n");
34  return AVERROR(EINVAL);
35  }
36 
37  return 0;
38 }
39 
41 {
42  WvMuxContext *s = ctx->priv_data;
43  WvHeader header;
44  int ret;
45 
46  if (pkt->size < WV_HEADER_SIZE ||
47  (ret = ff_wv_parse_header(&header, pkt->data)) < 0) {
48  av_log(ctx, AV_LOG_ERROR, "Invalid WavPack packet.\n");
49  return AVERROR(EINVAL);
50  }
51  s->samples += header.samples;
52 
53  avio_write(ctx->pb, pkt->data, pkt->size);
54 
55  return 0;
56 }
57 
59 {
60  WvMuxContext *s = ctx->priv_data;
61 
62  /* update total number of samples in the first block */
63  if (ctx->pb->seekable && s->samples &&
64  s->samples < UINT32_MAX) {
65  int64_t pos = avio_tell(ctx->pb);
66  avio_seek(ctx->pb, 12, SEEK_SET);
67  avio_wl32(ctx->pb, s->samples);
68  avio_seek(ctx->pb, pos, SEEK_SET);
69  }
70 
71  ff_ape_write_tag(ctx);
72  return 0;
73 }
74 
76  .name = "wv",
77  .long_name = NULL_IF_CONFIG_SMALL("raw WavPack"),
78  .mime_type = "audio/x-wavpack",
79  .extensions = "wv",
80  .priv_data_size = sizeof(WvMuxContext),
81  .audio_codec = AV_CODEC_ID_WAVPACK,
82  .video_codec = AV_CODEC_ID_NONE,
87 };
uint32_t samples
Definition: wv.h:39
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: assenc.c:58
int size
Definition: avcodec.h:974
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
Macro definitions for various function/variable attributes.
Format I/O context.
Definition: avformat.h:922
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:260
#define av_cold
Definition: attributes.h:66
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:990
uint8_t * data
Definition: avcodec.h:973
static int flags
Definition: log.c:44
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:219
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:165
Definition: wv.h:34
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:64
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#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:150
static av_cold int wv_write_header(AVFormatContext *ctx)
Definition: wvenc.c:29
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:978
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:117
int ff_wv_parse_header(WvHeader *wv, const uint8_t *data)
Parse a WavPack block header.
Definition: wv.c:29
const char * name
Definition: avformat.h:446
#define WV_HEADER_SIZE
Definition: wavpack.c:36
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:416
static av_cold int wv_write_trailer(AVFormatContext *ctx)
Definition: wvenc.c:58
enum AVCodecID codec_id
Definition: avcodec.h:1067
AVIOContext * pb
I/O context.
Definition: avformat.h:964
int64_t samples
Definition: wvenc.c:26
AVOutputFormat ff_wv_muxer
Definition: wvenc.c:75
int ff_ape_write_tag(AVFormatContext *s)
Write an APE tag into a file.
Definition: apetag.c:179
static int wv_write_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: wvenc.c:40
Main libavformat public API header.
void * priv_data
Format private data.
Definition: avformat.h:950
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:380
This structure stores compressed data.
Definition: avcodec.h:950