vo_gl: Make it possible to select GL_NEAREST scaling
[mplayer/glamo.git] / libmpdemux / muxer_lavf.c
bloba4113c7d9a98b409ea090b67e8da9b78af2260c6
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer 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
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/types.h>
23 #include <inttypes.h>
24 #include <limits.h>
25 #include "config.h"
26 #include "mp_msg.h"
28 #include "aviheader.h"
29 #include "ms_hdr.h"
30 #include "av_opts.h"
32 #include "stream/stream.h"
33 #include "muxer.h"
34 #include "demuxer.h"
35 #include "stheader.h"
36 #include "m_option.h"
37 #include "libavformat/avformat.h"
38 #include "libavutil/avstring.h"
40 #include "mp_taglists.h"
42 enum PixelFormat imgfmt2pixfmt(int fmt);
44 extern char *info_name;
45 extern char *info_artist;
46 extern char *info_genre;
47 extern char *info_subject;
48 extern char *info_copyright;
49 extern char *info_sourceform;
50 extern char *info_comment;
52 #define BIO_BUFFER_SIZE 32768
54 typedef struct {
55 //AVInputFormat *avif;
56 AVFormatContext *oc;
57 ByteIOContext *pb;
58 int audio_streams;
59 int video_streams;
60 int64_t last_pts;
61 uint8_t buffer[BIO_BUFFER_SIZE];
62 } muxer_priv_t;
64 typedef struct {
65 int64_t last_pts;
66 AVStream *avstream;
67 } muxer_stream_priv_t;
69 static char *conf_format = NULL;
70 static int mux_rate= 0;
71 static int mux_packet_size= 0;
72 static float mux_preload= 0.5;
73 static float mux_max_delay= 0.7;
74 static char *mux_avopt = NULL;
76 const m_option_t lavfopts_conf[] = {
77 {"format", &(conf_format), CONF_TYPE_STRING, 0, 0, 0, NULL},
78 {"muxrate", &mux_rate, CONF_TYPE_INT, CONF_RANGE, 0, INT_MAX, NULL},
79 {"packetsize", &mux_packet_size, CONF_TYPE_INT, CONF_RANGE, 0, INT_MAX, NULL},
80 {"preload", &mux_preload, CONF_TYPE_FLOAT, CONF_RANGE, 0, INT_MAX, NULL},
81 {"delay", &mux_max_delay, CONF_TYPE_FLOAT, CONF_RANGE, 0, INT_MAX, NULL},
82 {"o", &mux_avopt, CONF_TYPE_STRING, 0, 0, 0, NULL},
84 {NULL, NULL, 0, 0, 0, 0, NULL}
87 static int mp_write(void *opaque, uint8_t *buf, int size)
89 muxer_t *muxer = opaque;
90 return stream_write_buffer(muxer->stream, buf, size);
93 static int64_t mp_seek(void *opaque, int64_t pos, int whence)
95 muxer_t *muxer = opaque;
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 muxer_stream_t* lavf_new_stream(muxer_t *muxer, int type)
119 muxer_priv_t *priv = muxer->priv;
120 muxer_stream_t *stream;
121 muxer_stream_priv_t *spriv;
122 AVCodecContext *ctx;
124 if(!muxer || (type != MUXER_TYPE_VIDEO && type != MUXER_TYPE_AUDIO))
126 mp_msg(MSGT_MUXER, MSGL_ERR, "UNKNOWN TYPE %d\n", type);
127 return NULL;
130 stream = calloc(1, sizeof(muxer_stream_t));
131 if(!stream)
133 mp_msg(MSGT_MUXER, MSGL_ERR, "Could not allocate muxer_stream, EXIT.\n");
134 return NULL;
136 muxer->streams[muxer->avih.dwStreams] = stream;
137 stream->b_buffer = malloc(2048);
138 if(!stream->b_buffer)
140 mp_msg(MSGT_MUXER, MSGL_ERR, "Could not allocate b_buffer, EXIT.\n");
141 free(stream);
142 return NULL;
144 stream->b_buffer_size = 2048;
145 stream->b_buffer_ptr = 0;
146 stream->b_buffer_len = 0;
148 spriv = calloc(1, sizeof(muxer_stream_priv_t));
149 if(!spriv)
151 free(stream);
152 return NULL;
154 stream->priv = spriv;
156 spriv->avstream = av_new_stream(priv->oc, 1);
157 if(!spriv->avstream)
159 mp_msg(MSGT_MUXER, MSGL_ERR, "Could not allocate avstream, EXIT.\n");
160 return NULL;
162 spriv->avstream->stream_copy = 1;
164 ctx = spriv->avstream->codec;
165 ctx->codec_id = muxer->avih.dwStreams;
166 switch(type)
168 case MUXER_TYPE_VIDEO:
169 ctx->codec_type = CODEC_TYPE_VIDEO;
170 break;
171 case MUXER_TYPE_AUDIO:
172 ctx->codec_type = CODEC_TYPE_AUDIO;
173 break;
176 muxer->avih.dwStreams++;
177 stream->muxer = muxer;
178 stream->type = type;
179 mp_msg(MSGT_MUXER, MSGL_V, "ALLOCATED STREAM N. %d, type=%d\n", muxer->avih.dwStreams, type);
180 return stream;
184 static void fix_parameters(muxer_stream_t *stream)
186 muxer_stream_priv_t *spriv = (muxer_stream_priv_t *) stream->priv;
187 AVCodecContext *ctx;
189 ctx = spriv->avstream->codec;
191 ctx->bit_rate= stream->avg_rate;
192 if(stream->wf && stream->wf->nAvgBytesPerSec && !ctx->bit_rate)
193 ctx->bit_rate = stream->wf->nAvgBytesPerSec * 8;
194 ctx->rc_buffer_size= stream->vbv_size;
195 ctx->rc_max_rate= stream->max_rate;
197 if(stream->type == MUXER_TYPE_AUDIO)
199 ctx->codec_id = mp_av_codec_get_id(mp_wav_taglists, stream->wf->wFormatTag);
200 #if 0 //breaks aac in mov at least
201 ctx->codec_tag = codec_get_wav_tag(ctx->codec_id);
202 #endif
203 mp_msg(MSGT_MUXER, MSGL_INFO, "AUDIO CODEC ID: %x, TAG: %x\n", ctx->codec_id, (uint32_t) ctx->codec_tag);
204 ctx->sample_rate = stream->wf->nSamplesPerSec;
205 // mp_msg(MSGT_MUXER, MSGL_INFO, "stream->h.dwSampleSize: %d\n", stream->h.dwSampleSize);
206 ctx->channels = stream->wf->nChannels;
207 if(stream->h.dwRate && (stream->h.dwScale * (int64_t)ctx->sample_rate) % stream->h.dwRate == 0)
208 ctx->frame_size= (stream->h.dwScale * (int64_t)ctx->sample_rate) / stream->h.dwRate;
209 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",
210 ctx->frame_size, stream->h.dwScale, ctx->sample_rate, stream->h.dwRate,
211 ctx->block_align, stream->wf->nBlockAlign, stream->wf->nAvgBytesPerSec);
212 ctx->block_align = stream->h.dwSampleSize;
213 if(stream->wf+1 && stream->wf->cbSize)
215 ctx->extradata = av_malloc(stream->wf->cbSize);
216 if(ctx->extradata != NULL)
218 ctx->extradata_size = stream->wf->cbSize;
219 memcpy(ctx->extradata, stream->wf+1, ctx->extradata_size);
221 else
222 mp_msg(MSGT_MUXER, MSGL_ERR, "MUXER_LAVF(audio stream) error! Could not allocate %d bytes for extradata.\n",
223 stream->wf->cbSize);
226 else if(stream->type == MUXER_TYPE_VIDEO)
228 ctx->codec_id = mp_av_codec_get_id(mp_bmp_taglists, stream->bih->biCompression);
229 if(ctx->codec_id <= 0 || force_fourcc)
230 ctx->codec_tag= stream->bih->biCompression;
231 mp_msg(MSGT_MUXER, MSGL_INFO, "VIDEO CODEC ID: %d\n", ctx->codec_id);
232 if (stream->imgfmt)
233 ctx->pix_fmt = imgfmt2pixfmt(stream->imgfmt);
234 ctx->width = stream->bih->biWidth;
235 ctx->height = stream->bih->biHeight;
236 ctx->bit_rate = 800000;
237 ctx->time_base.den = stream->h.dwRate;
238 ctx->time_base.num = stream->h.dwScale;
239 if(stream->bih+1 && (stream->bih->biSize > sizeof(BITMAPINFOHEADER)))
241 ctx->extradata_size = stream->bih->biSize - sizeof(BITMAPINFOHEADER);
242 ctx->extradata = av_malloc(ctx->extradata_size);
243 if(ctx->extradata != NULL)
244 memcpy(ctx->extradata, stream->bih+1, ctx->extradata_size);
245 else
247 mp_msg(MSGT_MUXER, MSGL_ERR, "MUXER_LAVF(video stream) error! Could not allocate %d bytes for extradata.\n",
248 ctx->extradata_size);
249 ctx->extradata_size = 0;
255 static void write_chunk(muxer_stream_t *stream, size_t len, unsigned int flags, double dts, double pts)
257 muxer_t *muxer = (muxer_t*) stream->muxer;
258 muxer_priv_t *priv = (muxer_priv_t *) muxer->priv;
259 muxer_stream_priv_t *spriv = (muxer_stream_priv_t *) stream->priv;
260 AVPacket pkt;
262 if(len)
264 av_init_packet(&pkt);
265 pkt.size = len;
266 pkt.stream_index= spriv->avstream->index;
267 pkt.data = stream->buffer;
269 if(flags & AVIIF_KEYFRAME)
270 pkt.flags |= PKT_FLAG_KEY;
271 else
272 pkt.flags = 0;
274 pkt.dts = (dts / av_q2d(priv->oc->streams[pkt.stream_index]->time_base) + 0.5);
275 pkt.pts = (pts / av_q2d(priv->oc->streams[pkt.stream_index]->time_base) + 0.5);
276 //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);
278 if(av_interleaved_write_frame(priv->oc, &pkt) != 0) //av_write_frame(priv->oc, &pkt)
280 mp_msg(MSGT_MUXER, MSGL_ERR, "Error while writing frame.\n");
284 return;
288 static void write_header(muxer_t *muxer)
290 muxer_priv_t *priv = (muxer_priv_t *) muxer->priv;
292 mp_tmsg(MSGT_MUXER, MSGL_INFO, "Writing header...\n");
293 av_write_header(priv->oc);
294 muxer->cont_write_header = NULL;
298 static void write_trailer(muxer_t *muxer)
300 int i;
301 muxer_priv_t *priv = (muxer_priv_t *) muxer->priv;
303 mp_tmsg(MSGT_MUXER, MSGL_INFO, "Writing index...\n");
304 av_write_trailer(priv->oc);
305 for(i = 0; i < priv->oc->nb_streams; i++)
307 av_freep(&(priv->oc->streams[i]));
310 av_freep(&priv->oc->pb);
312 av_free(priv->oc);
315 static void list_formats(void) {
316 AVOutputFormat *fmt;
317 mp_msg(MSGT_DEMUX, MSGL_INFO, "Available lavf output formats:\n");
318 for (fmt = first_oformat; fmt; fmt = fmt->next)
319 mp_msg(MSGT_DEMUX, MSGL_INFO, "%15s : %s\n", fmt->name, fmt->long_name);
322 extern char *out_filename;
323 int muxer_init_muxer_lavf(muxer_t *muxer)
325 muxer_priv_t *priv;
326 AVOutputFormat *fmt = NULL;
328 av_register_all();
330 if (conf_format && strcmp(conf_format, "help") == 0) {
331 list_formats();
332 return 0;
335 mp_msg(MSGT_MUXER, MSGL_WARN, "** MUXER_LAVF *****************************************************************\n");
336 mp_msg(MSGT_MUXER, MSGL_WARN,
337 "REMEMBER: MEncoder's libavformat muxing is presently broken and can generate\n"
338 "INCORRECT files in the presence of B-frames. Moreover, due to bugs MPlayer\n"
339 "will play these INCORRECT files as if nothing were wrong!\n"
340 "*******************************************************************************\n");
342 priv = calloc(1, sizeof(muxer_priv_t));
343 if(priv == NULL)
344 return 0;
346 priv->oc = av_alloc_format_context();
347 if(!priv->oc)
349 mp_msg(MSGT_MUXER, MSGL_FATAL, "Could not get format context.\n");
350 goto fail;
353 if(conf_format)
354 fmt = guess_format(conf_format, NULL, NULL);
355 if(! fmt)
356 fmt = guess_format(NULL, out_filename, NULL);
357 if(! fmt)
359 mp_msg(MSGT_MUXER, MSGL_FATAL, "Cannot get specified format.\n");
360 goto fail;
362 priv->oc->oformat = fmt;
365 if(av_set_parameters(priv->oc, NULL) < 0)
367 mp_msg(MSGT_MUXER, MSGL_FATAL, "invalid output format parameters\n");
368 goto fail;
370 priv->oc->packet_size= mux_packet_size;
371 priv->oc->mux_rate= mux_rate;
372 priv->oc->preload= (int)(mux_preload*AV_TIME_BASE);
373 priv->oc->max_delay= (int)(mux_max_delay*AV_TIME_BASE);
374 if (info_name)
375 av_strlcpy(priv->oc->title , info_name, sizeof(priv->oc->title ));
376 if (info_artist)
377 av_strlcpy(priv->oc->author , info_artist, sizeof(priv->oc->author ));
378 if (info_genre)
379 av_strlcpy(priv->oc->genre , info_genre, sizeof(priv->oc->genre ));
380 if (info_copyright)
381 av_strlcpy(priv->oc->copyright, info_copyright, sizeof(priv->oc->copyright));
382 if (info_comment)
383 av_strlcpy(priv->oc->comment , info_comment, sizeof(priv->oc->comment ));
385 if(mux_avopt){
386 if(parse_avopts(priv->oc, mux_avopt) < 0){
387 mp_msg(MSGT_MUXER,MSGL_ERR, "Your options /%s/ look like gibberish to me pal.\n", mux_avopt);
388 goto fail;
392 priv->oc->pb = av_alloc_put_byte(priv->buffer, BIO_BUFFER_SIZE, 1, muxer, NULL, mp_write, mp_seek);
393 if ((muxer->stream->flags & MP_STREAM_SEEK) != MP_STREAM_SEEK)
394 priv->oc->pb->is_streamed = 1;
396 muxer->priv = (void *) priv;
397 muxer->cont_new_stream = &lavf_new_stream;
398 muxer->cont_write_chunk = &write_chunk;
399 muxer->cont_write_header = &write_header;
400 muxer->cont_write_index = &write_trailer;
401 muxer->fix_stream_parameters = &fix_parameters;
402 mp_msg(MSGT_MUXER, MSGL_INFO, "OK, exit.\n");
403 return 1;
405 fail:
406 free(priv);
407 return 0;