Add missing #include, fixes the warning:
[mplayer/greg.git] / libmpdemux / muxer_lavf.c
blob8a8851573e463e04bb0d62d8ff370a15988aec6e
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 #ifdef USE_LIBAVFORMAT_SO
21 #include <ffmpeg/avformat.h>
22 #else
23 #include "avformat.h"
24 #endif
25 #include "libavutil/avstring.h"
27 enum PixelFormat imgfmt2pixfmt(int fmt);
29 extern const struct AVCodecTag *mp_wav_taglists[];
30 extern const struct AVCodecTag *mp_bmp_taglists[];
32 extern char *info_name;
33 extern char *info_artist;
34 extern char *info_genre;
35 extern char *info_subject;
36 extern char *info_copyright;
37 extern char *info_sourceform;
38 extern char *info_comment;
40 typedef struct {
41 //AVInputFormat *avif;
42 AVFormatContext *oc;
43 ByteIOContext *pb;
44 int audio_streams;
45 int video_streams;
46 int64_t last_pts;
47 } muxer_priv_t;
49 typedef struct {
50 int64_t last_pts;
51 AVStream *avstream;
52 } muxer_stream_priv_t;
54 static char *conf_format = NULL;
55 static int mux_rate= 0;
56 static int mux_packet_size= 0;
57 static float mux_preload= 0.5;
58 static float mux_max_delay= 0.7;
60 m_option_t lavfopts_conf[] = {
61 {"format", &(conf_format), CONF_TYPE_STRING, 0, 0, 0, NULL},
62 {"muxrate", &mux_rate, CONF_TYPE_INT, CONF_RANGE, 0, INT_MAX, NULL},
63 {"packetsize", &mux_packet_size, CONF_TYPE_INT, CONF_RANGE, 0, INT_MAX, NULL},
64 {"preload", &mux_preload, CONF_TYPE_FLOAT, CONF_RANGE, 0, INT_MAX, NULL},
65 {"delay", &mux_max_delay, CONF_TYPE_FLOAT, CONF_RANGE, 0, INT_MAX, NULL},
67 {NULL, NULL, 0, 0, 0, 0, NULL}
70 static int mp_open(URLContext *h, const char *filename, int flags)
72 return 0;
75 static int mp_close(URLContext *h)
77 return 0;
81 static int mp_read(URLContext *h, unsigned char *buf, int size)
83 mp_msg(MSGT_MUXER, MSGL_WARN, "READ %d\n", size);
84 return -1;
87 static int mp_write(URLContext *h, unsigned char *buf, int size)
89 muxer_t *muxer = (muxer_t*)h->priv_data;
90 return stream_write_buffer(muxer->stream, buf, size);
93 static offset_t mp_seek(URLContext *h, offset_t pos, int whence)
95 muxer_t *muxer = (muxer_t*)h->priv_data;
96 if(whence == SEEK_CUR)
98 off_t cur = stream_tell(muxer->stream);
99 if(cur == -1)
100 return -1;
101 pos += cur;
103 else if(whence == SEEK_END)
105 off_t size=0;
106 if(stream_control(muxer->stream, STREAM_CTRL_GET_SIZE, &size) == STREAM_UNSUPPORTED || size < pos)
107 return -1;
108 pos = size - pos;
110 mp_msg(MSGT_MUXER, MSGL_DBG2, "SEEK %"PRIu64"\n", (int64_t)pos);
111 if(!stream_seek(muxer->stream, pos))
112 return -1;
113 return 0;
117 static URLProtocol mp_protocol = {
118 "menc",
119 mp_open,
120 mp_read,
121 mp_write,
122 mp_seek,
123 mp_close,
124 NULL
127 static muxer_stream_t* lavf_new_stream(muxer_t *muxer, int type)
129 muxer_priv_t *priv = muxer->priv;
130 muxer_stream_t *stream;
131 muxer_stream_priv_t *spriv;
132 AVCodecContext *ctx;
134 if(!muxer || (type != MUXER_TYPE_VIDEO && type != MUXER_TYPE_AUDIO))
136 mp_msg(MSGT_MUXER, MSGL_ERR, "UNKNOW TYPE %d\n", type);
137 return NULL;
140 stream = calloc(1, sizeof(muxer_stream_t));
141 if(!stream)
143 mp_msg(MSGT_MUXER, MSGL_ERR, "Could not alloc muxer_stream, EXIT\n");
144 return NULL;
146 muxer->streams[muxer->avih.dwStreams] = stream;
147 stream->b_buffer = malloc(2048);
148 if(!stream->b_buffer)
150 mp_msg(MSGT_MUXER, MSGL_ERR, "Could not alloc b_buffer, EXIT\n");
151 free(stream);
152 return NULL;
154 stream->b_buffer_size = 2048;
155 stream->b_buffer_ptr = 0;
156 stream->b_buffer_len = 0;
158 spriv = calloc(1, sizeof(muxer_stream_priv_t));
159 if(!spriv)
161 free(stream);
162 return NULL;
164 stream->priv = spriv;
166 spriv->avstream = av_new_stream(priv->oc, 1);
167 if(!spriv->avstream)
169 mp_msg(MSGT_MUXER, MSGL_ERR, "Could not alloc avstream, EXIT\n");
170 return NULL;
172 spriv->avstream->stream_copy = 1;
174 ctx = spriv->avstream->codec;
175 ctx->codec_id = muxer->avih.dwStreams;
176 switch(type)
178 case MUXER_TYPE_VIDEO:
179 ctx->codec_type = CODEC_TYPE_VIDEO;
180 break;
181 case MUXER_TYPE_AUDIO:
182 ctx->codec_type = CODEC_TYPE_AUDIO;
183 break;
186 muxer->avih.dwStreams++;
187 stream->muxer = muxer;
188 stream->type = type;
189 mp_msg(MSGT_MUXER, MSGL_V, "ALLOCATED STREAM N. %d, type=%d\n", muxer->avih.dwStreams, type);
190 return stream;
194 static void fix_parameters(muxer_stream_t *stream)
196 muxer_stream_priv_t *spriv = (muxer_stream_priv_t *) stream->priv;
197 AVCodecContext *ctx;
199 ctx = spriv->avstream->codec;
201 ctx->bit_rate= stream->avg_rate;
202 if(stream->wf && stream->wf->nAvgBytesPerSec && !ctx->bit_rate)
203 ctx->bit_rate = stream->wf->nAvgBytesPerSec * 8;
204 ctx->rc_buffer_size= stream->vbv_size;
205 ctx->rc_max_rate= stream->max_rate;
207 if(stream->type == MUXER_TYPE_AUDIO)
209 ctx->codec_id = av_codec_get_id(mp_wav_taglists, stream->wf->wFormatTag);
210 #if 0 //breaks aac in mov at least
211 ctx->codec_tag = codec_get_wav_tag(ctx->codec_id);
212 #endif
213 mp_msg(MSGT_MUXER, MSGL_INFO, "AUDIO CODEC ID: %x, TAG: %x\n", ctx->codec_id, (uint32_t) ctx->codec_tag);
214 ctx->sample_rate = stream->wf->nSamplesPerSec;
215 // mp_msg(MSGT_MUXER, MSGL_INFO, "stream->h.dwSampleSize: %d\n", stream->h.dwSampleSize);
216 ctx->channels = stream->wf->nChannels;
217 if(stream->h.dwRate && (stream->h.dwScale * (int64_t)ctx->sample_rate) % stream->h.dwRate == 0)
218 ctx->frame_size= (stream->h.dwScale * (int64_t)ctx->sample_rate) / stream->h.dwRate;
219 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",
220 ctx->frame_size, stream->h.dwScale, ctx->sample_rate, stream->h.dwRate,
221 ctx->block_align, stream->wf->nBlockAlign, stream->wf->nAvgBytesPerSec);
222 ctx->block_align = stream->h.dwSampleSize;
223 if(stream->wf+1 && stream->wf->cbSize)
225 ctx->extradata = av_malloc(stream->wf->cbSize);
226 if(ctx->extradata != NULL)
228 ctx->extradata_size = stream->wf->cbSize;
229 memcpy(ctx->extradata, stream->wf+1, ctx->extradata_size);
231 else
232 mp_msg(MSGT_MUXER, MSGL_ERR, "MUXER_LAVF(audio stream) error! couldn't allocate %d bytes for extradata\n",
233 stream->wf->cbSize);
236 else if(stream->type == MUXER_TYPE_VIDEO)
238 ctx->codec_id = av_codec_get_id(mp_bmp_taglists, stream->bih->biCompression);
239 if(ctx->codec_id <= 0 || force_fourcc)
240 ctx->codec_tag= stream->bih->biCompression;
241 mp_msg(MSGT_MUXER, MSGL_INFO, "VIDEO CODEC ID: %d\n", ctx->codec_id);
242 if (stream->imgfmt)
243 ctx->pix_fmt = imgfmt2pixfmt(stream->imgfmt);
244 ctx->width = stream->bih->biWidth;
245 ctx->height = stream->bih->biHeight;
246 ctx->bit_rate = 800000;
247 ctx->time_base.den = stream->h.dwRate;
248 ctx->time_base.num = stream->h.dwScale;
249 if(stream->bih+1 && (stream->bih->biSize > sizeof(BITMAPINFOHEADER)))
251 ctx->extradata_size = stream->bih->biSize - sizeof(BITMAPINFOHEADER);
252 ctx->extradata = av_malloc(ctx->extradata_size);
253 if(ctx->extradata != NULL)
254 memcpy(ctx->extradata, stream->bih+1, ctx->extradata_size);
255 else
257 mp_msg(MSGT_MUXER, MSGL_ERR, "MUXER_LAVF(video stream) error! couldn't allocate %d bytes for extradata\n",
258 ctx->extradata_size);
259 ctx->extradata_size = 0;
265 static void write_chunk(muxer_stream_t *stream, size_t len, unsigned int flags, double dts, double pts)
267 muxer_t *muxer = (muxer_t*) stream->muxer;
268 muxer_priv_t *priv = (muxer_priv_t *) muxer->priv;
269 muxer_stream_priv_t *spriv = (muxer_stream_priv_t *) stream->priv;
270 AVPacket pkt;
272 if(len)
274 av_init_packet(&pkt);
275 pkt.size = len;
276 pkt.stream_index= spriv->avstream->index;
277 pkt.data = stream->buffer;
279 if(flags & AVIIF_KEYFRAME)
280 pkt.flags |= PKT_FLAG_KEY;
281 else
282 pkt.flags = 0;
284 pkt.dts = (dts / av_q2d(priv->oc->streams[pkt.stream_index]->time_base) + 0.5);
285 pkt.pts = (pts / av_q2d(priv->oc->streams[pkt.stream_index]->time_base) + 0.5);
286 //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);
288 if(av_interleaved_write_frame(priv->oc, &pkt) != 0) //av_write_frame(priv->oc, &pkt)
290 mp_msg(MSGT_MUXER, MSGL_ERR, "Error while writing frame\n");
294 return;
298 static void write_header(muxer_t *muxer)
300 muxer_priv_t *priv = (muxer_priv_t *) muxer->priv;
302 mp_msg(MSGT_MUXER, MSGL_INFO, MSGTR_WritingHeader);
303 av_write_header(priv->oc);
304 muxer->cont_write_header = NULL;
308 static void write_trailer(muxer_t *muxer)
310 int i;
311 muxer_priv_t *priv = (muxer_priv_t *) muxer->priv;
313 mp_msg(MSGT_MUXER, MSGL_INFO, MSGTR_WritingTrailer);
314 av_write_trailer(priv->oc);
315 for(i = 0; i < priv->oc->nb_streams; i++)
317 av_freep(&(priv->oc->streams[i]));
320 url_fclose(priv->oc->pb);
322 av_free(priv->oc);
325 static void list_formats(void) {
326 AVOutputFormat *fmt;
327 mp_msg(MSGT_DEMUX, MSGL_INFO, "Available lavf output formats:\n");
328 for (fmt = first_oformat; fmt; fmt = fmt->next)
329 mp_msg(MSGT_DEMUX, MSGL_INFO, "%15s : %s\n", fmt->name, fmt->long_name);
332 extern char *out_filename;
333 int muxer_init_muxer_lavf(muxer_t *muxer)
335 muxer_priv_t *priv;
336 AVOutputFormat *fmt = NULL;
337 char mp_filename[256] = "menc://stream.dummy";
339 av_register_all();
341 if (conf_format && strcmp(conf_format, "help") == 0) {
342 list_formats();
343 return 0;
346 mp_msg(MSGT_MUXER, MSGL_WARN, "** MUXER_LAVF *****************************************************************\n");
347 mp_msg(MSGT_MUXER, MSGL_WARN,
348 "REMEMBER: MEncoder's libavformat muxing is presently broken and can generate\n"
349 "INCORRECT files in the presence of B frames. Moreover, due to bugs MPlayer\n"
350 "will play these INCORRECT files as if nothing were wrong!\n"
351 "*******************************************************************************\n");
353 priv = (muxer_priv_t *) calloc(1, sizeof(muxer_priv_t));
354 if(priv == NULL)
355 return 0;
357 priv->oc = av_alloc_format_context();
358 if(!priv->oc)
360 mp_msg(MSGT_MUXER, MSGL_FATAL, "Couldn't get format context\n");
361 goto fail;
364 if(conf_format)
365 fmt = guess_format(conf_format, NULL, NULL);
366 if(! fmt)
367 fmt = guess_format(NULL, out_filename, NULL);
368 if(! fmt)
370 mp_msg(MSGT_MUXER, MSGL_FATAL, "CAN'T GET SPECIFIED FORMAT\n");
371 goto fail;
373 priv->oc->oformat = fmt;
376 if(av_set_parameters(priv->oc, NULL) < 0)
378 mp_msg(MSGT_MUXER, MSGL_FATAL, "Invalid output format parameters\n");
379 goto fail;
381 priv->oc->packet_size= mux_packet_size;
382 priv->oc->mux_rate= mux_rate;
383 priv->oc->preload= (int)(mux_preload*AV_TIME_BASE);
384 priv->oc->max_delay= (int)(mux_max_delay*AV_TIME_BASE);
385 if (info_name)
386 av_strlcpy(priv->oc->title , info_name, sizeof(priv->oc->title ));
387 if (info_artist)
388 av_strlcpy(priv->oc->author , info_artist, sizeof(priv->oc->author ));
389 if (info_genre)
390 av_strlcpy(priv->oc->genre , info_genre, sizeof(priv->oc->genre ));
391 if (info_copyright)
392 av_strlcpy(priv->oc->copyright, info_copyright, sizeof(priv->oc->copyright));
393 if (info_comment)
394 av_strlcpy(priv->oc->comment , info_comment, sizeof(priv->oc->comment ));
395 register_protocol(&mp_protocol);
397 if(url_fopen(&priv->oc->pb, mp_filename, URL_WRONLY))
399 mp_msg(MSGT_MUXER, MSGL_FATAL, "Could not open outfile\n");
400 goto fail;
403 ((URLContext*)(priv->oc->pb->opaque))->priv_data= muxer;
405 muxer->priv = (void *) priv;
406 muxer->cont_new_stream = &lavf_new_stream;
407 muxer->cont_write_chunk = &write_chunk;
408 muxer->cont_write_header = &write_header;
409 muxer->cont_write_index = &write_trailer;
410 muxer->fix_stream_parameters = &fix_parameters;
411 mp_msg(MSGT_MUXER, MSGL_INFO, "OK, exit\n");
412 return 1;
414 fail:
415 free(priv);
416 return 0;