2002-12-27 22:43:20 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <inttypes.h>
|
2002-12-28 00:48:07 +00:00
|
|
|
#include <unistd.h>
|
2002-12-27 22:43:20 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
2005-11-18 14:39:25 +00:00
|
|
|
#include "version.h"
|
2002-12-27 22:43:20 +00:00
|
|
|
|
2004-04-28 10:18:33 +00:00
|
|
|
#include "aviheader.h"
|
|
|
|
#include "ms_hdr.h"
|
2002-12-27 22:43:20 +00:00
|
|
|
|
|
|
|
#include "muxer.h"
|
2005-02-21 21:45:49 +00:00
|
|
|
#include "stream.h"
|
|
|
|
#include "demuxer.h"
|
|
|
|
#include "mp_msg.h"
|
|
|
|
#include "help_mp.h"
|
|
|
|
#include "stheader.h"
|
2002-12-27 22:43:20 +00:00
|
|
|
|
2003-01-19 00:33:11 +00:00
|
|
|
muxer_t *muxer_new_muxer(int type,FILE *f){
|
2006-03-12 22:14:00 +00:00
|
|
|
muxer_t* muxer=calloc(1,sizeof(muxer_t));
|
2006-03-12 18:13:11 +00:00
|
|
|
if(!muxer)
|
|
|
|
return NULL;
|
2003-01-19 00:33:11 +00:00
|
|
|
muxer->file = f;
|
2002-12-27 22:43:20 +00:00
|
|
|
switch (type) {
|
|
|
|
case MUXER_TYPE_MPEG:
|
2005-02-21 21:45:49 +00:00
|
|
|
if(! muxer_init_muxer_mpeg(muxer))
|
2006-03-12 22:14:00 +00:00
|
|
|
goto fail;
|
2002-12-27 22:43:20 +00:00
|
|
|
break;
|
2004-03-09 14:46:34 +00:00
|
|
|
case MUXER_TYPE_RAWVIDEO:
|
2005-02-21 21:45:49 +00:00
|
|
|
if(! muxer_init_muxer_rawvideo(muxer))
|
2006-03-12 22:14:00 +00:00
|
|
|
goto fail;
|
2004-03-09 14:46:34 +00:00
|
|
|
break;
|
2005-06-21 18:54:50 +00:00
|
|
|
case MUXER_TYPE_RAWAUDIO:
|
|
|
|
if(! muxer_init_muxer_rawaudio(muxer))
|
2006-03-12 22:14:00 +00:00
|
|
|
goto fail;
|
2005-06-21 18:54:50 +00:00
|
|
|
break;
|
2006-01-09 19:35:44 +00:00
|
|
|
#if defined(USE_LIBAVFORMAT) || defined(USE_LIBAVFORMAT_SO)
|
2005-02-21 23:18:31 +00:00
|
|
|
case MUXER_TYPE_LAVF:
|
|
|
|
if(! muxer_init_muxer_lavf(muxer))
|
2006-03-12 22:14:00 +00:00
|
|
|
goto fail;
|
2005-02-21 23:18:31 +00:00
|
|
|
break;
|
|
|
|
#endif
|
2002-12-27 22:43:20 +00:00
|
|
|
case MUXER_TYPE_AVI:
|
|
|
|
default:
|
2005-02-21 21:45:49 +00:00
|
|
|
if(! muxer_init_muxer_avi(muxer))
|
2006-03-12 22:14:00 +00:00
|
|
|
goto fail;
|
2002-12-27 22:43:20 +00:00
|
|
|
}
|
|
|
|
return muxer;
|
2006-03-12 22:14:00 +00:00
|
|
|
|
|
|
|
fail:
|
|
|
|
free(muxer);
|
|
|
|
return NULL;
|
2002-12-27 22:43:20 +00:00
|
|
|
}
|
2005-11-21 22:53:14 +00:00
|
|
|
|
|
|
|
/* buffer frames until we either:
|
|
|
|
* (a) have at least one frame from each stream
|
|
|
|
* (b) run out of memory */
|
2006-01-26 19:32:07 +00:00
|
|
|
void muxer_write_chunk(muxer_stream_t *s, size_t len, unsigned int flags, double dts, double pts) {
|
|
|
|
if(dts == MP_NOPTS_VALUE) dts= s->timer;
|
|
|
|
if(pts == MP_NOPTS_VALUE) pts= s->timer; // this is wrong
|
|
|
|
|
2005-11-21 22:53:14 +00:00
|
|
|
if (s->muxer->muxbuf_skip_buffer) {
|
2006-01-26 19:32:07 +00:00
|
|
|
s->muxer->cont_write_chunk(s, len, flags, dts, pts);
|
2005-11-21 22:53:14 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
int num = s->muxer->muxbuf_num++;
|
|
|
|
muxbuf_t *buf, *tmp;
|
|
|
|
|
|
|
|
tmp = realloc(s->muxer->muxbuf, (num+1) * sizeof(muxbuf_t));
|
|
|
|
if(!tmp) {
|
2005-11-29 22:04:57 +00:00
|
|
|
mp_msg(MSGT_MUXER, MSGL_FATAL, MSGTR_MuxbufReallocErr);
|
2005-11-21 22:53:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
s->muxer->muxbuf = tmp;
|
|
|
|
buf = s->muxer->muxbuf + num;
|
|
|
|
|
|
|
|
/* buffer this frame */
|
|
|
|
buf->stream = s;
|
2006-01-26 19:32:07 +00:00
|
|
|
buf->dts= dts;
|
|
|
|
buf->pts= pts;
|
2005-11-21 22:53:14 +00:00
|
|
|
buf->len = len;
|
|
|
|
buf->flags = flags;
|
2006-05-01 02:04:09 +00:00
|
|
|
buf->buffer = malloc(len);
|
2005-11-21 22:53:14 +00:00
|
|
|
if (!buf->buffer) {
|
2005-11-29 22:04:57 +00:00
|
|
|
mp_msg(MSGT_MUXER, MSGL_FATAL, MSGTR_MuxbufMallocErr);
|
2005-11-21 22:53:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
memcpy(buf->buffer, s->buffer, buf->len);
|
|
|
|
s->muxbuf_seen = 1;
|
|
|
|
|
|
|
|
/* see if we need to keep buffering */
|
|
|
|
s->muxer->muxbuf_skip_buffer = 1;
|
|
|
|
for (num = 0; s->muxer->streams[num]; ++num)
|
|
|
|
if (!s->muxer->streams[num]->muxbuf_seen)
|
|
|
|
s->muxer->muxbuf_skip_buffer = 0;
|
|
|
|
|
|
|
|
/* see if we can flush buffer now */
|
|
|
|
if (s->muxer->muxbuf_skip_buffer) {
|
2005-11-29 22:04:57 +00:00
|
|
|
mp_msg(MSGT_MUXER, MSGL_V, MSGTR_MuxbufSending, s->muxer->muxbuf_num);
|
2005-11-21 22:53:14 +00:00
|
|
|
|
|
|
|
/* fix parameters for all streams */
|
|
|
|
for (num = 0; s->muxer->streams[num]; ++num) {
|
|
|
|
muxer_stream_t *str = s->muxer->streams[num];
|
|
|
|
if(str->muxer->fix_stream_parameters)
|
|
|
|
muxer_stream_fix_parameters(str->muxer, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write header */
|
|
|
|
if (s->muxer->cont_write_header)
|
|
|
|
muxer_write_header(s->muxer);
|
|
|
|
|
|
|
|
/* send all buffered frames to muxer */
|
|
|
|
for (num = 0; num < s->muxer->muxbuf_num; ++num) {
|
2006-01-26 19:32:07 +00:00
|
|
|
muxbuf_t tmp_buf;
|
2005-11-21 22:53:14 +00:00
|
|
|
buf = s->muxer->muxbuf + num;
|
|
|
|
s = buf->stream;
|
|
|
|
|
|
|
|
/* 1. save timer and buffer (might have changed by now) */
|
2006-01-26 19:32:07 +00:00
|
|
|
tmp_buf.dts = s->timer;
|
|
|
|
tmp_buf.buffer = s->buffer;
|
2005-11-21 22:53:14 +00:00
|
|
|
|
|
|
|
/* 2. move stored timer and buffer into stream and mux it */
|
2006-01-26 19:32:07 +00:00
|
|
|
s->timer = buf->dts;
|
2005-11-21 22:53:14 +00:00
|
|
|
s->buffer = buf->buffer;
|
2006-01-26 19:32:07 +00:00
|
|
|
s->muxer->cont_write_chunk(s, buf->len, buf->flags, buf->dts, buf->pts);
|
2005-11-21 22:53:14 +00:00
|
|
|
|
|
|
|
/* 3. restore saved timer and buffer */
|
2006-01-26 19:32:07 +00:00
|
|
|
s->timer = tmp_buf.dts;
|
|
|
|
s->buffer = tmp_buf.buffer;
|
2005-11-21 22:53:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(s->muxer->muxbuf);
|
|
|
|
s->muxer->muxbuf_num = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this code moved directly from muxer_avi.c */
|
|
|
|
// alter counters:
|
|
|
|
if(s->h.dwSampleSize){
|
|
|
|
// CBR
|
|
|
|
s->h.dwLength+=len/s->h.dwSampleSize;
|
2005-11-29 22:04:57 +00:00
|
|
|
if(len%s->h.dwSampleSize) mp_msg(MSGT_MUXER, MSGL_WARN, MSGTR_WarningLenIsntDivisible);
|
2005-11-21 22:53:14 +00:00
|
|
|
} else {
|
|
|
|
// VBR
|
|
|
|
s->h.dwLength++;
|
|
|
|
}
|
|
|
|
s->timer=(double)s->h.dwLength*s->h.dwScale/s->h.dwRate;
|
|
|
|
s->size+=len;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|