lirc anc lircc are MPlayer-only, so add LDFLAGS for MPlayer link only.
[mplayer/glamo.git] / libmpdemux / muxer_lavf.c
blob3cb536476b4b0673af51281d99cd43b3148b2391
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <inttypes.h>
6 #include <limits.h>
7 #include "config.h"
8 #include "mp_msg.h"
9 #include "help_mp.h"
11 #include "aviheader.h"
12 #include "ms_hdr.h"
13 #include "av_opts.h"
15 #include "stream/stream.h"
16 #include "muxer.h"
17 #include "demuxer.h"
18 #include "stheader.h"
19 #include "m_option.h"
20 #include "libavformat/avformat.h"
21 #include "libavutil/avstring.h"
23 #include "mp_taglists.h"
25 enum PixelFormat imgfmt2pixfmt(int fmt);
27 extern char *info_name;
28 extern char *info_artist;
29 extern char *info_genre;
30 extern char *info_subject;
31 extern char *info_copyright;
32 extern char *info_sourceform;
33 extern char *info_comment;
35 #define BIO_BUFFER_SIZE 32768
37 typedef struct {
38 //AVInputFormat *avif;
39 AVFormatContext *oc;
40 ByteIOContext *pb;
41 int audio_streams;
42 int video_streams;
43 int64_t last_pts;
44 uint8_t buffer[BIO_BUFFER_SIZE];
45 } muxer_priv_t;
47 typedef struct {
48 int64_t last_pts;
49 AVStream *avstream;
50 } muxer_stream_priv_t;
52 static char *conf_format = NULL;
53 static int mux_rate= 0;
54 static int mux_packet_size= 0;
55 static float mux_preload= 0.5;
56 static float mux_max_delay= 0.7;
57 static char *mux_avopt = NULL;
59 m_option_t lavfopts_conf[] = {
60 {"format", &(conf_format), CONF_TYPE_STRING, 0, 0, 0, NULL},
61 {"muxrate", &mux_rate, CONF_TYPE_INT, CONF_RANGE, 0, INT_MAX, NULL},
62 {"packetsize", &mux_packet_size, CONF_TYPE_INT, CONF_RANGE, 0, INT_MAX, NULL},
63 {"preload", &mux_preload, CONF_TYPE_FLOAT, CONF_RANGE, 0, INT_MAX, NULL},
64 {"delay", &mux_max_delay, CONF_TYPE_FLOAT, CONF_RANGE, 0, INT_MAX, NULL},
65 {"o", &mux_avopt, CONF_TYPE_STRING, 0, 0, 0, NULL},
67 {NULL, NULL, 0, 0, 0, 0, NULL}
70 static int mp_write(void *opaque, uint8_t *buf, int size)
72 muxer_t *muxer = opaque;
73 return stream_write_buffer(muxer->stream, buf, size);
76 static int64_t mp_seek(void *opaque, int64_t pos, int whence)
78 muxer_t *muxer = opaque;
79 if(whence == SEEK_CUR)
81 off_t cur = stream_tell(muxer->stream);
82 if(cur == -1)
83 return -1;
84 pos += cur;
86 else if(whence == SEEK_END)
88 off_t size=0;
89 if(stream_control(muxer->stream, STREAM_CTRL_GET_SIZE, &size) == STREAM_UNSUPPORTED || size < pos)
90 return -1;
91 pos = size - pos;
93 mp_msg(MSGT_MUXER, MSGL_DBG2, "SEEK %"PRIu64"\n", (int64_t)pos);
94 if(!stream_seek(muxer->stream, pos))
95 return -1;
96 return 0;
100 static muxer_stream_t* lavf_new_stream(muxer_t *muxer, int type)
102 muxer_priv_t *priv = muxer->priv;
103 muxer_stream_t *stream;
104 muxer_stream_priv_t *spriv;
105 AVCodecContext *ctx;
107 if(!muxer || (type != MUXER_TYPE_VIDEO && type != MUXER_TYPE_AUDIO))
109 mp_msg(MSGT_MUXER, MSGL_ERR, "UNKNOWN TYPE %d\n", type);
110 return NULL;
113 stream = calloc(1, sizeof(muxer_stream_t));
114 if(!stream)
116 mp_msg(MSGT_MUXER, MSGL_ERR, "Could not allocate muxer_stream, EXIT.\n");
117 return NULL;
119 muxer->streams[muxer->avih.dwStreams] = stream;
120 stream->b_buffer = malloc(2048);
121 if(!stream->b_buffer)
123 mp_msg(MSGT_MUXER, MSGL_ERR, "Could not allocate b_buffer, EXIT.\n");
124 free(stream);
125 return NULL;
127 stream->b_buffer_size = 2048;
128 stream->b_buffer_ptr = 0;
129 stream->b_buffer_len = 0;
131 spriv = calloc(1, sizeof(muxer_stream_priv_t));
132 if(!spriv)
134 free(stream);
135 return NULL;
137 stream->priv = spriv;
139 spriv->avstream = av_new_stream(priv->oc, 1);
140 if(!spriv->avstream)
142 mp_msg(MSGT_MUXER, MSGL_ERR, "Could not allocate avstream, EXIT.\n");
143 return NULL;
145 spriv->avstream->stream_copy = 1;
147 ctx = spriv->avstream->codec;
148 ctx->codec_id = muxer->avih.dwStreams;
149 switch(type)
151 case MUXER_TYPE_VIDEO:
152 ctx->codec_type = CODEC_TYPE_VIDEO;
153 break;
154 case MUXER_TYPE_AUDIO:
155 ctx->codec_type = CODEC_TYPE_AUDIO;
156 break;
159 muxer->avih.dwStreams++;
160 stream->muxer = muxer;
161 stream->type = type;
162 mp_msg(MSGT_MUXER, MSGL_V, "ALLOCATED STREAM N. %d, type=%d\n", muxer->avih.dwStreams, type);
163 return stream;
167 static void fix_parameters(muxer_stream_t *stream)
169 muxer_stream_priv_t *spriv = (muxer_stream_priv_t *) stream->priv;
170 AVCodecContext *ctx;
172 ctx = spriv->avstream->codec;
174 ctx->bit_rate= stream->avg_rate;
175 if(stream->wf && stream->wf->nAvgBytesPerSec && !ctx->bit_rate)
176 ctx->bit_rate = stream->wf->nAvgBytesPerSec * 8;
177 ctx->rc_buffer_size= stream->vbv_size;
178 ctx->rc_max_rate= stream->max_rate;
180 if(stream->type == MUXER_TYPE_AUDIO)
182 ctx->codec_id = av_codec_get_id(mp_wav_taglists, stream->wf->wFormatTag);
183 #if 0 //breaks aac in mov at least
184 ctx->codec_tag = codec_get_wav_tag(ctx->codec_id);
185 #endif
186 mp_msg(MSGT_MUXER, MSGL_INFO, "AUDIO CODEC ID: %x, TAG: %x\n", ctx->codec_id, (uint32_t) ctx->codec_tag);
187 ctx->sample_rate = stream->wf->nSamplesPerSec;
188 // mp_msg(MSGT_MUXER, MSGL_INFO, "stream->h.dwSampleSize: %d\n", stream->h.dwSampleSize);
189 ctx->channels = stream->wf->nChannels;
190 if(stream->h.dwRate && (stream->h.dwScale * (int64_t)ctx->sample_rate) % stream->h.dwRate == 0)
191 ctx->frame_size= (stream->h.dwScale * (int64_t)ctx->sample_rate) / stream->h.dwRate;
192 mp_msg(MSGT_MUXER, MSGL_V, "MUXER_LAVF(audio stream) frame_size: %d, scale: %u, sps: %u, rate: %u, ctx->block_align = stream->wf->nBlockAlign; %d=%d stream->wf->nAvgBytesPerSec:%d\n",
193 ctx->frame_size, stream->h.dwScale, ctx->sample_rate, stream->h.dwRate,
194 ctx->block_align, stream->wf->nBlockAlign, stream->wf->nAvgBytesPerSec);
195 ctx->block_align = stream->h.dwSampleSize;
196 if(stream->wf+1 && stream->wf->cbSize)
198 ctx->extradata = av_malloc(stream->wf->cbSize);
199 if(ctx->extradata != NULL)
201 ctx->extradata_size = stream->wf->cbSize;
202 memcpy(ctx->extradata, stream->wf+1, ctx->extradata_size);
204 else
205 mp_msg(MSGT_MUXER, MSGL_ERR, "MUXER_LAVF(audio stream) error! Could not allocate %d bytes for extradata.\n",
206 stream->wf->cbSize);
209 else if(stream->type == MUXER_TYPE_VIDEO)
211 ctx->codec_id = av_codec_get_id(mp_bmp_taglists, stream->bih->biCompression);
212 if(ctx->codec_id <= 0 || force_fourcc)
213 ctx->codec_tag= stream->bih->biCompression;
214 mp_msg(MSGT_MUXER, MSGL_INFO, "VIDEO CODEC ID: %d\n", ctx->codec_id);
215 if (stream->imgfmt)
216 ctx->pix_fmt = imgfmt2pixfmt(stream->imgfmt);
217 ctx->width = stream->bih->biWidth;
218 ctx->height = stream->bih->biHeight;
219 ctx->bit_rate = 800000;
220 ctx->time_base.den = stream->h.dwRate;
221 ctx->time_base.num = stream->h.dwScale;
222 if(stream->bih+1 && (stream->bih->biSize > sizeof(BITMAPINFOHEADER)))
224 ctx->extradata_size = stream->bih->biSize - sizeof(BITMAPINFOHEADER);
225 ctx->extradata = av_malloc(ctx->extradata_size);
226 if(ctx->extradata != NULL)
227 memcpy(ctx->extradata, stream->bih+1, ctx->extradata_size);
228 else
230 mp_msg(MSGT_MUXER, MSGL_ERR, "MUXER_LAVF(video stream) error! Could not allocate %d bytes for extradata.\n",
231 ctx->extradata_size);
232 ctx->extradata_size = 0;
238 static void write_chunk(muxer_stream_t *stream, size_t len, unsigned int flags, double dts, double pts)
240 muxer_t *muxer = (muxer_t*) stream->muxer;
241 muxer_priv_t *priv = (muxer_priv_t *) muxer->priv;
242 muxer_stream_priv_t *spriv = (muxer_stream_priv_t *) stream->priv;
243 AVPacket pkt;
245 if(len)
247 av_init_packet(&pkt);
248 pkt.size = len;
249 pkt.stream_index= spriv->avstream->index;
250 pkt.data = stream->buffer;
252 if(flags & AVIIF_KEYFRAME)
253 pkt.flags |= PKT_FLAG_KEY;
254 else
255 pkt.flags = 0;
257 pkt.dts = (dts / av_q2d(priv->oc->streams[pkt.stream_index]->time_base) + 0.5);
258 pkt.pts = (pts / av_q2d(priv->oc->streams[pkt.stream_index]->time_base) + 0.5);
259 //fprintf(stderr, "%Ld %Ld id:%d tb:%f %f\n", pkt.dts, pkt.pts, pkt.stream_index, av_q2d(priv->oc->streams[pkt.stream_index]->time_base), stream->timer);
261 if(av_interleaved_write_frame(priv->oc, &pkt) != 0) //av_write_frame(priv->oc, &pkt)
263 mp_msg(MSGT_MUXER, MSGL_ERR, "Error while writing frame.\n");
267 return;
271 static void write_header(muxer_t *muxer)
273 muxer_priv_t *priv = (muxer_priv_t *) muxer->priv;
275 mp_msg(MSGT_MUXER, MSGL_INFO, MSGTR_WritingHeader);
276 av_write_header(priv->oc);
277 muxer->cont_write_header = NULL;
281 static void write_trailer(muxer_t *muxer)
283 int i;
284 muxer_priv_t *priv = (muxer_priv_t *) muxer->priv;
286 mp_msg(MSGT_MUXER, MSGL_INFO, MSGTR_WritingTrailer);
287 av_write_trailer(priv->oc);
288 for(i = 0; i < priv->oc->nb_streams; i++)
290 av_freep(&(priv->oc->streams[i]));
293 av_freep(&priv->oc->pb);
295 av_free(priv->oc);
298 static void list_formats(void) {
299 AVOutputFormat *fmt;
300 mp_msg(MSGT_DEMUX, MSGL_INFO, "Available lavf output formats:\n");
301 for (fmt = first_oformat; fmt; fmt = fmt->next)
302 mp_msg(MSGT_DEMUX, MSGL_INFO, "%15s : %s\n", fmt->name, fmt->long_name);
305 extern char *out_filename;
306 int muxer_init_muxer_lavf(muxer_t *muxer)
308 muxer_priv_t *priv;
309 AVOutputFormat *fmt = NULL;
311 av_register_all();
313 if (conf_format && strcmp(conf_format, "help") == 0) {
314 list_formats();
315 return 0;
318 mp_msg(MSGT_MUXER, MSGL_WARN, "** MUXER_LAVF *****************************************************************\n");
319 mp_msg(MSGT_MUXER, MSGL_WARN,
320 "REMEMBER: MEncoder's libavformat muxing is presently broken and can generate\n"
321 "INCORRECT files in the presence of B-frames. Moreover, due to bugs MPlayer\n"
322 "will play these INCORRECT files as if nothing were wrong!\n"
323 "*******************************************************************************\n");
325 priv = (muxer_priv_t *) calloc(1, sizeof(muxer_priv_t));
326 if(priv == NULL)
327 return 0;
329 priv->oc = av_alloc_format_context();
330 if(!priv->oc)
332 mp_msg(MSGT_MUXER, MSGL_FATAL, "Could not get format context.\n");
333 goto fail;
336 if(conf_format)
337 fmt = guess_format(conf_format, NULL, NULL);
338 if(! fmt)
339 fmt = guess_format(NULL, out_filename, NULL);
340 if(! fmt)
342 mp_msg(MSGT_MUXER, MSGL_FATAL, "Cannot get specified format.\n");
343 goto fail;
345 priv->oc->oformat = fmt;
348 if(av_set_parameters(priv->oc, NULL) < 0)
350 mp_msg(MSGT_MUXER, MSGL_FATAL, "invalid output format parameters\n");
351 goto fail;
353 priv->oc->packet_size= mux_packet_size;
354 priv->oc->mux_rate= mux_rate;
355 priv->oc->preload= (int)(mux_preload*AV_TIME_BASE);
356 priv->oc->max_delay= (int)(mux_max_delay*AV_TIME_BASE);
357 if (info_name)
358 av_strlcpy(priv->oc->title , info_name, sizeof(priv->oc->title ));
359 if (info_artist)
360 av_strlcpy(priv->oc->author , info_artist, sizeof(priv->oc->author ));
361 if (info_genre)
362 av_strlcpy(priv->oc->genre , info_genre, sizeof(priv->oc->genre ));
363 if (info_copyright)
364 av_strlcpy(priv->oc->copyright, info_copyright, sizeof(priv->oc->copyright));
365 if (info_comment)
366 av_strlcpy(priv->oc->comment , info_comment, sizeof(priv->oc->comment ));
368 if(mux_avopt){
369 if(parse_avopts(priv->oc, mux_avopt) < 0){
370 mp_msg(MSGT_MUXER,MSGL_ERR, "Your options /%s/ look like gibberish to me pal.\n", mux_avopt);
371 goto fail;
375 priv->oc->pb = av_alloc_put_byte(priv->buffer, BIO_BUFFER_SIZE, 1, muxer, NULL, mp_write, mp_seek);
376 if ((muxer->stream->flags & STREAM_SEEK) != STREAM_SEEK)
377 priv->oc->pb->is_streamed = 1;
379 muxer->priv = (void *) priv;
380 muxer->cont_new_stream = &lavf_new_stream;
381 muxer->cont_write_chunk = &write_chunk;
382 muxer->cont_write_header = &write_header;
383 muxer->cont_write_index = &write_trailer;
384 muxer->fix_stream_parameters = &fix_parameters;
385 mp_msg(MSGT_MUXER, MSGL_INFO, "OK, exit.\n");
386 return 1;
388 fail:
389 free(priv);
390 return 0;