| 1 |
/* dvbcut |
| 2 |
Copyright (c) 2005 Sven Over <[email protected]> |
| 3 |
|
| 4 |
This program 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. |
| 8 |
|
| 9 |
This program 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. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program; if not, write to the Free Software |
| 16 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 |
*/ |
| 18 |
|
| 19 |
/* $Id$ */ |
| 20 |
|
| 21 |
extern "C" { |
| 22 |
#include <libavformat/avformat.h> |
| 23 |
#include <libavcodec/avcodec.h> |
| 24 |
#include <libavutil/opt.h> |
| 25 |
} |
| 26 |
#include <cstring> |
| 27 |
#include <utility> |
| 28 |
#include <list> |
| 29 |
#include "avframe.h" |
| 30 |
#include "streamhandle.h" |
| 31 |
#include "lavfmuxer.h" |
| 32 |
|
| 33 |
#include <stdio.h> |
| 34 |
|
| 35 |
lavfmuxer::lavfmuxer(const char *format, uint32_t audiostreammask, mpgfile &mpg, const char *filename) |
| 36 |
: muxer(), avfc(0), fileopened(false) |
| 37 |
{ |
| 38 |
auto fmt_ = av_guess_format(format, NULL, NULL); |
| 39 |
if (!fmt_) { |
| 40 |
return; |
| 41 |
} |
| 42 |
fmt = fmt_; |
| 43 |
|
| 44 |
if (avformat_alloc_output_context2(&avfc, fmt_, NULL, av_strdup(filename ? filename : "")) < 0) |
| 45 |
return; |
| 46 |
|
| 47 |
av_opt_set_int(avfc, "preload", (int)(.5 * AV_TIME_BASE), AV_OPT_SEARCH_CHILDREN); |
| 48 |
av_opt_set_int(avfc, "muxrate", 10080000, AV_OPT_SEARCH_CHILDREN); |
| 49 |
avfc->max_delay= (int)(.7*AV_TIME_BASE); |
| 50 |
|
| 51 |
int id=0; |
| 52 |
|
| 53 |
st[VIDEOSTREAM].stream_index=id; |
| 54 |
|
| 55 |
strpres[VIDEOSTREAM]=true; |
| 56 |
mpg.setvideoencodingparameters(); |
| 57 |
AVCodecContext* codec = mpg.getavcc(VIDEOSTREAM); |
| 58 |
codec->rc_buffer_size = 224*1024*8; |
| 59 |
AVStream *s = st[VIDEOSTREAM].avstr = avformat_new_stream(avfc, codec->codec); |
| 60 |
s->id = id++; |
| 61 |
s->sample_aspect_ratio = codec->sample_aspect_ratio; |
| 62 |
s->time_base = codec->time_base; |
| 63 |
|
| 64 |
for (int i=0;i<mpg.getaudiostreams();++i) |
| 65 |
if (audiostreammask & (1u<<i)) { |
| 66 |
int astr=audiostream(i); |
| 67 |
st[astr].stream_index=id; |
| 68 |
codec = avcodec_alloc_context3(NULL); |
| 69 |
codec->codec_type = AVMEDIA_TYPE_AUDIO; |
| 70 |
codec->codec_id = (mpg.getstreamtype(astr)==streamtype::ac3audio) ? AV_CODEC_ID_AC3 : AV_CODEC_ID_MP2; |
| 71 |
codec->rc_buffer_size = 224*1024*8; |
| 72 |
s = st[astr].avstr = avformat_new_stream(avfc, codec->codec); |
| 73 |
s->id = id++; |
| 74 |
strpres[astr]=true; |
| 75 |
|
| 76 |
// Must read some packets to get codec parameters |
| 77 |
streamhandle sh(mpg.getinitialoffset()); |
| 78 |
streamdata *sd=sh.newstream(astr,mpg.getstreamtype(astr),mpg.istransportstream()); |
| 79 |
|
| 80 |
while (sh.fileposition < mpg.getinitialoffset()+(4<<20)) { |
| 81 |
if (mpg.streamreader(sh)<=0) |
| 82 |
break; |
| 83 |
|
| 84 |
if (sd->getitemlistsize() > 1) { |
| 85 |
if (!avcodec_open2(codec, |
| 86 |
avcodec_find_decoder(codec->codec_id), NULL)) { |
| 87 |
AVFrame *frame = av_frame_alloc(); |
| 88 |
AVPacket* pkt = av_packet_alloc(); |
| 89 |
|
| 90 |
pkt->data = (uint8_t*)sd->getdata(); |
| 91 |
pkt->size = sd->inbytes(); |
| 92 |
|
| 93 |
avcodec_send_packet(codec, pkt); |
| 94 |
avcodec_receive_frame(codec, frame); |
| 95 |
|
| 96 |
av_packet_free(&pkt); |
| 97 |
av_frame_free(&frame); |
| 98 |
avcodec_close(codec); |
| 99 |
} |
| 100 |
break; |
| 101 |
} |
| 102 |
} |
| 103 |
s->time_base = codec->time_base; |
| 104 |
} |
| 105 |
|
| 106 |
if (!(fmt->flags & AVFMT_NOFILE)&&(avio_open(&avfc->pb, filename, AVIO_FLAG_WRITE) < 0)) { |
| 107 |
av_free(avfc); |
| 108 |
avfc=0; |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
av_opt_set_int(avfc, "preload", (int)(.5 * AV_TIME_BASE), AV_OPT_SEARCH_CHILDREN); |
| 113 |
av_opt_set_int(avfc, "muxrate", 10080000, AV_OPT_SEARCH_CHILDREN); |
| 114 |
avfc->max_delay= (int)(.7*AV_TIME_BASE); |
| 115 |
|
| 116 |
av_dump_format(avfc, 0, filename, 1); |
| 117 |
int ret = avformat_write_header(avfc, NULL); |
| 118 |
if (ret < 0) { |
| 119 |
fprintf(stderr, "avformat_write_header failed ret[%d]\n", ret); |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
fileopened = true; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
lavfmuxer::~lavfmuxer() |
| 128 |
{ |
| 129 |
if (avfc) { |
| 130 |
if (fileopened) { |
| 131 |
av_write_trailer(avfc); |
| 132 |
if (!(fmt->flags & AVFMT_NOFILE)) |
| 133 |
avio_close(avfc->pb); |
| 134 |
} |
| 135 |
|
| 136 |
av_free(avfc); |
| 137 |
} |
| 138 |
} |
| 139 |
|