[Libav-user] av_read_frame not discarding streams and/or programs from mpegts marked as AVDISCARD_ALL
Piotr Woźniak
woziosek at gmail.com
Sun Oct 23 15:46:39 EEST 2016
Hi,
I have a problem with remuxing mpegts output from DVB DVR device to mpegts
with selected streams only. DVR file contains while transport stream from
transponder and I want to remux it to transport stream containing only
selected service. First, av_read_frame returns all streams, seems that
marking streams and probrams as AVDISCARD_ALL doesn't have an effect on it.
Other problem is that after some number of demuxed frames it starts to
return AVERROR_BUFFER_TOO_SMALL. Here is the code (sorry for mess, it is
proof of concept code before cleaning up):
void log_packet(const AVFormatContext *fmt_ctx, const AVPacket *pkt)
{
LOG(TRACE) << pkt->stream_index << ":" << (pkt->flags & AV_PKT_FLAG_KEY ?
" K" : "");
}
void thread_exec()
{
AVOutputFormat *ofmt = NULL;
AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;
const char *in_filename, *out_filename;
in_filename = "/dev/dvb/adapter0/dvr0";
out_filename = "stream.ts";
//av_log_set_level(AV_LOG_QUIET);
av_register_all();
LOG(DEBUG) << "Opening input stream";
AVInputFormat ifmt;
memset(&ifmt, 0, sizeof(ifmt));
ifmt.name = "mpegts";
//ifmt.flags = AVFMT_NOFILE;
if (avformat_open_input(&ifmt_ctx, in_filename, /*&ifmt*/0, 0) < 0)
{
printf("Could not open input file '%s'", in_filename);
return;
}
if (avformat_find_stream_info(ifmt_ctx, 0) < 0)
{
printf("Failed to retrieve input stream information");
return;
}
av_dump_format(ifmt_ctx, 0, in_filename, 0);
LOG(DEBUG) << "Number of input programs: " << ifmt_ctx->nb_programs;
LOG(DEBUG) << "Number of input streams: " << ifmt_ctx->nb_streams;
LOG(DEBUG) << "Streams for program " << 27;
// finding program index
// discard everything else
int program_index = -1;
for (size_t i = 0; i < ifmt_ctx->nb_programs; i++)
{
if (ifmt_ctx->programs[i]->id == 27)
{
program_index = i;
for (size_t j = 0; j < ifmt_ctx->programs[i]->nb_stream_indexes; j++)
{
LOG(DEBUG) << ifmt_ctx->programs[i]->stream_index[j];
}
}
else
{
ifmt_ctx->programs[i]->discard = AVDISCARD_ALL;
}
}
// LUT input stream index to output stream index
std::unique_ptr<int[]> in_to_out(new int[ifmt_ctx->nb_streams]);
// discard all streams by default
for (size_t i = 0; i < ifmt_ctx->nb_streams; i++)
{
ifmt_ctx->streams[i]->discard = AVDISCARD_ALL;
in_to_out[i] = -1;
}
avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);
if (!ofmt_ctx)
{
printf("Could not create output context\n");
return;
}
// copying streams from input to output for given program
ofmt = ofmt_ctx->oformat;
for (size_t i = 0; i <
ifmt_ctx->programs[program_index]->nb_stream_indexes; i++)
{
int si = ifmt_ctx->programs[program_index]->stream_index[i];
AVStream *in_stream = ifmt_ctx->streams[si];
// getting codec parameters from input stream, only known media types
// should be demuxed and muxed back
switch (in_stream->codecpar->codec_type)
{
case AVMEDIA_TYPE_VIDEO:
case AVMEDIA_TYPE_AUDIO:
case AVMEDIA_TYPE_SUBTITLE:
{
LOG(DEBUG) << "copying stream " << si << "->" << i;
in_to_out[si] = i;
in_stream->discard = AVDISCARD_DEFAULT;
AVStream *out_stream = avformat_new_stream(ofmt_ctx, 0);
if (!out_stream)
{
printf("Failed allocating output stream\n");
return;
}
if (avcodec_parameters_copy(out_stream->codecpar,
in_stream->codecpar) < 0)
{
printf("Cannot get parameters from context\n");
return;
}
break;
}
default:
break;
}
}
LOG(DEBUG) << "Number of output streams: " << ofmt_ctx->nb_streams;
av_dump_format(ofmt_ctx, 0, out_filename, 1);
if (avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE) < 0)
{
printf("Could not open output file '%s'", out_filename);
return;
}
if (avformat_write_header(ofmt_ctx, NULL) < 0)
{
printf("Error occurred when writing header to output file\n");
return;
}
// reading input stream
AVPacket pkt;
while (continue_)
{
av_init_packet(&pkt);
auto ret = av_read_frame(ifmt_ctx, &pkt);
if (ret < 0)
{
char buf[10240];
av_strerror(ret, buf, 10240);
LOG(WARNING) << "av_read_frame returned error: " << buf;
av_packet_unref(&pkt);
this_thread::sleep_for(std::chrono::milliseconds(100));
continue;
}
log_packet(ifmt_ctx, &pkt);
int osi = in_to_out[pkt.stream_index];
if (osi != -1)
{
AVStream *in_stream, *out_stream;
in_stream = ifmt_ctx->streams[pkt.stream_index];
out_stream = ofmt_ctx->streams[osi];
/* copy packet */
pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base,
out_stream->time_base, (enum
AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base,
out_stream->time_base, (enum
AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base,
out_stream->time_base);
pkt.pos = -1;
pkt.stream_index = osi;
if (av_interleaved_write_frame(ofmt_ctx, &pkt) < 0)
{
fprintf(stderr, "Error muxing packet\n");
break;
}
}
av_packet_unref(&pkt);
}
av_write_trailer(ofmt_ctx);
avformat_close_input(&ifmt_ctx);
/* close output */
if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))
avio_close(ofmt_ctx->pb);
avformat_free_context(ofmt_ctx);
}
And output:
[2016-10-23 11:26:44,540] [D] [2904533824] [file-reader.cpp:65] Opening
input stream [NULL @ 0xafa54100] SPS unavailable in decode_picture_timing
[NULL @ 0xafa54100] non-existing PPS 0 referenced [h264 @ 0xafa54100] SPS
unavailable in decode_picture_timing [h264 @ 0xafa54100] non-existing PPS 0
referenced [h264 @ 0xafa54100] decode_slice_header error [h264 @
0xafa54100] no frame! ... [h264 @ 0xafa677e0] SPS unavailable in
decode_picture_timing [h264 @ 0xafa677e0] non-existing PPS 0 referenced
[h264 @ 0xafa677e0] SPS unavailable in decode_picture_timing [h264 @
0xafa677e0] non-existing PPS 0 referenced [h264 @ 0xafa677e0]
decode_slice_header error [h264 @ 0xafa677e0] no frame! [mpegts @
0xafa07da0] PES packet size mismatch [mpegts @ 0xafa07da0] PES packet size
mismatch [h264 @ 0xafa677e0] SPS unavailable in decode_picture_timing [h264
@ 0xafa677e0] non-existing PPS 0 referenced [h264 @ 0xafa30b00] error while
decoding MB 34 20, bytestream -10 [h264 @ 0xafa30b00] concealing 735 DC,
735 AC, 735 MV errors in B frame [h264 @ 0xafa54100] error while decoding
MB 11 20, bytestream -63 [h264 @ 0xafa54100] concealing 758 DC, 758 AC, 758
MV errors in I frame [h264 @ 0xafa57ac0] Increasing reorder buffer to 2
[h264 @ 0xafa57ac0] error while decoding MB 1 10, bytestream -12 [h264 @
0xafa57ac0] concealing 1218 DC, 1218 AC, 1218 MV errors in B frame [h264 @
0xafa61320] error while decoding MB 26 6, bytestream -14 [h264 @
0xafa61320] concealing 1373 DC, 1373 AC, 1373 MV errors in B frame [h264 @
0xafa677e0] SPS unavailable in decode_picture_timing [h264 @ 0xafa677e0]
non-existing PPS 0 referenced [h264 @ 0xafa677e0] decode_slice_header error
[h264 @ 0xafa677e0] no frame! [mpegts @ 0xafa07da0] decoding for stream 2
failed [mpegts @ 0xafa07da0] decoding for stream 3 failed [mpegts @
0xafa07da0] decoding for stream 4 failed [mpegts @ 0xafa07da0] decoding for
stream 7 failed [mpegts @ 0xafa07da0] decoding for stream 13 failed [mpegts
@ 0xafa07da0] decoding for stream 17 failed [mpegts @ 0xafa07da0] Could not
find codec parameters for stream 6 (Unknown: none ([5][0][0][0] / 0x0005)):
unknown codec Consider increasing the value for the 'analyzeduration' and
'probesize' options [mpegts @ 0xafa07da0] Could not find codec parameters
for stream 17 (Video: h264 ([27][0][0][0] / 0x001B), none): unspecified
size Consider increasing the value for the 'analyzeduration' and
'probesize' options [mpegts @ 0xafa07da0] Could not find codec parameters
for stream 28 (Unknown: none ([5][0][0][0] / 0x0005)): unknown codec
Consider increasing the value for the 'analyzeduration' and 'probesize'
options [mpegts @ 0xafa07da0] Could not find codec parameters for stream 29
(Unknown: none ([5][0][0][0] / 0x0005)): unknown codec Consider increasing
the value for the 'analyzeduration' and 'probesize' options [mpegts @
0xafa07da0] Could not find codec parameters for stream 31 (Unknown: none
([5][0][0][0] / 0x0005)): unknown codec Consider increasing the value for
the 'analyzeduration' and 'probesize' options [mpegts @ 0xafa07da0] Could
not find codec parameters for stream 33 (Unknown: none ([5][0][0][0] /
0x0005)): unknown codec Consider increasing the value for the
'analyzeduration' and 'probesize' options [mpegts @ 0xafa07da0] Could not
find codec parameters for stream 34 (Unknown: none ([5][0][0][0] /
0x0005)): unknown codec Consider increasing the value for the
'analyzeduration' and 'probesize' options Input #0, mpegts, from
'/dev/dvb/adapter0/dvr0': Duration: N/A, start: 29689.269567, bitrate: N/A
Program 27 Metadata: service_name : 8TV service_provider: EmiTel Stream
#0:7[0xa8e]: Video: h264 (Main) ([27][0][0][0] / 0x001B), yuv420p, 720x576
[SAR 16:11 DAR 20:11], 25 fps, 25 tbr, 90k tbn, 50 tbc Stream
#0:21[0xa8f](pol): Audio: mp2 ([3][0][0][0] / 0x0003), 48000 Hz, stereo,
s16p, 256 kb/s Stream #0:33[0xa93]: Unknown: none ([5][0][0][0] / 0x0005)
Program 28 Metadata: service_name : TTV service_provider: EmiTel Stream
#0:3[0xaf2]: Video: h264 (Main) ([27][0][0][0] / 0x001B), yuv420p, 720x576
[SAR 16:11 DAR 20:11], 25 fps, 25 tbr, 90k tbn, 50 tbc Stream
#0:24[0xaf3](pol): Audio: mp2 ([3][0][0][0] / 0x0003), 48000 Hz, stereo,
s16p, 192 kb/s Stream #0:22[0xaf4](mul): Audio: ac3 ([6][0][0][0] /
0x0006), 48000 Hz, stereo, fltp, 384 kb/s Stream #0:18[0xaf5](pol,pol):
Subtitle: dvb_teletext ([6][0][0][0] / 0x0006), 492x250 Stream
#0:34[0xaf7]: Unknown: none ([5][0][0][0] / 0x0005) Program 29 Metadata:
service_name : Polo TV service_provider: EmiTel Stream #0:4[0xb56]: Video:
h264 (Main) ([27][0][0][0] / 0x001B), yuv420p, 720x576 [SAR 16:11 DAR
20:11], 25 fps, 25 tbr, 90k tbn, 50 tbc Stream #0:5[0xb57](pol): Audio: mp2
([3][0][0][0] / 0x0003), 48000 Hz, stereo, s16p, 256 kb/s Stream
#0:6[0xb5b]: Unknown: none ([5][0][0][0] / 0x0005) Program 30 Metadata:
service_name : ATM Rozrywka service_provider: EmiTel Stream #0:15[0xbba]:
Video: h264 (Main) ([27][0][0][0] / 0x001B), yuv420p, 720x576 [SAR 16:11
DAR 20:11], 25 fps, 25 tbr, 90k tbn, 50 tbc Stream #0:11[0xbbb](pol):
Audio: mp2 ([3][0][0][0] / 0x0003), 48000 Hz, stereo, s16p, 192 kb/s Stream
#0:8[0xbbd](pol): Subtitle: dvb_teletext ([6][0][0][0] / 0x0006), 492x250
Stream #0:32[0xbbe](pol): Subtitle: dvb_subtitle ([6][0][0][0] / 0x0006)
(hearing impaired) Stream #0:12[0xbc0](aux): Audio: mp2 ([3][0][0][0] /
0x0003), 48000 Hz, stereo, s16p, 192 kb/s (visual impaired) Program 50
Metadata: service_name : TV Trwam service_provider: EmiTel Stream
#0:0[0x138b](pol): Audio: mp2 ([3][0][0][0] / 0x0003), 48000 Hz, stereo,
s16p, 192 kb/s Stream #0:1[0x138d](pol): Subtitle: dvb_teletext
([6][0][0][0] / 0x0006) Stream #0:2[0x1394]: Video: h264 (Main)
([27][0][0][0] / 0x001B), yuv420p, 720x576 [SAR 16:11 DAR 20:11], 25 fps,
25 tbr, 90k tbn, 50 tbc Program 51 Metadata: service_name : TVP ABC
service_provider: EmiTel Stream #0:13[0x13ee]: Video: h264 (Main)
([27][0][0][0] / 0x001B), yuv420p, 720x576 [SAR 12:11 DAR 15:11], 25 fps,
25 tbr, 90k tbn, 50 tbc Stream #0:9[0x13ef](pol): Audio: mp2 ([3][0][0][0]
/ 0x0003), 48000 Hz, stereo, s16p, 192 kb/s Stream #0:23[0x13f0](org):
Audio: ac3 (AC-3 / 0x332D4341), 48000 Hz, stereo, fltp, 384 kb/s Stream
#0:30[0x13f2](pol): Subtitle: dvb_subtitle ([6][0][0][0] / 0x0006) Stream
#0:31[0x13f3]: Unknown: none ([5][0][0][0] / 0x0005) Stream
#0:14[0x13f4](aux): Audio: eac3 (EAC3 / 0x33434145), 48000 Hz, stereo,
fltp, 96 kb/s (visual impaired) Program 52 Metadata: service_name :
Stopklatka TV service_provider: EmiTel Stream #0:26[0x1452]: Video: h264
(Main) ([27][0][0][0] / 0x001B), yuv420p, 720x576 [SAR 16:11 DAR 20:11], 25
fps, 25 tbr, 90k tbn, 50 tbc Stream #0:16[0x1453](pol): Audio: mp2
([3][0][0][0] / 0x0003), 48000 Hz, stereo, s16p, 192 kb/s Stream
#0:25[0x1454](org): Audio: eac3 ([6][0][0][0] / 0x0006), 48000 Hz, stereo,
fltp, 256 kb/s Stream #0:27[0x1456](pol): Subtitle: dvb_subtitle
([6][0][0][0] / 0x0006) Stream #0:28[0x1457]: Unknown: none ([5][0][0][0] /
0x0005) Program 53 Metadata: service_name : Fokus TV service_provider:
EmiTel Stream #0:17[0x14b6]: Video: h264 ([27][0][0][0] / 0x001B), none, 25
fps, 25 tbr, 90k tbn Stream #0:20[0x14b7](pol): Audio: mp2 ([3][0][0][0] /
0x0003), 48000 Hz, stereo, s16p, 256 kb/s Stream #0:10[0x14b9](pol,pol):
Subtitle: dvb_teletext ([6][0][0][0] / 0x0006), 492x250 Stream
#0:29[0x14bb]: Unknown: none ([5][0][0][0] / 0x0005) Stream
#0:19[0x14bc](aux): Audio: mp2 ([3][0][0][0] / 0x0003), 48000 Hz, stereo,
s16p, 128 kb/s (visual impaired) [2016-10-23 11:26:46,705] [D] [2904533824]
[file-reader.cpp:88] Number of input programs: 8 [2016-10-23 11:26:46,706]
[D] [2904533824] [file-reader.cpp:89] Number of input streams: 35
[2016-10-23 11:26:46,706] [D] [2904533824] [file-reader.cpp:91] Streams for
program 27 [2016-10-23 11:26:46,706] [D] [2904533824] [file-reader.cpp:102]
7 [2016-10-23 11:26:46,706] [D] [2904533824] [file-reader.cpp:102] 21
[2016-10-23 11:26:46,706] [D] [2904533824] [file-reader.cpp:102] 33
[2016-10-23 11:26:46,707] [D] [2904533824] [file-reader.cpp:145] copying
stream 7->0 [2016-10-23 11:26:46,707] [D] [2904533824]
[file-reader.cpp:145] copying stream 21->1 [2016-10-23 11:26:46,707] [D]
[2904533824] [file-reader.cpp:165] Number of output streams: 2 Output #0,
mpegts, to 'stream.ts': Stream #0:0: Video: h264 (Main) ([27][0][0][0] /
0x001B), yuv420p, 720x576 [SAR 16:11 DAR 20:11], q=2-31 Stream #0:1: Audio:
mp2 ([3][0][0][0] / 0x0003), 48000 Hz, stereo, s16p, 256 kb/s [2016-10-23
11:26:46,709] [T] [2904533824] [file-reader.cpp:47] 10: K [2016-10-23
11:26:46,709] [T] [2904533824] [file-reader.cpp:47] 8: K [2016-10-23
11:26:46,709] [T] [2904533824] [file-reader.cpp:47] 18: K [2016-10-23
11:26:46,709] [T] [2904533824] [file-reader.cpp:47] 3: [2016-10-23
11:26:46,711] [T] [2904533824] [file-reader.cpp:47] 1: K [2016-10-23
11:26:46,712] [T] [2904533824] [file-reader.cpp:47] 13: [2016-10-23
11:26:46,712] [T] [2904533824] [file-reader.cpp:47] 10: K [2016-10-23
11:26:46,712] [T] [2904533824] [file-reader.cpp:47] 8: K [2016-10-23
11:26:46,712] [T] [2904533824] [file-reader.cpp:47] 4: ... [2016-10-23
11:26:46,870] [T] [2904533824] [file-reader.cpp:47] 7: [2016-10-23
11:26:46,870] [T] [2904533824] [file-reader.cpp:47] 9: K [2016-10-23
11:26:46,871] [T] [2904533824] [file-reader.cpp:47] 11: K [2016-10-23
11:26:46,871] [T] [2904533824] [file-reader.cpp:47] 12: K [2016-10-23
11:26:46,871] [T] [2904533824] [file-reader.cpp:47] 13: [2016-10-23
11:26:46,873] [T] [2904533824] [file-reader.cpp:47] 14: K [2016-10-23
11:26:46,873] [T] [2904533824] [file-reader.cpp:47] 15: [2016-10-23
11:26:46,873] [T] [2904533824] [file-reader.cpp:47] 16: K [2016-10-23
11:26:46,873] [T] [2904533824] [file-reader.cpp:47] 17: [2016-10-23
11:26:46,874] [T] [2904533824] [file-reader.cpp:47] 19: K [2016-10-23
11:26:46,874] [T] [2904533824] [file-reader.cpp:47] 20: K [2016-10-23
11:26:46,874] [T] [2904533824] [file-reader.cpp:47] 21: K [2016-10-23
11:26:46,875] [T] [2904533824] [file-reader.cpp:47] 22: K [2016-10-23
11:26:46,875] [T] [2904533824] [file-reader.cpp:47] 23: K [2016-10-23
11:26:46,875] [T] [2904533824] [file-reader.cpp:47] 24: K [2016-10-23
11:26:46,875] [T] [2904533824] [file-reader.cpp:47] 25: K [2016-10-23
11:26:46,875] [T] [2904533824] [file-reader.cpp:47] 26: [2016-10-23
11:26:46,876] [W] [2904533824] [file-reader.cpp:188] av_read_frame returned
error: Value too large for defined data type [2016-10-23 11:26:46,976] [W]
[2904533824] [file-reader.cpp:188] av_read_frame returned error: Value too
large for defined data type [2016-10-23 11:26:47,076] [W] [2904533824]
[file-reader.cpp:188] av_read_frame returned error: Value too large for
defined data type [2016-10-23 11:26:47,177] [W] [2904533824]
[file-reader.cpp:188] av_read_frame returned error: Value too large for
defined data type ...
Best regards
Piotr
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20161023/7cb2577d/attachment.html>
More information about the Libav-user
mailing list