12 #include "aviheader.h"
15 #include "stream/stream.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
;
36 //AVInputFormat *avif;
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
)
70 static int mp_close(URLContext
*h
)
76 static int mp_read(URLContext
*h
, unsigned char *buf
, int size
)
78 mp_msg(MSGT_MUXER
, MSGL_WARN
, "READ %d\n", size
);
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
);
98 else if(whence
== SEEK_END
)
101 if(stream_control(muxer
->stream
, STREAM_CTRL_GET_SIZE
, &size
) == STREAM_UNSUPPORTED
|| size
< pos
)
105 mp_msg(MSGT_MUXER
, MSGL_DBG2
, "SEEK %"PRIu64
"\n", (int64_t)pos
);
106 if(!stream_seek(muxer
->stream
, pos
))
112 static URLProtocol mp_protocol
= {
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
;
129 if(!muxer
|| (type
!= MUXER_TYPE_VIDEO
&& type
!= MUXER_TYPE_AUDIO
))
131 mp_msg(MSGT_MUXER
, MSGL_ERR
, "UNKNOW TYPE %d\n", type
);
135 stream
= calloc(1, sizeof(muxer_stream_t
));
138 mp_msg(MSGT_MUXER
, MSGL_ERR
, "Could not alloc muxer_stream, EXIT\n");
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");
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
));
159 stream
->priv
= spriv
;
161 spriv
->avstream
= av_new_stream(priv
->oc
, 1);
164 mp_msg(MSGT_MUXER
, MSGL_ERR
, "Could not alloc avstream, EXIT\n");
167 spriv
->avstream
->stream_copy
= 1;
169 ctx
= spriv
->avstream
->codec
;
170 ctx
->codec_id
= muxer
->avih
.dwStreams
;
173 case MUXER_TYPE_VIDEO
:
174 ctx
->codec_type
= CODEC_TYPE_VIDEO
;
176 case MUXER_TYPE_AUDIO
:
177 ctx
->codec_type
= CODEC_TYPE_AUDIO
;
181 muxer
->avih
.dwStreams
++;
182 stream
->muxer
= muxer
;
184 mp_msg(MSGT_MUXER
, MSGL_V
, "ALLOCATED STREAM N. %d, type=%d\n", muxer
->avih
.dwStreams
, type
);
189 static void fix_parameters(muxer_stream_t
*stream
)
191 muxer_stream_priv_t
*spriv
= (muxer_stream_priv_t
*) stream
->priv
;
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
);
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
);
227 mp_msg(MSGT_MUXER
, MSGL_ERR
, "MUXER_LAVF(audio stream) error! couldn't allocate %d bytes for extradata\n",
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
);
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
);
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
;
269 av_init_packet(&pkt
);
271 pkt
.stream_index
= spriv
->avstream
->index
;
272 pkt
.data
= stream
->buffer
;
274 if(flags
& AVIIF_KEYFRAME
)
275 pkt
.flags
|= PKT_FLAG_KEY
;
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");
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
)
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
);
320 static void list_formats(void) {
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
)
331 AVOutputFormat
*fmt
= NULL
;
332 char mp_filename
[256] = "menc://stream.dummy";
336 if (conf_format
&& strcmp(conf_format
, "help") == 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
));
352 priv
->oc
= av_alloc_format_context();
355 mp_msg(MSGT_MUXER
, MSGL_FATAL
, "Couldn't get format context\n");
360 fmt
= guess_format(conf_format
, NULL
, NULL
);
362 fmt
= guess_format(NULL
, out_filename
, NULL
);
365 mp_msg(MSGT_MUXER
, MSGL_FATAL
, "CAN'T GET SPECIFIED FORMAT\n");
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");
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
);
381 av_strlcpy(priv
->oc
->title
, info_name
, sizeof(priv
->oc
->title
));
383 av_strlcpy(priv
->oc
->author
, info_artist
, sizeof(priv
->oc
->author
));
385 av_strlcpy(priv
->oc
->genre
, info_genre
, sizeof(priv
->oc
->genre
));
387 av_strlcpy(priv
->oc
->copyright
, info_copyright
, sizeof(priv
->oc
->copyright
));
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");
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");