osdep: Remove cruft
[mplayer.git] / libmpdemux / muxer_lavf.c
blobcd52168cdc0ed8ee002f9629bdae77ef692f0c57
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 "version.h"
9 #include "mp_msg.h"
10 #include "help_mp.h"
12 #include "aviheader.h"
13 #include "ms_hdr.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 typedef struct {
36 //AVInputFormat *avif;
37 AVFormatContext *oc;
38 ByteIOContext *pb;
39 int audio_streams;
40 int video_streams;
41 int64_t last_pts;
42 } muxer_priv_t;
44 typedef struct {
45 int64_t last_pts;
46 AVStream *avstream;
47 } muxer_stream_priv_t;
49 static char *conf_format = NULL;
50 static int mux_rate= 0;
51 static int mux_packet_size= 0;
52 static float mux_preload= 0.5;
53 static float mux_max_delay= 0.7;
55 m_option_t lavfopts_conf[] = {
56 {"format", &(conf_format), CONF_TYPE_STRING, 0, 0, 0, NULL},
57 {"muxrate", &mux_rate, CONF_TYPE_INT, CONF_RANGE, 0, INT_MAX, NULL},
58 {"packetsize", &mux_packet_size, CONF_TYPE_INT, CONF_RANGE, 0, INT_MAX, NULL},
59 {"preload", &mux_preload, CONF_TYPE_FLOAT, CONF_RANGE, 0, INT_MAX, NULL},
60 {"delay", &mux_max_delay, CONF_TYPE_FLOAT, CONF_RANGE, 0, INT_MAX, NULL},
62 {NULL, NULL, 0, 0, 0, 0, NULL}
65 static int mp_open(URLContext *h, const char *filename, int flags)
67 return 0;
70 static int mp_close(URLContext *h)
72 return 0;
76 static int mp_read(URLContext *h, unsigned char *buf, int size)
78 mp_msg(MSGT_MUXER, MSGL_WARN, "READ %d\n", size);
79 return -1;
82 static int mp_write(URLContext *h, unsigned char *buf, int size)
84 muxer_t *muxer = (muxer_t*)h->priv_data;
85 return stream_write_buffer(muxer->stream, buf, size);
88 static offset_t mp_seek(URLContext *h, offset_t pos, int whence)
90 muxer_t *muxer = (muxer_t*)h->priv_data;
91 if(whence == SEEK_CUR)
93 off_t cur = stream_tell(muxer->stream);
94 if(cur == -1)
95 return -1;
96 pos += cur;
98 else if(whence == SEEK_END)
100 off_t size=0;
101 if(stream_control(muxer->stream, STREAM_CTRL_GET_SIZE, &size) == STREAM_UNSUPPORTED || size < pos)
102 return -1;
103 pos = size - pos;
105 mp_msg(MSGT_MUXER, MSGL_DBG2, "SEEK %"PRIu64"\n", (int64_t)pos);
106 if(!stream_seek(muxer->stream, pos))
107 return -1;
108 return 0;
112 static URLProtocol mp_protocol = {
113 "menc",
114 mp_open,
115 mp_read,
116 mp_write,
117 mp_seek,
118 mp_close,
119 NULL
122 static muxer_stream_t* lavf_new_stream(muxer_t *muxer, int type)
124 muxer_priv_t *priv = muxer->priv;
125 muxer_stream_t *stream;
126 muxer_stream_priv_t *spriv;
127 AVCodecContext *ctx;
129 if(!muxer || (type != MUXER_TYPE_VIDEO && type != MUXER_TYPE_AUDIO))
131 mp_msg(MSGT_MUXER, MSGL_ERR, "UNKNOW TYPE %d\n", type);
132 return NULL;
135 stream = calloc(1, sizeof(muxer_stream_t));
136 if(!stream)
138 mp_msg(MSGT_MUXER, MSGL_ERR, "Could not alloc muxer_stream, EXIT\n");
139 return NULL;
141 muxer->streams[muxer->avih.dwStreams] = stream;
142 stream->b_buffer = malloc(2048);
143 if(!stream->b_buffer)
145 mp_msg(MSGT_MUXER, MSGL_ERR, "Could not alloc b_buffer, EXIT\n");
146 free(stream);
147 return NULL;
149 stream->b_buffer_size = 2048;
150 stream->b_buffer_ptr = 0;
151 stream->b_buffer_len = 0;
153 spriv = calloc(1, sizeof(muxer_stream_priv_t));
154 if(!spriv)
156 free(stream);
157 return NULL;
159 stream->priv = spriv;
161 spriv->avstream = av_new_stream(priv->oc, 1);
162 if(!spriv->avstream)
164 mp_msg(MSGT_MUXER, MSGL_ERR, "Could not alloc avstream, EXIT\n");
165 return NULL;
167 spriv->avstream->stream_copy = 1;
169 ctx = spriv->avstream->codec;
170 ctx->codec_id = muxer->avih.dwStreams;
171 switch(type)
173 case MUXER_TYPE_VIDEO:
174 ctx->codec_type = CODEC_TYPE_VIDEO;
175 break;
176 case MUXER_TYPE_AUDIO:
177 ctx->codec_type = CODEC_TYPE_AUDIO;
178 break;
181 muxer->avih.dwStreams++;
182 stream->muxer = muxer;
183 stream->type = type;
184 mp_msg(MSGT_MUXER, MSGL_V, "ALLOCATED STREAM N. %d, type=%d\n", muxer->avih.dwStreams, type);
185 return stream;
189 static void fix_parameters(muxer_stream_t *stream)
191 muxer_stream_priv_t *spriv = (muxer_stream_priv_t *) stream->priv;
192 AVCodecContext *ctx;
194 ctx = spriv->avstream->codec;
196 ctx->bit_rate= stream->avg_rate;
197 if(stream->wf && stream->wf->nAvgBytesPerSec && !ctx->bit_rate)
198 ctx->bit_rate = stream->wf->nAvgBytesPerSec * 8;
199 ctx->rc_buffer_size= stream->vbv_size;
200 ctx->rc_max_rate= stream->max_rate;
202 if(stream->type == MUXER_TYPE_AUDIO)
204 ctx->codec_id = av_codec_get_id(mp_wav_taglists, stream->wf->wFormatTag);
205 #if 0 //breaks aac in mov at least
206 ctx->codec_tag = codec_get_wav_tag(ctx->codec_id);
207 #endif
208 mp_msg(MSGT_MUXER, MSGL_INFO, "AUDIO CODEC ID: %x, TAG: %x\n", ctx->codec_id, (uint32_t) ctx->codec_tag);
209 ctx->sample_rate = stream->wf->nSamplesPerSec;
210 // mp_msg(MSGT_MUXER, MSGL_INFO, "stream->h.dwSampleSize: %d\n", stream->h.dwSampleSize);
211 ctx->channels = stream->wf->nChannels;
212 if(stream->h.dwRate && (stream->h.dwScale * (int64_t)ctx->sample_rate) % stream->h.dwRate == 0)
213 ctx->frame_size= (stream->h.dwScale * (int64_t)ctx->sample_rate) / stream->h.dwRate;
214 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",
215 ctx->frame_size, stream->h.dwScale, ctx->sample_rate, stream->h.dwRate,
216 ctx->block_align, stream->wf->nBlockAlign, stream->wf->nAvgBytesPerSec);
217 ctx->block_align = stream->h.dwSampleSize;
218 if(stream->wf+1 && stream->wf->cbSize)
220 ctx->extradata = av_malloc(stream->wf->cbSize);
221 if(ctx->extradata != NULL)
223 ctx->extradata_size = stream->wf->cbSize;
224 memcpy(ctx->extradata, stream->wf+1, ctx->extradata_size);
226 else
227 mp_msg(MSGT_MUXER, MSGL_ERR, "MUXER_LAVF(audio stream) error! couldn't allocate %d bytes for extradata\n",
228 stream->wf->cbSize);
231 else if(stream->type == MUXER_TYPE_VIDEO)
233 ctx->codec_id = av_codec_get_id(mp_bmp_taglists, stream->bih->biCompression);
234 if(ctx->codec_id <= 0 || force_fourcc)
235 ctx->codec_tag= stream->bih->biCompression;
236 mp_msg(MSGT_MUXER, MSGL_INFO, "VIDEO CODEC ID: %d\n", ctx->codec_id);
237 if (stream->imgfmt)
238 ctx->pix_fmt = imgfmt2pixfmt(stream->imgfmt);
239 ctx->width = stream->bih->biWidth;
240 ctx->height = stream->bih->biHeight;
241 ctx->bit_rate = 800000;
242 ctx->time_base.den = stream->h.dwRate;
243 ctx->time_base.num = stream->h.dwScale;
244 if(stream->bih+1 && (stream->bih->biSize > sizeof(BITMAPINFOHEADER)))
246 ctx->extradata_size = stream->bih->biSize - sizeof(BITMAPINFOHEADER);
247 ctx->extradata = av_malloc(ctx->extradata_size);
248 if(ctx->extradata != NULL)
249 memcpy(ctx->extradata, stream->bih+1, ctx->extradata_size);
250 else
252 mp_msg(MSGT_MUXER, MSGL_ERR, "MUXER_LAVF(video stream) error! couldn't allocate %d bytes for extradata\n",
253 ctx->extradata_size);
254 ctx->extradata_size = 0;
260 static void write_chunk(muxer_stream_t *stream, size_t len, unsigned int flags, double dts, double pts)
262 muxer_t *muxer = (muxer_t*) stream->muxer;
263 muxer_priv_t *priv = (muxer_priv_t *) muxer->priv;
264 muxer_stream_priv_t *spriv = (muxer_stream_priv_t *) stream->priv;
265 AVPacket pkt;
267 if(len)
269 av_init_packet(&pkt);
270 pkt.size = len;
271 pkt.stream_index= spriv->avstream->index;
272 pkt.data = stream->buffer;
274 if(flags & AVIIF_KEYFRAME)
275 pkt.flags |= PKT_FLAG_KEY;
276 else
277 pkt.flags = 0;
279 pkt.dts = (dts / av_q2d(priv->oc->streams[pkt.stream_index]->time_base) + 0.5);
280 pkt.pts = (pts / av_q2d(priv->oc->streams[pkt.stream_index]->time_base) + 0.5);
281 //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);
283 if(av_interleaved_write_frame(priv->oc, &pkt) != 0) //av_write_frame(priv->oc, &pkt)
285 mp_msg(MSGT_MUXER, MSGL_ERR, "Error while writing frame\n");
289 return;
293 static void write_header(muxer_t *muxer)
295 muxer_priv_t *priv = (muxer_priv_t *) muxer->priv;
297 mp_msg(MSGT_MUXER, MSGL_INFO, MSGTR_WritingHeader);
298 av_write_header(priv->oc);
299 muxer->cont_write_header = NULL;
303 static void write_trailer(muxer_t *muxer)
305 int i;
306 muxer_priv_t *priv = (muxer_priv_t *) muxer->priv;
308 mp_msg(MSGT_MUXER, MSGL_INFO, MSGTR_WritingTrailer);
309 av_write_trailer(priv->oc);
310 for(i = 0; i < priv->oc->nb_streams; i++)
312 av_freep(&(priv->oc->streams[i]));
315 url_fclose(priv->oc->pb);
317 av_free(priv->oc);
320 static void list_formats(void) {
321 AVOutputFormat *fmt;
322 mp_msg(MSGT_DEMUX, MSGL_INFO, "Available lavf output formats:\n");
323 for (fmt = first_oformat; fmt; fmt = fmt->next)
324 mp_msg(MSGT_DEMUX, MSGL_INFO, "%15s : %s\n", fmt->name, fmt->long_name);
327 extern char *out_filename;
328 int muxer_init_muxer_lavf(muxer_t *muxer)
330 muxer_priv_t *priv;
331 AVOutputFormat *fmt = NULL;
332 char mp_filename[256] = "menc://stream.dummy";
334 av_register_all();
336 if (conf_format && strcmp(conf_format, "help") == 0) {
337 list_formats();
338 return 0;
341 mp_msg(MSGT_MUXER, MSGL_WARN, "** MUXER_LAVF *****************************************************************\n");
342 mp_msg(MSGT_MUXER, MSGL_WARN,
343 "REMEMBER: MEncoder's libavformat muxing is presently broken and can generate\n"
344 "INCORRECT files in the presence of B frames. Moreover, due to bugs MPlayer\n"
345 "will play these INCORRECT files as if nothing were wrong!\n"
346 "*******************************************************************************\n");
348 priv = (muxer_priv_t *) calloc(1, sizeof(muxer_priv_t));
349 if(priv == NULL)
350 return 0;
352 priv->oc = av_alloc_format_context();
353 if(!priv->oc)
355 mp_msg(MSGT_MUXER, MSGL_FATAL, "Couldn't get format context\n");
356 goto fail;
359 if(conf_format)
360 fmt = guess_format(conf_format, NULL, NULL);
361 if(! fmt)
362 fmt = guess_format(NULL, out_filename, NULL);
363 if(! fmt)
365 mp_msg(MSGT_MUXER, MSGL_FATAL, "CAN'T GET SPECIFIED FORMAT\n");
366 goto fail;
368 priv->oc->oformat = fmt;
371 if(av_set_parameters(priv->oc, NULL) < 0)
373 mp_msg(MSGT_MUXER, MSGL_FATAL, "Invalid output format parameters\n");
374 goto fail;
376 priv->oc->packet_size= mux_packet_size;
377 priv->oc->mux_rate= mux_rate;
378 priv->oc->preload= (int)(mux_preload*AV_TIME_BASE);
379 priv->oc->max_delay= (int)(mux_max_delay*AV_TIME_BASE);
380 if (info_name)
381 av_strlcpy(priv->oc->title , info_name, sizeof(priv->oc->title ));
382 if (info_artist)
383 av_strlcpy(priv->oc->author , info_artist, sizeof(priv->oc->author ));
384 if (info_genre)
385 av_strlcpy(priv->oc->genre , info_genre, sizeof(priv->oc->genre ));
386 if (info_copyright)
387 av_strlcpy(priv->oc->copyright, info_copyright, sizeof(priv->oc->copyright));
388 if (info_comment)
389 av_strlcpy(priv->oc->comment , info_comment, sizeof(priv->oc->comment ));
390 register_protocol(&mp_protocol);
392 if(url_fopen(&priv->oc->pb, mp_filename, URL_WRONLY))
394 mp_msg(MSGT_MUXER, MSGL_FATAL, "Could not open outfile\n");
395 goto fail;
398 ((URLContext*)(priv->oc->pb->opaque))->priv_data= muxer;
400 muxer->priv = (void *) priv;
401 muxer->cont_new_stream = &lavf_new_stream;
402 muxer->cont_write_chunk = &write_chunk;
403 muxer->cont_write_header = &write_header;
404 muxer->cont_write_index = &write_trailer;
405 muxer->fix_stream_parameters = &fix_parameters;
406 mp_msg(MSGT_MUXER, MSGL_INFO, "OK, exit\n");
407 return 1;
409 fail:
410 free(priv);
411 return 0;