11 #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
;
35 #define BIO_BUFFER_SIZE 32768
38 //AVInputFormat *avif;
44 uint8_t buffer
[BIO_BUFFER_SIZE
];
50 } muxer_stream_priv_t
;
52 static char *conf_format
= NULL
;
53 static int mux_rate
= 0;
54 static int mux_packet_size
= 0;
55 static float mux_preload
= 0.5;
56 static float mux_max_delay
= 0.7;
57 static char *mux_avopt
= NULL
;
59 m_option_t lavfopts_conf
[] = {
60 {"format", &(conf_format
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
61 {"muxrate", &mux_rate
, CONF_TYPE_INT
, CONF_RANGE
, 0, INT_MAX
, NULL
},
62 {"packetsize", &mux_packet_size
, CONF_TYPE_INT
, CONF_RANGE
, 0, INT_MAX
, NULL
},
63 {"preload", &mux_preload
, CONF_TYPE_FLOAT
, CONF_RANGE
, 0, INT_MAX
, NULL
},
64 {"delay", &mux_max_delay
, CONF_TYPE_FLOAT
, CONF_RANGE
, 0, INT_MAX
, NULL
},
65 {"o", &mux_avopt
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
67 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
70 static int mp_write(void *opaque
, uint8_t *buf
, int size
)
72 muxer_t
*muxer
= opaque
;
73 return stream_write_buffer(muxer
->stream
, buf
, size
);
76 static int64_t mp_seek(void *opaque
, int64_t pos
, int whence
)
78 muxer_t
*muxer
= opaque
;
79 if(whence
== SEEK_CUR
)
81 off_t cur
= stream_tell(muxer
->stream
);
86 else if(whence
== SEEK_END
)
89 if(stream_control(muxer
->stream
, STREAM_CTRL_GET_SIZE
, &size
) == STREAM_UNSUPPORTED
|| size
< pos
)
93 mp_msg(MSGT_MUXER
, MSGL_DBG2
, "SEEK %"PRIu64
"\n", (int64_t)pos
);
94 if(!stream_seek(muxer
->stream
, pos
))
100 static muxer_stream_t
* lavf_new_stream(muxer_t
*muxer
, int type
)
102 muxer_priv_t
*priv
= muxer
->priv
;
103 muxer_stream_t
*stream
;
104 muxer_stream_priv_t
*spriv
;
107 if(!muxer
|| (type
!= MUXER_TYPE_VIDEO
&& type
!= MUXER_TYPE_AUDIO
))
109 mp_msg(MSGT_MUXER
, MSGL_ERR
, "UNKNOWN TYPE %d\n", type
);
113 stream
= calloc(1, sizeof(muxer_stream_t
));
116 mp_msg(MSGT_MUXER
, MSGL_ERR
, "Could not allocate muxer_stream, EXIT.\n");
119 muxer
->streams
[muxer
->avih
.dwStreams
] = stream
;
120 stream
->b_buffer
= malloc(2048);
121 if(!stream
->b_buffer
)
123 mp_msg(MSGT_MUXER
, MSGL_ERR
, "Could not allocate b_buffer, EXIT.\n");
127 stream
->b_buffer_size
= 2048;
128 stream
->b_buffer_ptr
= 0;
129 stream
->b_buffer_len
= 0;
131 spriv
= calloc(1, sizeof(muxer_stream_priv_t
));
137 stream
->priv
= spriv
;
139 spriv
->avstream
= av_new_stream(priv
->oc
, 1);
142 mp_msg(MSGT_MUXER
, MSGL_ERR
, "Could not allocate avstream, EXIT.\n");
145 spriv
->avstream
->stream_copy
= 1;
147 ctx
= spriv
->avstream
->codec
;
148 ctx
->codec_id
= muxer
->avih
.dwStreams
;
151 case MUXER_TYPE_VIDEO
:
152 ctx
->codec_type
= CODEC_TYPE_VIDEO
;
154 case MUXER_TYPE_AUDIO
:
155 ctx
->codec_type
= CODEC_TYPE_AUDIO
;
159 muxer
->avih
.dwStreams
++;
160 stream
->muxer
= muxer
;
162 mp_msg(MSGT_MUXER
, MSGL_V
, "ALLOCATED STREAM N. %d, type=%d\n", muxer
->avih
.dwStreams
, type
);
167 static void fix_parameters(muxer_stream_t
*stream
)
169 muxer_stream_priv_t
*spriv
= (muxer_stream_priv_t
*) stream
->priv
;
172 ctx
= spriv
->avstream
->codec
;
174 ctx
->bit_rate
= stream
->avg_rate
;
175 if(stream
->wf
&& stream
->wf
->nAvgBytesPerSec
&& !ctx
->bit_rate
)
176 ctx
->bit_rate
= stream
->wf
->nAvgBytesPerSec
* 8;
177 ctx
->rc_buffer_size
= stream
->vbv_size
;
178 ctx
->rc_max_rate
= stream
->max_rate
;
180 if(stream
->type
== MUXER_TYPE_AUDIO
)
182 ctx
->codec_id
= av_codec_get_id(mp_wav_taglists
, stream
->wf
->wFormatTag
);
183 #if 0 //breaks aac in mov at least
184 ctx
->codec_tag
= codec_get_wav_tag(ctx
->codec_id
);
186 mp_msg(MSGT_MUXER
, MSGL_INFO
, "AUDIO CODEC ID: %x, TAG: %x\n", ctx
->codec_id
, (uint32_t) ctx
->codec_tag
);
187 ctx
->sample_rate
= stream
->wf
->nSamplesPerSec
;
188 // mp_msg(MSGT_MUXER, MSGL_INFO, "stream->h.dwSampleSize: %d\n", stream->h.dwSampleSize);
189 ctx
->channels
= stream
->wf
->nChannels
;
190 if(stream
->h
.dwRate
&& (stream
->h
.dwScale
* (int64_t)ctx
->sample_rate
) % stream
->h
.dwRate
== 0)
191 ctx
->frame_size
= (stream
->h
.dwScale
* (int64_t)ctx
->sample_rate
) / stream
->h
.dwRate
;
192 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",
193 ctx
->frame_size
, stream
->h
.dwScale
, ctx
->sample_rate
, stream
->h
.dwRate
,
194 ctx
->block_align
, stream
->wf
->nBlockAlign
, stream
->wf
->nAvgBytesPerSec
);
195 ctx
->block_align
= stream
->h
.dwSampleSize
;
196 if(stream
->wf
+1 && stream
->wf
->cbSize
)
198 ctx
->extradata
= av_malloc(stream
->wf
->cbSize
);
199 if(ctx
->extradata
!= NULL
)
201 ctx
->extradata_size
= stream
->wf
->cbSize
;
202 memcpy(ctx
->extradata
, stream
->wf
+1, ctx
->extradata_size
);
205 mp_msg(MSGT_MUXER
, MSGL_ERR
, "MUXER_LAVF(audio stream) error! Could not allocate %d bytes for extradata.\n",
209 else if(stream
->type
== MUXER_TYPE_VIDEO
)
211 ctx
->codec_id
= av_codec_get_id(mp_bmp_taglists
, stream
->bih
->biCompression
);
212 if(ctx
->codec_id
<= 0 || force_fourcc
)
213 ctx
->codec_tag
= stream
->bih
->biCompression
;
214 mp_msg(MSGT_MUXER
, MSGL_INFO
, "VIDEO CODEC ID: %d\n", ctx
->codec_id
);
216 ctx
->pix_fmt
= imgfmt2pixfmt(stream
->imgfmt
);
217 ctx
->width
= stream
->bih
->biWidth
;
218 ctx
->height
= stream
->bih
->biHeight
;
219 ctx
->bit_rate
= 800000;
220 ctx
->time_base
.den
= stream
->h
.dwRate
;
221 ctx
->time_base
.num
= stream
->h
.dwScale
;
222 if(stream
->bih
+1 && (stream
->bih
->biSize
> sizeof(BITMAPINFOHEADER
)))
224 ctx
->extradata_size
= stream
->bih
->biSize
- sizeof(BITMAPINFOHEADER
);
225 ctx
->extradata
= av_malloc(ctx
->extradata_size
);
226 if(ctx
->extradata
!= NULL
)
227 memcpy(ctx
->extradata
, stream
->bih
+1, ctx
->extradata_size
);
230 mp_msg(MSGT_MUXER
, MSGL_ERR
, "MUXER_LAVF(video stream) error! Could not allocate %d bytes for extradata.\n",
231 ctx
->extradata_size
);
232 ctx
->extradata_size
= 0;
238 static void write_chunk(muxer_stream_t
*stream
, size_t len
, unsigned int flags
, double dts
, double pts
)
240 muxer_t
*muxer
= (muxer_t
*) stream
->muxer
;
241 muxer_priv_t
*priv
= (muxer_priv_t
*) muxer
->priv
;
242 muxer_stream_priv_t
*spriv
= (muxer_stream_priv_t
*) stream
->priv
;
247 av_init_packet(&pkt
);
249 pkt
.stream_index
= spriv
->avstream
->index
;
250 pkt
.data
= stream
->buffer
;
252 if(flags
& AVIIF_KEYFRAME
)
253 pkt
.flags
|= PKT_FLAG_KEY
;
257 pkt
.dts
= (dts
/ av_q2d(priv
->oc
->streams
[pkt
.stream_index
]->time_base
) + 0.5);
258 pkt
.pts
= (pts
/ av_q2d(priv
->oc
->streams
[pkt
.stream_index
]->time_base
) + 0.5);
259 //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);
261 if(av_interleaved_write_frame(priv
->oc
, &pkt
) != 0) //av_write_frame(priv->oc, &pkt)
263 mp_msg(MSGT_MUXER
, MSGL_ERR
, "Error while writing frame.\n");
271 static void write_header(muxer_t
*muxer
)
273 muxer_priv_t
*priv
= (muxer_priv_t
*) muxer
->priv
;
275 mp_msg(MSGT_MUXER
, MSGL_INFO
, MSGTR_WritingHeader
);
276 av_write_header(priv
->oc
);
277 muxer
->cont_write_header
= NULL
;
281 static void write_trailer(muxer_t
*muxer
)
284 muxer_priv_t
*priv
= (muxer_priv_t
*) muxer
->priv
;
286 mp_msg(MSGT_MUXER
, MSGL_INFO
, MSGTR_WritingTrailer
);
287 av_write_trailer(priv
->oc
);
288 for(i
= 0; i
< priv
->oc
->nb_streams
; i
++)
290 av_freep(&(priv
->oc
->streams
[i
]));
293 av_freep(&priv
->oc
->pb
);
298 static void list_formats(void) {
300 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "Available lavf output formats:\n");
301 for (fmt
= first_oformat
; fmt
; fmt
= fmt
->next
)
302 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "%15s : %s\n", fmt
->name
, fmt
->long_name
);
305 extern char *out_filename
;
306 int muxer_init_muxer_lavf(muxer_t
*muxer
)
309 AVOutputFormat
*fmt
= NULL
;
313 if (conf_format
&& strcmp(conf_format
, "help") == 0) {
318 mp_msg(MSGT_MUXER
, MSGL_WARN
, "** MUXER_LAVF *****************************************************************\n");
319 mp_msg(MSGT_MUXER
, MSGL_WARN
,
320 "REMEMBER: MEncoder's libavformat muxing is presently broken and can generate\n"
321 "INCORRECT files in the presence of B-frames. Moreover, due to bugs MPlayer\n"
322 "will play these INCORRECT files as if nothing were wrong!\n"
323 "*******************************************************************************\n");
325 priv
= (muxer_priv_t
*) calloc(1, sizeof(muxer_priv_t
));
329 priv
->oc
= av_alloc_format_context();
332 mp_msg(MSGT_MUXER
, MSGL_FATAL
, "Could not get format context.\n");
337 fmt
= guess_format(conf_format
, NULL
, NULL
);
339 fmt
= guess_format(NULL
, out_filename
, NULL
);
342 mp_msg(MSGT_MUXER
, MSGL_FATAL
, "Cannot get specified format.\n");
345 priv
->oc
->oformat
= fmt
;
348 if(av_set_parameters(priv
->oc
, NULL
) < 0)
350 mp_msg(MSGT_MUXER
, MSGL_FATAL
, "invalid output format parameters\n");
353 priv
->oc
->packet_size
= mux_packet_size
;
354 priv
->oc
->mux_rate
= mux_rate
;
355 priv
->oc
->preload
= (int)(mux_preload
*AV_TIME_BASE
);
356 priv
->oc
->max_delay
= (int)(mux_max_delay
*AV_TIME_BASE
);
358 av_strlcpy(priv
->oc
->title
, info_name
, sizeof(priv
->oc
->title
));
360 av_strlcpy(priv
->oc
->author
, info_artist
, sizeof(priv
->oc
->author
));
362 av_strlcpy(priv
->oc
->genre
, info_genre
, sizeof(priv
->oc
->genre
));
364 av_strlcpy(priv
->oc
->copyright
, info_copyright
, sizeof(priv
->oc
->copyright
));
366 av_strlcpy(priv
->oc
->comment
, info_comment
, sizeof(priv
->oc
->comment
));
369 if(parse_avopts(priv
->oc
, mux_avopt
) < 0){
370 mp_msg(MSGT_MUXER
,MSGL_ERR
, "Your options /%s/ look like gibberish to me pal.\n", mux_avopt
);
375 priv
->oc
->pb
= av_alloc_put_byte(priv
->buffer
, BIO_BUFFER_SIZE
, 1, muxer
, NULL
, mp_write
, mp_seek
);
376 if ((muxer
->stream
->flags
& STREAM_SEEK
) != STREAM_SEEK
)
377 priv
->oc
->pb
->is_streamed
= 1;
379 muxer
->priv
= (void *) priv
;
380 muxer
->cont_new_stream
= &lavf_new_stream
;
381 muxer
->cont_write_chunk
= &write_chunk
;
382 muxer
->cont_write_header
= &write_header
;
383 muxer
->cont_write_index
= &write_trailer
;
384 muxer
->fix_stream_parameters
= &fix_parameters
;
385 mp_msg(MSGT_MUXER
, MSGL_INFO
, "OK, exit.\n");