-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLibavWaveWriter.cpp
More file actions
150 lines (108 loc) · 3.41 KB
/
LibavWaveWriter.cpp
File metadata and controls
150 lines (108 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "LibavWaveWriter.h"
LibavWaveWriter::LibavWaveWriter() throw (AVException*)
{
av_register_all();
oc = avformat_alloc_context();
libavfmt = av_guess_format("wav", NULL, NULL);
if (libavfmt == NULL) {
throw new AVException("Internal codec name not supported",FILE_ERROR);
// std::cerr << "internal codec name not supported\n";
;
}
oc->oformat = libavfmt;
codec = avcodec_find_encoder(libavfmt->audio_codec);
if (!codec) {
throw new AVException("Codec not supported",FILE_ERROR);
// std::cerr << "codec not found\n";
}
audio_st = avformat_new_stream(oc, codec);
if (!audio_st) {
throw new AVException("Could not alloc stream",FILE_ERROR);
// std::cerr << "Could not alloc stream\n";
}
audio_st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
}
LibavWaveWriter::~LibavWaveWriter()
{
avcodec_close(audio_st->codec);
int i;
for (i = 0; i < oc->nb_streams; i++) {
av_freep(&oc->streams[i]->codec);
av_freep(&oc->streams[i]);
}
avio_close(oc->pb);
av_free(oc);
}
int LibavWaveWriter::setOutputFilename(std::string filename) throw (AVException*)
{
snprintf(oc->filename, sizeof(oc->filename), "%s", filename.c_str());
if (avio_open(&oc->pb, filename.c_str(), AVIO_FLAG_WRITE) < 0) {
throw new AVException("Could not open " + filename,IO_ERROR);
// std::cerr << "Could not open "<< filename << "\n";
// return -1;
}
return 0;
}
int LibavWaveWriter::writeHeader(void) throw (AVException *)
{
AVCodecContext *c = audio_st->codec;
// NSLog(@"avcodec_open2");
if (avcodec_open2(c, codec, NULL) < 0) {
throw new AVException("Codec not supported",FILE_ERROR);
std::cerr << "could not open codec\n";
return -1;
}
/* Write the stream header, if any. */
return avformat_write_header(oc, NULL);
}
void LibavWaveWriter::setSampleFormat(AVSampleFormat fmt)
{
// std::cerr <<"Setting format to: "<<av_get_sample_fmt_name(fmt) << "\n";
audio_st->codec->sample_fmt = fmt;
}
void LibavWaveWriter::setSampleChannels(int ch)
{
// std::cerr<<"Channels: "<<ch<<"\n";
audio_st->codec->channels = ch;
}
void LibavWaveWriter::setSamplesPerSecond(int sps) {
// std::cerr << "Sample Rate: " << sps;
audio_st->codec->sample_rate = sps;
}
int LibavWaveWriter::writeFrameData(AVFrame *pFrame) throw (AVException*)
{
AVCodecContext *c = audio_st->codec;
AVPacket pkt = { 0 }; // data and size must be 0;
int got_packet;
// I'm not sure which version this changed
//AVFrame *frame = avcodec_alloc_frame();
AVFrame *frame = av_frame_alloc();
int status;
frame->nb_samples = pFrame->nb_samples;
frame->data[0] = pFrame->data[0];
// NSLog(@"write samples: %d",pFrame->nb_samples);
/*
avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,
(uint8_t *)pFrame->data[0],
pFrame->nb_samples *
av_get_bytes_per_sample(c->sample_fmt) *
c->channels, 1);
*/
// NSLog(@"encode audio 2");
avcodec_encode_audio2(c, &pkt, frame, &got_packet);
if (!got_packet) {
throw new AVException ("Unable to get packet",IO_ERROR);
// return -1;
}
pkt.stream_index = audio_st->index;
// NSLog(@"av_interleaved_write_frame");
if ((status=av_interleaved_write_frame(oc, &pkt)) != 0) {
throw new AVException ("Error while writing audio frame",IO_ERROR);
// std::cerr<<"Error while writing audio frame\n";
// return status;
}
// NSLog(@"avcodec free frame");
//avcodec_free_frame(&frame);
av_frame_free(&frame);
return 0;
}