12 #include "aviheader.h"
15 #include "stream/stream.h"
20 #ifdef USE_LIBAVFORMAT_SO
21 #include <ffmpeg/avformat.h>
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
;
41 //AVInputFormat *avif;
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
)
75 static int mp_close(URLContext
*h
)
81 static int mp_read(URLContext
*h
, unsigned char *buf
, int size
)
83 mp_msg(MSGT_MUXER
, MSGL_WARN
, "READ %d\n", size
);
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
);
103 else if(whence
== SEEK_END
)
106 if(stream_control(muxer
->stream
, STREAM_CTRL_GET_SIZE
, &size
) == STREAM_UNSUPPORTED
|| size
< pos
)
110 mp_msg(MSGT_MUXER
, MSGL_DBG2
, "SEEK %"PRIu64
"\n", (int64_t)pos
);
111 if(!stream_seek(muxer
->stream
, pos
))
117 static URLProtocol mp_protocol
= {
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
;
134 if(!muxer
|| (type
!= MUXER_TYPE_VIDEO
&& type
!= MUXER_TYPE_AUDIO
))
136 mp_msg(MSGT_MUXER
, MSGL_ERR
, "UNKNOW TYPE %d\n", type
);
140 stream
= calloc(1, sizeof(muxer_stream_t
));
143 mp_msg(MSGT_MUXER
, MSGL_ERR
, "Could not alloc muxer_stream, EXIT\n");
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");
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
));
164 stream
->priv
= spriv
;
166 spriv
->avstream
= av_new_stream(priv
->oc
, 1);
169 mp_msg(MSGT_MUXER
, MSGL_ERR
, "Could not alloc avstream, EXIT\n");
172 spriv
->avstream
->stream_copy
= 1;
174 ctx
= spriv
->avstream
->codec
;
175 ctx
->codec_id
= muxer
->avih
.dwStreams
;
178 case MUXER_TYPE_VIDEO
:
179 ctx
->codec_type
= CODEC_TYPE_VIDEO
;
181 case MUXER_TYPE_AUDIO
:
182 ctx
->codec_type
= CODEC_TYPE_AUDIO
;
186 muxer
->avih
.dwStreams
++;
187 stream
->muxer
= muxer
;
189 mp_msg(MSGT_MUXER
, MSGL_V
, "ALLOCATED STREAM N. %d, type=%d\n", muxer
->avih
.dwStreams
, type
);
194 static void fix_parameters(muxer_stream_t
*stream
)
196 muxer_stream_priv_t
*spriv
= (muxer_stream_priv_t
*) stream
->priv
;
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
);
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
);
232 mp_msg(MSGT_MUXER
, MSGL_ERR
, "MUXER_LAVF(audio stream) error! couldn't allocate %d bytes for extradata\n",
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
);
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
);
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
;
274 av_init_packet(&pkt
);
276 pkt
.stream_index
= spriv
->avstream
->index
;
277 pkt
.data
= stream
->buffer
;
279 if(flags
& AVIIF_KEYFRAME
)
280 pkt
.flags
|= PKT_FLAG_KEY
;
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");
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
)
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
);
325 static void list_formats(void) {
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
)
336 AVOutputFormat
*fmt
= NULL
;
337 char mp_filename
[256] = "menc://stream.dummy";
341 if (conf_format
&& strcmp(conf_format
, "help") == 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
));
357 priv
->oc
= av_alloc_format_context();
360 mp_msg(MSGT_MUXER
, MSGL_FATAL
, "Couldn't get format context\n");
365 fmt
= guess_format(conf_format
, NULL
, NULL
);
367 fmt
= guess_format(NULL
, out_filename
, NULL
);
370 mp_msg(MSGT_MUXER
, MSGL_FATAL
, "CAN'T GET SPECIFIED FORMAT\n");
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");
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
);
386 av_strlcpy(priv
->oc
->title
, info_name
, sizeof(priv
->oc
->title
));
388 av_strlcpy(priv
->oc
->author
, info_artist
, sizeof(priv
->oc
->author
));
390 av_strlcpy(priv
->oc
->genre
, info_genre
, sizeof(priv
->oc
->genre
));
392 av_strlcpy(priv
->oc
->copyright
, info_copyright
, sizeof(priv
->oc
->copyright
));
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");
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");