2003-06-13 17:28:45 +03:00
|
|
|
/*
|
2016-04-27 19:45:23 +02:00
|
|
|
* MPEG-2 transport stream (aka DVB) muxer
|
2009-01-19 17:46:40 +02:00
|
|
|
* Copyright (c) 2003 Fabrice Bellard
|
2003-06-13 17:28:45 +03:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2003-06-13 17:28:45 +03:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 18:30:46 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2003-06-13 17:28:45 +03:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2003-06-13 17:28:45 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2006-10-07 18:30:46 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-01-13 00:43:26 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2003-06-13 17:28:45 +03:00
|
|
|
*/
|
2008-05-09 14:56:36 +03:00
|
|
|
|
2014-08-07 22:16:06 +03:00
|
|
|
#include "libavutil/avassert.h"
|
2009-01-12 00:19:48 +02:00
|
|
|
#include "libavutil/bswap.h"
|
2008-05-09 14:56:36 +03:00
|
|
|
#include "libavutil/crc.h"
|
2011-05-22 13:46:29 +03:00
|
|
|
#include "libavutil/dict.h"
|
2013-03-25 15:11:41 +03:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2011-06-04 14:58:23 +03:00
|
|
|
#include "libavutil/mathematics.h"
|
2011-01-31 13:19:51 +02:00
|
|
|
#include "libavutil/opt.h"
|
2014-08-06 04:02:18 +03:00
|
|
|
|
2013-03-25 15:11:41 +03:00
|
|
|
#include "libavcodec/internal.h"
|
2014-08-06 04:02:18 +03:00
|
|
|
|
2003-06-13 17:28:45 +03:00
|
|
|
#include "avformat.h"
|
2015-02-24 14:07:54 +02:00
|
|
|
#include "avio_internal.h"
|
2010-04-12 00:44:23 +03:00
|
|
|
#include "internal.h"
|
2003-06-13 17:28:45 +03:00
|
|
|
#include "mpegts.h"
|
|
|
|
|
2010-12-03 15:26:42 +02:00
|
|
|
#define PCR_TIME_BASE 27000000
|
|
|
|
|
2003-06-13 17:28:45 +03:00
|
|
|
/* write DVB SI sections */
|
|
|
|
|
2016-05-17 09:54:30 +02:00
|
|
|
#define DVB_PRIVATE_NETWORK_START 0xff01
|
|
|
|
|
2003-06-13 17:28:45 +03:00
|
|
|
/*********************************************/
|
|
|
|
/* mpegts section writer */
|
|
|
|
|
|
|
|
typedef struct MpegTSSection {
|
|
|
|
int pid;
|
|
|
|
int cc;
|
|
|
|
void (*write_packet)(struct MpegTSSection *s, const uint8_t *packet);
|
|
|
|
void *opaque;
|
|
|
|
} MpegTSSection;
|
|
|
|
|
2009-01-14 04:20:26 +02:00
|
|
|
typedef struct MpegTSService {
|
2016-04-27 19:45:23 +02:00
|
|
|
MpegTSSection pmt; /* MPEG-2 PMT table context */
|
2009-01-14 04:20:26 +02:00
|
|
|
int sid; /* service ID */
|
|
|
|
char *name;
|
|
|
|
char *provider_name;
|
|
|
|
int pcr_pid;
|
|
|
|
int pcr_packet_count;
|
2009-07-28 20:51:12 +03:00
|
|
|
int pcr_packet_period;
|
2016-03-23 03:52:03 +02:00
|
|
|
AVProgram *program;
|
2009-01-14 04:20:26 +02:00
|
|
|
} MpegTSService;
|
|
|
|
|
2015-02-03 15:46:39 +02:00
|
|
|
// service_type values as defined in ETSI 300 468
|
|
|
|
enum {
|
|
|
|
MPEGTS_SERVICE_TYPE_DIGITAL_TV = 0x01,
|
|
|
|
MPEGTS_SERVICE_TYPE_DIGITAL_RADIO = 0x02,
|
|
|
|
MPEGTS_SERVICE_TYPE_TELETEXT = 0x03,
|
|
|
|
MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_RADIO = 0x0A,
|
|
|
|
MPEGTS_SERVICE_TYPE_MPEG2_DIGITAL_HDTV = 0x11,
|
|
|
|
MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_SDTV = 0x16,
|
2016-05-03 14:25:22 +02:00
|
|
|
MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_HDTV = 0x19,
|
|
|
|
MPEGTS_SERVICE_TYPE_HEVC_DIGITAL_HDTV = 0x1F,
|
2015-02-03 15:46:39 +02:00
|
|
|
};
|
2009-01-14 04:20:26 +02:00
|
|
|
typedef struct MpegTSWrite {
|
2011-05-20 11:33:57 +03:00
|
|
|
const AVClass *av_class;
|
2016-04-27 19:45:23 +02:00
|
|
|
MpegTSSection pat; /* MPEG-2 PAT table */
|
|
|
|
MpegTSSection sdt; /* MPEG-2 SDT table context */
|
2009-01-14 04:20:26 +02:00
|
|
|
MpegTSService **services;
|
|
|
|
int sdt_packet_count;
|
2009-07-28 20:51:12 +03:00
|
|
|
int sdt_packet_period;
|
2009-01-14 04:20:26 +02:00
|
|
|
int pat_packet_count;
|
2009-07-28 20:51:12 +03:00
|
|
|
int pat_packet_period;
|
2009-01-14 04:20:26 +02:00
|
|
|
int nb_services;
|
|
|
|
int onid;
|
|
|
|
int tsid;
|
2010-12-03 15:26:42 +02:00
|
|
|
int64_t first_pcr;
|
2010-03-09 01:46:19 +02:00
|
|
|
int mux_rate; ///< set to 1 when VBR
|
2011-12-19 22:15:21 +03:00
|
|
|
int pes_payload_size;
|
2011-01-31 13:19:51 +02:00
|
|
|
|
|
|
|
int transport_stream_id;
|
|
|
|
int original_network_id;
|
|
|
|
int service_id;
|
2015-02-02 23:30:56 +02:00
|
|
|
int service_type;
|
2011-01-31 13:19:51 +02:00
|
|
|
|
|
|
|
int pmt_start_pid;
|
|
|
|
int start_pid;
|
2011-08-13 16:51:36 +03:00
|
|
|
int m2ts_mode;
|
2012-01-29 03:12:06 +03:00
|
|
|
|
2012-06-01 11:42:48 +03:00
|
|
|
int reemit_pat_pmt; // backward compatibility
|
|
|
|
|
2014-06-06 16:59:25 +03:00
|
|
|
int pcr_period;
|
2012-06-01 11:42:48 +03:00
|
|
|
#define MPEGTS_FLAG_REEMIT_PAT_PMT 0x01
|
|
|
|
#define MPEGTS_FLAG_AAC_LATM 0x02
|
2015-07-16 01:54:56 +02:00
|
|
|
#define MPEGTS_FLAG_PAT_PMT_AT_FRAMES 0x04
|
2015-12-13 12:54:32 +02:00
|
|
|
#define MPEGTS_FLAG_SYSTEM_B 0x08
|
2012-06-01 11:42:48 +03:00
|
|
|
int flags;
|
2012-06-08 04:30:47 +03:00
|
|
|
int copyts;
|
2013-08-02 22:08:05 +03:00
|
|
|
int tables_version;
|
2015-10-16 21:09:19 +02:00
|
|
|
double pat_period;
|
2015-10-16 21:09:20 +02:00
|
|
|
double sdt_period;
|
2015-07-16 12:56:55 +02:00
|
|
|
int64_t last_pat_ts;
|
2015-07-18 23:25:07 +02:00
|
|
|
int64_t last_sdt_ts;
|
2014-04-22 17:09:46 +03:00
|
|
|
|
2014-04-22 21:40:31 +03:00
|
|
|
int omit_video_pes_length;
|
2009-01-14 04:20:26 +02:00
|
|
|
} MpegTSWrite;
|
|
|
|
|
2011-12-19 22:15:21 +03:00
|
|
|
/* a PES packet header is generated every DEFAULT_PES_HEADER_FREQ packets */
|
2014-08-06 04:02:18 +03:00
|
|
|
#define DEFAULT_PES_HEADER_FREQ 16
|
2011-12-19 22:15:21 +03:00
|
|
|
#define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
|
|
|
|
|
2014-08-03 20:27:07 +03:00
|
|
|
/* The section length is 12 bits. The first 2 are set to 0, the remaining
|
|
|
|
* 10 bits should not exceed 1021. */
|
|
|
|
#define SECTION_LENGTH 1020
|
|
|
|
|
2003-06-13 17:28:45 +03:00
|
|
|
/* NOTE: 4 bytes must be left at the end for the crc32 */
|
2006-06-18 14:33:14 +03:00
|
|
|
static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
|
2003-06-13 17:28:45 +03:00
|
|
|
{
|
|
|
|
unsigned int crc;
|
|
|
|
unsigned char packet[TS_PACKET_SIZE];
|
|
|
|
const unsigned char *buf_ptr;
|
|
|
|
unsigned char *q;
|
|
|
|
int first, b, len1, left;
|
|
|
|
|
2014-08-06 04:02:18 +03:00
|
|
|
crc = av_bswap32(av_crc(av_crc_get_table(AV_CRC_32_IEEE),
|
|
|
|
-1, buf, len - 4));
|
|
|
|
|
2003-06-13 17:28:45 +03:00
|
|
|
buf[len - 4] = (crc >> 24) & 0xff;
|
|
|
|
buf[len - 3] = (crc >> 16) & 0xff;
|
2014-08-06 04:02:18 +03:00
|
|
|
buf[len - 2] = (crc >> 8) & 0xff;
|
2014-08-06 14:39:24 +03:00
|
|
|
buf[len - 1] = crc & 0xff;
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2003-06-13 17:28:45 +03:00
|
|
|
/* send each packet */
|
|
|
|
buf_ptr = buf;
|
|
|
|
while (len > 0) {
|
2014-08-06 14:39:24 +03:00
|
|
|
first = buf == buf_ptr;
|
2014-08-06 04:02:18 +03:00
|
|
|
q = packet;
|
|
|
|
*q++ = 0x47;
|
2014-08-06 14:39:24 +03:00
|
|
|
b = s->pid >> 8;
|
2003-06-13 17:28:45 +03:00
|
|
|
if (first)
|
|
|
|
b |= 0x40;
|
2014-08-06 04:02:18 +03:00
|
|
|
*q++ = b;
|
|
|
|
*q++ = s->pid;
|
2014-08-06 14:39:24 +03:00
|
|
|
s->cc = s->cc + 1 & 0xf;
|
2014-08-06 04:02:18 +03:00
|
|
|
*q++ = 0x10 | s->cc;
|
2003-06-13 17:28:45 +03:00
|
|
|
if (first)
|
|
|
|
*q++ = 0; /* 0 offset */
|
|
|
|
len1 = TS_PACKET_SIZE - (q - packet);
|
2005-12-17 20:14:38 +02:00
|
|
|
if (len1 > len)
|
2003-06-13 17:28:45 +03:00
|
|
|
len1 = len;
|
|
|
|
memcpy(q, buf_ptr, len1);
|
|
|
|
q += len1;
|
|
|
|
/* add known padding data */
|
|
|
|
left = TS_PACKET_SIZE - (q - packet);
|
|
|
|
if (left > 0)
|
|
|
|
memset(q, 0xff, left);
|
|
|
|
|
|
|
|
s->write_packet(s, packet);
|
|
|
|
|
|
|
|
buf_ptr += len1;
|
2014-08-06 04:02:18 +03:00
|
|
|
len -= len1;
|
2003-06-13 17:28:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void put16(uint8_t **q_ptr, int val)
|
|
|
|
{
|
|
|
|
uint8_t *q;
|
2014-08-06 04:02:18 +03:00
|
|
|
q = *q_ptr;
|
|
|
|
*q++ = val >> 8;
|
|
|
|
*q++ = val;
|
2003-06-13 17:28:45 +03:00
|
|
|
*q_ptr = q;
|
|
|
|
}
|
|
|
|
|
2006-06-18 14:33:14 +03:00
|
|
|
static int mpegts_write_section1(MpegTSSection *s, int tid, int id,
|
2014-08-06 04:02:18 +03:00
|
|
|
int version, int sec_num, int last_sec_num,
|
|
|
|
uint8_t *buf, int len)
|
2003-06-13 17:28:45 +03:00
|
|
|
{
|
|
|
|
uint8_t section[1024], *q;
|
|
|
|
unsigned int tot_len;
|
2011-01-28 18:28:20 +02:00
|
|
|
/* reserved_future_use field must be set to 1 for SDT */
|
|
|
|
unsigned int flags = tid == SDT_TID ? 0xf000 : 0xb000;
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2003-06-13 17:28:45 +03:00
|
|
|
tot_len = 3 + 5 + len + 4;
|
|
|
|
/* check if not too big */
|
|
|
|
if (tot_len > 1024)
|
2012-01-12 21:45:10 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2003-06-13 17:28:45 +03:00
|
|
|
|
2014-08-06 04:02:18 +03:00
|
|
|
q = section;
|
2003-06-13 17:28:45 +03:00
|
|
|
*q++ = tid;
|
2011-01-28 18:28:20 +02:00
|
|
|
put16(&q, flags | (len + 5 + 4)); /* 5 byte header + 4 byte CRC */
|
2003-06-13 17:28:45 +03:00
|
|
|
put16(&q, id);
|
|
|
|
*q++ = 0xc1 | (version << 1); /* current_next_indicator = 1 */
|
|
|
|
*q++ = sec_num;
|
|
|
|
*q++ = last_sec_num;
|
|
|
|
memcpy(q, buf, len);
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2003-06-13 17:28:45 +03:00
|
|
|
mpegts_write_section(s, section, tot_len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************/
|
|
|
|
/* mpegts writer */
|
|
|
|
|
2011-04-18 00:57:50 +03:00
|
|
|
#define DEFAULT_PROVIDER_NAME "FFmpeg"
|
2003-06-13 17:28:45 +03:00
|
|
|
#define DEFAULT_SERVICE_NAME "Service01"
|
|
|
|
|
|
|
|
/* we retransmit the SI info at this rate */
|
|
|
|
#define SDT_RETRANS_TIME 500
|
|
|
|
#define PAT_RETRANS_TIME 100
|
2005-06-03 17:05:41 +03:00
|
|
|
#define PCR_RETRANS_TIME 20
|
2003-06-13 17:28:45 +03:00
|
|
|
|
|
|
|
typedef struct MpegTSWriteStream {
|
2005-06-03 17:05:41 +03:00
|
|
|
struct MpegTSService *service;
|
2003-06-13 17:28:45 +03:00
|
|
|
int pid; /* stream associated pid */
|
|
|
|
int cc;
|
2011-12-17 16:08:11 +03:00
|
|
|
int payload_size;
|
2009-05-10 04:57:21 +03:00
|
|
|
int first_pts_check; ///< first pts check needed
|
2012-02-11 04:19:08 +03:00
|
|
|
int prev_payload_key;
|
2003-09-16 15:56:42 +03:00
|
|
|
int64_t payload_pts;
|
2007-03-02 23:47:24 +02:00
|
|
|
int64_t payload_dts;
|
2011-06-29 16:01:39 +03:00
|
|
|
int payload_flags;
|
2011-12-19 22:15:21 +03:00
|
|
|
uint8_t *payload;
|
2012-06-01 13:42:20 +03:00
|
|
|
AVFormatContext *amux;
|
2014-05-18 13:12:59 +03:00
|
|
|
AVRational user_tb;
|
2015-11-06 00:35:42 +02:00
|
|
|
|
|
|
|
/* For Opus */
|
|
|
|
int opus_queued_samples;
|
2015-11-02 09:06:19 +02:00
|
|
|
int opus_pending_trim_start;
|
2003-06-13 17:28:45 +03:00
|
|
|
} MpegTSWriteStream;
|
|
|
|
|
|
|
|
static void mpegts_write_pat(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
MpegTSWrite *ts = s->priv_data;
|
|
|
|
MpegTSService *service;
|
2014-08-03 20:27:07 +03:00
|
|
|
uint8_t data[SECTION_LENGTH], *q;
|
2003-06-13 17:28:45 +03:00
|
|
|
int i;
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2003-06-13 17:28:45 +03:00
|
|
|
q = data;
|
2014-08-06 04:02:18 +03:00
|
|
|
for (i = 0; i < ts->nb_services; i++) {
|
2003-06-13 17:28:45 +03:00
|
|
|
service = ts->services[i];
|
|
|
|
put16(&q, service->sid);
|
|
|
|
put16(&q, 0xe000 | service->pmt.pid);
|
|
|
|
}
|
2013-08-02 22:08:05 +03:00
|
|
|
mpegts_write_section1(&ts->pat, PAT_TID, ts->tsid, ts->tables_version, 0, 0,
|
2003-06-13 17:28:45 +03:00
|
|
|
data, q - data);
|
|
|
|
}
|
|
|
|
|
2014-02-13 15:59:51 +03:00
|
|
|
static int mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
|
2003-06-13 17:28:45 +03:00
|
|
|
{
|
2012-06-01 11:42:48 +03:00
|
|
|
MpegTSWrite *ts = s->priv_data;
|
2014-08-03 20:27:07 +03:00
|
|
|
uint8_t data[SECTION_LENGTH], *q, *desc_length_ptr, *program_info_length_ptr;
|
2014-08-12 21:21:12 +03:00
|
|
|
int val, stream_type, i, err = 0;
|
2003-06-13 17:28:45 +03:00
|
|
|
|
|
|
|
q = data;
|
|
|
|
put16(&q, 0xe000 | service->pcr_pid);
|
|
|
|
|
|
|
|
program_info_length_ptr = q;
|
|
|
|
q += 2; /* patched after */
|
|
|
|
|
|
|
|
/* put program info here */
|
|
|
|
|
|
|
|
val = 0xf000 | (q - program_info_length_ptr - 2);
|
|
|
|
program_info_length_ptr[0] = val >> 8;
|
|
|
|
program_info_length_ptr[1] = val;
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2014-08-06 04:02:18 +03:00
|
|
|
for (i = 0; i < s->nb_streams; i++) {
|
2003-06-13 17:28:45 +03:00
|
|
|
AVStream *st = s->streams[i];
|
|
|
|
MpegTSWriteStream *ts_st = st->priv_data;
|
2014-08-06 04:02:18 +03:00
|
|
|
AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
|
2014-08-12 21:21:12 +03:00
|
|
|
|
2015-12-10 22:02:29 +02:00
|
|
|
if (s->nb_programs) {
|
2016-03-23 03:52:03 +02:00
|
|
|
int k, found = 0;
|
|
|
|
AVProgram *program = service->program;
|
|
|
|
|
|
|
|
for (k = 0; k < program->nb_stream_indexes; k++)
|
|
|
|
if (program->stream_index[k] == i) {
|
|
|
|
found = 1;
|
2016-01-24 07:06:36 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
2015-12-10 22:02:29 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-08-14 03:11:02 +03:00
|
|
|
if (q - data > SECTION_LENGTH - 32) {
|
2014-08-12 21:21:12 +03:00
|
|
|
err = 1;
|
|
|
|
break;
|
|
|
|
}
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
switch (st->codecpar->codec_id) {
|
2012-08-05 12:11:04 +03:00
|
|
|
case AV_CODEC_ID_MPEG1VIDEO:
|
|
|
|
case AV_CODEC_ID_MPEG2VIDEO:
|
2003-11-12 23:56:57 +02:00
|
|
|
stream_type = STREAM_TYPE_VIDEO_MPEG2;
|
2003-06-13 17:28:45 +03:00
|
|
|
break;
|
2012-08-05 12:11:04 +03:00
|
|
|
case AV_CODEC_ID_MPEG4:
|
2003-11-12 23:56:57 +02:00
|
|
|
stream_type = STREAM_TYPE_VIDEO_MPEG4;
|
|
|
|
break;
|
2012-08-05 12:11:04 +03:00
|
|
|
case AV_CODEC_ID_H264:
|
2003-11-12 23:56:57 +02:00
|
|
|
stream_type = STREAM_TYPE_VIDEO_H264;
|
|
|
|
break;
|
2014-03-03 22:32:41 +03:00
|
|
|
case AV_CODEC_ID_HEVC:
|
|
|
|
stream_type = STREAM_TYPE_VIDEO_HEVC;
|
|
|
|
break;
|
2013-02-26 05:01:21 +03:00
|
|
|
case AV_CODEC_ID_CAVS:
|
|
|
|
stream_type = STREAM_TYPE_VIDEO_CAVS;
|
|
|
|
break;
|
2012-08-05 12:11:04 +03:00
|
|
|
case AV_CODEC_ID_DIRAC:
|
2008-08-13 22:29:35 +03:00
|
|
|
stream_type = STREAM_TYPE_VIDEO_DIRAC;
|
|
|
|
break;
|
2015-11-12 23:54:18 +02:00
|
|
|
case AV_CODEC_ID_VC1:
|
|
|
|
stream_type = STREAM_TYPE_VIDEO_VC1;
|
|
|
|
break;
|
2012-08-05 12:11:04 +03:00
|
|
|
case AV_CODEC_ID_MP2:
|
|
|
|
case AV_CODEC_ID_MP3:
|
2016-05-04 23:39:27 +02:00
|
|
|
if ( st->codecpar->sample_rate > 0
|
|
|
|
&& st->codecpar->sample_rate < 32000) {
|
2016-05-03 14:15:14 +02:00
|
|
|
stream_type = STREAM_TYPE_AUDIO_MPEG2;
|
|
|
|
} else {
|
|
|
|
stream_type = STREAM_TYPE_AUDIO_MPEG1;
|
|
|
|
}
|
2003-06-13 17:28:45 +03:00
|
|
|
break;
|
2012-08-05 12:11:04 +03:00
|
|
|
case AV_CODEC_ID_AAC:
|
2014-08-06 04:02:18 +03:00
|
|
|
stream_type = (ts->flags & MPEGTS_FLAG_AAC_LATM)
|
|
|
|
? STREAM_TYPE_AUDIO_AAC_LATM
|
|
|
|
: STREAM_TYPE_AUDIO_AAC;
|
2003-11-12 23:56:57 +02:00
|
|
|
break;
|
2012-08-05 12:11:04 +03:00
|
|
|
case AV_CODEC_ID_AAC_LATM:
|
2011-01-29 00:28:03 +02:00
|
|
|
stream_type = STREAM_TYPE_AUDIO_AAC_LATM;
|
|
|
|
break;
|
2012-08-05 12:11:04 +03:00
|
|
|
case AV_CODEC_ID_AC3:
|
2015-12-13 12:54:32 +02:00
|
|
|
stream_type = (ts->flags & MPEGTS_FLAG_SYSTEM_B)
|
|
|
|
? STREAM_TYPE_PRIVATE_DATA
|
|
|
|
: STREAM_TYPE_AUDIO_AC3;
|
|
|
|
break;
|
|
|
|
case AV_CODEC_ID_EAC3:
|
|
|
|
stream_type = (ts->flags & MPEGTS_FLAG_SYSTEM_B)
|
|
|
|
? STREAM_TYPE_PRIVATE_DATA
|
|
|
|
: STREAM_TYPE_AUDIO_EAC3;
|
2003-11-12 23:56:57 +02:00
|
|
|
break;
|
2014-04-25 07:23:41 +03:00
|
|
|
case AV_CODEC_ID_DTS:
|
|
|
|
stream_type = STREAM_TYPE_AUDIO_DTS;
|
|
|
|
break;
|
|
|
|
case AV_CODEC_ID_TRUEHD:
|
|
|
|
stream_type = STREAM_TYPE_AUDIO_TRUEHD;
|
|
|
|
break;
|
2015-11-06 00:35:42 +02:00
|
|
|
case AV_CODEC_ID_OPUS:
|
|
|
|
stream_type = STREAM_TYPE_PRIVATE_DATA;
|
|
|
|
break;
|
2016-03-26 13:02:49 +02:00
|
|
|
case AV_CODEC_ID_TIMED_ID3:
|
|
|
|
stream_type = STREAM_TYPE_METADATA;
|
|
|
|
break;
|
2003-06-13 17:28:45 +03:00
|
|
|
default:
|
|
|
|
stream_type = STREAM_TYPE_PRIVATE_DATA;
|
|
|
|
break;
|
|
|
|
}
|
2014-02-13 15:59:51 +03:00
|
|
|
|
2003-06-13 17:28:45 +03:00
|
|
|
*q++ = stream_type;
|
|
|
|
put16(&q, 0xe000 | ts_st->pid);
|
|
|
|
desc_length_ptr = q;
|
|
|
|
q += 2; /* patched after */
|
|
|
|
|
|
|
|
/* write optional descriptors here */
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
switch (st->codecpar->codec_type) {
|
2010-03-31 02:30:55 +03:00
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
2016-04-10 21:58:15 +02:00
|
|
|
if (st->codecpar->codec_id==AV_CODEC_ID_AC3 && (ts->flags & MPEGTS_FLAG_SYSTEM_B)) {
|
2015-12-13 12:54:32 +02:00
|
|
|
*q++=0x6a; // AC3 descriptor see A038 DVB SI
|
|
|
|
*q++=1; // 1 byte, all flags sets to 0
|
|
|
|
*q++=0; // omit all fields...
|
|
|
|
}
|
2016-04-10 21:58:15 +02:00
|
|
|
if (st->codecpar->codec_id==AV_CODEC_ID_EAC3 && (ts->flags & MPEGTS_FLAG_SYSTEM_B)) {
|
2011-11-02 22:41:24 +03:00
|
|
|
*q++=0x7a; // EAC3 descriptor see A038 DVB SI
|
|
|
|
*q++=1; // 1 byte, all flags sets to 0
|
|
|
|
*q++=0; // omit all fields...
|
|
|
|
}
|
2016-04-10 21:58:15 +02:00
|
|
|
if (st->codecpar->codec_id==AV_CODEC_ID_S302M) {
|
2012-04-08 10:44:14 +03:00
|
|
|
*q++ = 0x05; /* MPEG-2 registration descriptor*/
|
|
|
|
*q++ = 4;
|
|
|
|
*q++ = 'B';
|
|
|
|
*q++ = 'S';
|
|
|
|
*q++ = 'S';
|
|
|
|
*q++ = 'D';
|
|
|
|
}
|
2016-04-10 21:58:15 +02:00
|
|
|
if (st->codecpar->codec_id==AV_CODEC_ID_OPUS) {
|
2015-11-06 00:35:42 +02:00
|
|
|
/* 6 bytes registration descriptor, 4 bytes Opus audio descriptor */
|
|
|
|
if (q - data > SECTION_LENGTH - 6 - 4) {
|
|
|
|
err = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
*q++ = 0x05; /* MPEG-2 registration descriptor*/
|
|
|
|
*q++ = 4;
|
|
|
|
*q++ = 'O';
|
|
|
|
*q++ = 'p';
|
|
|
|
*q++ = 'u';
|
|
|
|
*q++ = 's';
|
|
|
|
|
|
|
|
*q++ = 0x7f; /* DVB extension descriptor */
|
|
|
|
*q++ = 2;
|
|
|
|
*q++ = 0x80;
|
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
if (st->codecpar->extradata && st->codecpar->extradata_size >= 19) {
|
|
|
|
if (st->codecpar->extradata[18] == 0 && st->codecpar->channels <= 2) {
|
2015-11-06 00:35:42 +02:00
|
|
|
/* RTP mapping family */
|
2016-04-10 21:58:15 +02:00
|
|
|
*q++ = st->codecpar->channels;
|
|
|
|
} else if (st->codecpar->extradata[18] == 1 && st->codecpar->channels <= 8 &&
|
|
|
|
st->codecpar->extradata_size >= 21 + st->codecpar->channels) {
|
2015-11-06 00:35:42 +02:00
|
|
|
static const uint8_t coupled_stream_counts[9] = {
|
|
|
|
1, 0, 1, 1, 2, 2, 2, 3, 3
|
|
|
|
};
|
|
|
|
static const uint8_t channel_map_a[8][8] = {
|
|
|
|
{0},
|
|
|
|
{0, 1},
|
|
|
|
{0, 2, 1},
|
|
|
|
{0, 1, 2, 3},
|
|
|
|
{0, 4, 1, 2, 3},
|
|
|
|
{0, 4, 1, 2, 3, 5},
|
|
|
|
{0, 4, 1, 2, 3, 5, 6},
|
|
|
|
{0, 6, 1, 2, 3, 4, 5, 7},
|
|
|
|
};
|
|
|
|
static const uint8_t channel_map_b[8][8] = {
|
|
|
|
{0},
|
|
|
|
{0, 1},
|
|
|
|
{0, 1, 2},
|
|
|
|
{0, 1, 2, 3},
|
|
|
|
{0, 1, 2, 3, 4},
|
|
|
|
{0, 1, 2, 3, 4, 5},
|
|
|
|
{0, 1, 2, 3, 4, 5, 6},
|
|
|
|
{0, 1, 2, 3, 4, 5, 6, 7},
|
|
|
|
};
|
|
|
|
/* Vorbis mapping family */
|
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
if (st->codecpar->extradata[19] == st->codecpar->channels - coupled_stream_counts[st->codecpar->channels] &&
|
|
|
|
st->codecpar->extradata[20] == coupled_stream_counts[st->codecpar->channels] &&
|
|
|
|
memcmp(&st->codecpar->extradata[21], channel_map_a[st->codecpar->channels-1], st->codecpar->channels) == 0) {
|
|
|
|
*q++ = st->codecpar->channels;
|
|
|
|
} else if (st->codecpar->channels >= 2 && st->codecpar->extradata[19] == st->codecpar->channels &&
|
|
|
|
st->codecpar->extradata[20] == 0 &&
|
|
|
|
memcmp(&st->codecpar->extradata[21], channel_map_b[st->codecpar->channels-1], st->codecpar->channels) == 0) {
|
|
|
|
*q++ = st->codecpar->channels | 0x80;
|
2015-11-06 00:35:42 +02:00
|
|
|
} else {
|
|
|
|
/* Unsupported, could write an extended descriptor here */
|
|
|
|
av_log(s, AV_LOG_ERROR, "Unsupported Opus Vorbis-style channel mapping");
|
|
|
|
*q++ = 0xff;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Unsupported */
|
2016-04-10 21:58:15 +02:00
|
|
|
av_log(s, AV_LOG_ERROR, "Unsupported Opus channel mapping for family %d", st->codecpar->extradata[18]);
|
2015-11-06 00:35:42 +02:00
|
|
|
*q++ = 0xff;
|
|
|
|
}
|
2016-04-10 21:58:15 +02:00
|
|
|
} else if (st->codecpar->channels <= 2) {
|
2015-11-06 00:35:42 +02:00
|
|
|
/* Assume RTP mapping family */
|
2016-04-10 21:58:15 +02:00
|
|
|
*q++ = st->codecpar->channels;
|
2015-11-06 00:35:42 +02:00
|
|
|
} else {
|
|
|
|
/* Unsupported */
|
|
|
|
av_log(s, AV_LOG_ERROR, "Unsupported Opus channel mapping");
|
|
|
|
*q++ = 0xff;
|
|
|
|
}
|
|
|
|
}
|
2011-11-02 22:41:24 +03:00
|
|
|
|
2011-02-17 17:26:35 +02:00
|
|
|
if (lang) {
|
|
|
|
char *p;
|
|
|
|
char *next = lang->value;
|
|
|
|
uint8_t *len_ptr;
|
|
|
|
|
2014-08-06 04:02:18 +03:00
|
|
|
*q++ = 0x0a; /* ISO 639 language descriptor */
|
|
|
|
len_ptr = q++;
|
2011-02-17 17:26:35 +02:00
|
|
|
*len_ptr = 0;
|
|
|
|
|
|
|
|
for (p = lang->value; next && *len_ptr < 255 / 4 * 4; p = next + 1) {
|
2014-08-12 21:21:12 +03:00
|
|
|
if (q - data > SECTION_LENGTH - 4) {
|
|
|
|
err = 1;
|
|
|
|
break;
|
|
|
|
}
|
2011-02-17 17:26:35 +02:00
|
|
|
next = strchr(p, ',');
|
|
|
|
if (strlen(p) != 3 && (!next || next != p + 3))
|
|
|
|
continue; /* not a 3-letter code */
|
|
|
|
|
|
|
|
*q++ = *p++;
|
|
|
|
*q++ = *p++;
|
|
|
|
*q++ = *p++;
|
|
|
|
|
2014-08-06 04:02:18 +03:00
|
|
|
if (st->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
|
|
|
|
*q++ = 0x01;
|
|
|
|
else if (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
|
|
|
|
*q++ = 0x02;
|
|
|
|
else if (st->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
|
|
|
|
*q++ = 0x03;
|
|
|
|
else
|
|
|
|
*q++ = 0; /* undefined type */
|
2011-02-17 17:26:35 +02:00
|
|
|
|
|
|
|
*len_ptr += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*len_ptr == 0)
|
|
|
|
q -= 2; /* no language codes were written */
|
2005-06-03 17:05:41 +03:00
|
|
|
}
|
|
|
|
break;
|
2010-03-31 02:30:55 +03:00
|
|
|
case AVMEDIA_TYPE_SUBTITLE:
|
2014-08-06 04:02:18 +03:00
|
|
|
{
|
2014-08-07 22:16:06 +03:00
|
|
|
const char default_language[] = "und";
|
|
|
|
const char *language = lang && strlen(lang->value) >= 3 ? lang->value : default_language;
|
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
if (st->codecpar->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
|
2014-08-07 22:16:06 +03:00
|
|
|
uint8_t *len_ptr;
|
|
|
|
int extradata_copied = 0;
|
|
|
|
|
|
|
|
*q++ = 0x59; /* subtitling_descriptor */
|
|
|
|
len_ptr = q++;
|
|
|
|
|
2014-08-14 03:11:02 +03:00
|
|
|
while (strlen(language) >= 3) {
|
|
|
|
if (sizeof(data) - (q - data) < 8) { /* 8 bytes per DVB subtitle substream data */
|
|
|
|
err = 1;
|
|
|
|
break;
|
|
|
|
}
|
2014-08-07 22:16:06 +03:00
|
|
|
*q++ = *language++;
|
|
|
|
*q++ = *language++;
|
|
|
|
*q++ = *language++;
|
|
|
|
/* Skip comma */
|
|
|
|
if (*language != '\0')
|
|
|
|
language++;
|
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
if (st->codecpar->extradata_size - extradata_copied >= 5) {
|
|
|
|
*q++ = st->codecpar->extradata[extradata_copied + 4]; /* subtitling_type */
|
|
|
|
memcpy(q, st->codecpar->extradata + extradata_copied, 4); /* composition_page_id and ancillary_page_id */
|
2014-08-07 22:16:06 +03:00
|
|
|
extradata_copied += 5;
|
|
|
|
q += 4;
|
|
|
|
} else {
|
|
|
|
/* subtitling_type:
|
|
|
|
* 0x10 - normal with no monitor aspect ratio criticality
|
|
|
|
* 0x20 - for the hard of hearing with no monitor aspect ratio criticality */
|
|
|
|
*q++ = (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED) ? 0x20 : 0x10;
|
2016-04-10 21:58:15 +02:00
|
|
|
if ((st->codecpar->extradata_size == 4) && (extradata_copied == 0)) {
|
2014-08-07 22:16:06 +03:00
|
|
|
/* support of old 4-byte extradata format */
|
2016-04-10 21:58:15 +02:00
|
|
|
memcpy(q, st->codecpar->extradata, 4); /* composition_page_id and ancillary_page_id */
|
2014-08-07 22:16:06 +03:00
|
|
|
extradata_copied += 4;
|
|
|
|
q += 4;
|
|
|
|
} else {
|
|
|
|
put16(&q, 1); /* composition_page_id */
|
|
|
|
put16(&q, 1); /* ancillary_page_id */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*len_ptr = q - len_ptr - 1;
|
2016-04-10 21:58:15 +02:00
|
|
|
} else if (st->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT) {
|
2014-08-07 22:16:06 +03:00
|
|
|
uint8_t *len_ptr = NULL;
|
|
|
|
int extradata_copied = 0;
|
|
|
|
|
|
|
|
/* The descriptor tag. teletext_descriptor */
|
|
|
|
*q++ = 0x56;
|
|
|
|
len_ptr = q++;
|
|
|
|
|
|
|
|
while (strlen(language) >= 3 && q - data < sizeof(data) - 6) {
|
|
|
|
*q++ = *language++;
|
|
|
|
*q++ = *language++;
|
|
|
|
*q++ = *language++;
|
|
|
|
/* Skip comma */
|
|
|
|
if (*language != '\0')
|
|
|
|
language++;
|
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
if (st->codecpar->extradata_size - 1 > extradata_copied) {
|
|
|
|
memcpy(q, st->codecpar->extradata + extradata_copied, 2);
|
2014-08-07 22:16:06 +03:00
|
|
|
extradata_copied += 2;
|
|
|
|
q += 2;
|
|
|
|
} else {
|
|
|
|
/* The Teletext descriptor:
|
|
|
|
* teletext_type: This 5-bit field indicates the type of Teletext page indicated. (0x01 Initial Teletext page)
|
|
|
|
* teletext_magazine_number: This is a 3-bit field which identifies the magazine number.
|
|
|
|
* teletext_page_number: This is an 8-bit field giving two 4-bit hex digits identifying the page number. */
|
|
|
|
*q++ = 0x08;
|
|
|
|
*q++ = 0x00;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*len_ptr = q - len_ptr - 1;
|
2005-06-03 17:05:41 +03:00
|
|
|
}
|
2014-08-06 04:02:18 +03:00
|
|
|
}
|
|
|
|
break;
|
2010-03-31 02:30:55 +03:00
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
2008-08-13 22:29:35 +03:00
|
|
|
if (stream_type == STREAM_TYPE_VIDEO_DIRAC) {
|
|
|
|
*q++ = 0x05; /*MPEG-2 registration descriptor*/
|
|
|
|
*q++ = 4;
|
|
|
|
*q++ = 'd';
|
|
|
|
*q++ = 'r';
|
|
|
|
*q++ = 'a';
|
|
|
|
*q++ = 'c';
|
2015-11-12 23:54:18 +02:00
|
|
|
} else if (stream_type == STREAM_TYPE_VIDEO_VC1) {
|
|
|
|
*q++ = 0x05; /*MPEG-2 registration descriptor*/
|
|
|
|
*q++ = 4;
|
|
|
|
*q++ = 'V';
|
|
|
|
*q++ = 'C';
|
|
|
|
*q++ = '-';
|
|
|
|
*q++ = '1';
|
2008-08-13 22:29:35 +03:00
|
|
|
}
|
|
|
|
break;
|
2013-04-11 19:57:15 +03:00
|
|
|
case AVMEDIA_TYPE_DATA:
|
2016-04-10 21:58:15 +02:00
|
|
|
if (st->codecpar->codec_id == AV_CODEC_ID_SMPTE_KLV) {
|
2013-04-11 19:57:15 +03:00
|
|
|
*q++ = 0x05; /* MPEG-2 registration descriptor */
|
|
|
|
*q++ = 4;
|
|
|
|
*q++ = 'K';
|
|
|
|
*q++ = 'L';
|
|
|
|
*q++ = 'V';
|
|
|
|
*q++ = 'A';
|
2016-03-26 13:02:49 +02:00
|
|
|
} else if (st->codecpar->codec_id == AV_CODEC_ID_TIMED_ID3) {
|
|
|
|
*q++ = 0x5; /* MPEG-2 registration descriptor */
|
|
|
|
*q++ = 4;
|
|
|
|
*q++ = 'I';
|
|
|
|
*q++ = 'D';
|
|
|
|
*q++ = '3';
|
|
|
|
*q++ = ' ';
|
2013-04-11 19:57:15 +03:00
|
|
|
}
|
|
|
|
break;
|
2005-06-03 17:05:41 +03:00
|
|
|
}
|
2003-06-13 17:28:45 +03:00
|
|
|
|
|
|
|
val = 0xf000 | (q - desc_length_ptr - 2);
|
|
|
|
desc_length_ptr[0] = val >> 8;
|
|
|
|
desc_length_ptr[1] = val;
|
|
|
|
}
|
2014-08-12 21:21:12 +03:00
|
|
|
|
|
|
|
if (err)
|
|
|
|
av_log(s, AV_LOG_ERROR,
|
|
|
|
"The PMT section cannot fit stream %d and all following streams.\n"
|
|
|
|
"Try reducing the number of languages in the audio streams "
|
|
|
|
"or the total number of streams.\n", i);
|
|
|
|
|
2013-08-02 22:08:05 +03:00
|
|
|
mpegts_write_section1(&service->pmt, PMT_TID, service->sid, ts->tables_version, 0, 0,
|
2003-06-13 17:28:45 +03:00
|
|
|
data, q - data);
|
2014-02-13 15:59:51 +03:00
|
|
|
return 0;
|
2005-12-17 20:14:38 +02:00
|
|
|
}
|
2003-06-13 17:28:45 +03:00
|
|
|
|
2014-08-15 21:33:21 +03:00
|
|
|
/* NOTE: !str is accepted for an empty string */
|
2003-06-13 17:28:45 +03:00
|
|
|
static void putstr8(uint8_t **q_ptr, const char *str)
|
|
|
|
{
|
|
|
|
uint8_t *q;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
q = *q_ptr;
|
|
|
|
if (!str)
|
|
|
|
len = 0;
|
|
|
|
else
|
|
|
|
len = strlen(str);
|
|
|
|
*q++ = len;
|
|
|
|
memcpy(q, str, len);
|
2014-08-06 04:02:18 +03:00
|
|
|
q += len;
|
2003-06-13 17:28:45 +03:00
|
|
|
*q_ptr = q;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mpegts_write_sdt(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
MpegTSWrite *ts = s->priv_data;
|
|
|
|
MpegTSService *service;
|
2014-08-03 20:27:07 +03:00
|
|
|
uint8_t data[SECTION_LENGTH], *q, *desc_list_len_ptr, *desc_len_ptr;
|
2003-06-13 17:28:45 +03:00
|
|
|
int i, running_status, free_ca_mode, val;
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2003-06-13 17:28:45 +03:00
|
|
|
q = data;
|
|
|
|
put16(&q, ts->onid);
|
|
|
|
*q++ = 0xff;
|
2014-08-06 04:02:18 +03:00
|
|
|
for (i = 0; i < ts->nb_services; i++) {
|
2003-06-13 17:28:45 +03:00
|
|
|
service = ts->services[i];
|
|
|
|
put16(&q, service->sid);
|
2014-08-06 04:02:18 +03:00
|
|
|
*q++ = 0xfc | 0x00; /* currently no EIT info */
|
2003-06-13 17:28:45 +03:00
|
|
|
desc_list_len_ptr = q;
|
2014-08-06 04:02:18 +03:00
|
|
|
q += 2;
|
|
|
|
running_status = 4; /* running */
|
|
|
|
free_ca_mode = 0;
|
2003-06-13 17:28:45 +03:00
|
|
|
|
|
|
|
/* write only one descriptor for the service name and provider */
|
2014-08-06 04:02:18 +03:00
|
|
|
*q++ = 0x48;
|
2003-06-13 17:28:45 +03:00
|
|
|
desc_len_ptr = q;
|
|
|
|
q++;
|
2015-02-02 11:56:52 +02:00
|
|
|
*q++ = ts->service_type;
|
2003-06-13 17:28:45 +03:00
|
|
|
putstr8(&q, service->provider_name);
|
|
|
|
putstr8(&q, service->name);
|
|
|
|
desc_len_ptr[0] = q - desc_len_ptr - 1;
|
|
|
|
|
|
|
|
/* fill descriptor length */
|
2005-12-17 20:14:38 +02:00
|
|
|
val = (running_status << 13) | (free_ca_mode << 12) |
|
2014-08-06 04:02:18 +03:00
|
|
|
(q - desc_list_len_ptr - 2);
|
2003-06-13 17:28:45 +03:00
|
|
|
desc_list_len_ptr[0] = val >> 8;
|
|
|
|
desc_list_len_ptr[1] = val;
|
|
|
|
}
|
2013-08-02 22:08:05 +03:00
|
|
|
mpegts_write_section1(&ts->sdt, SDT_TID, ts->tsid, ts->tables_version, 0, 0,
|
2003-06-13 17:28:45 +03:00
|
|
|
data, q - data);
|
|
|
|
}
|
|
|
|
|
2014-08-06 04:02:18 +03:00
|
|
|
static MpegTSService *mpegts_add_service(MpegTSWrite *ts, int sid,
|
2005-12-17 20:14:38 +02:00
|
|
|
const char *provider_name,
|
2003-06-13 17:28:45 +03:00
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
MpegTSService *service;
|
|
|
|
|
|
|
|
service = av_mallocz(sizeof(MpegTSService));
|
|
|
|
if (!service)
|
|
|
|
return NULL;
|
2014-08-06 04:02:18 +03:00
|
|
|
service->pmt.pid = ts->pmt_start_pid + ts->nb_services;
|
|
|
|
service->sid = sid;
|
2014-08-06 14:08:50 +03:00
|
|
|
service->pcr_pid = 0x1fff;
|
2003-06-13 17:28:45 +03:00
|
|
|
service->provider_name = av_strdup(provider_name);
|
2014-08-06 04:02:18 +03:00
|
|
|
service->name = av_strdup(name);
|
2014-08-07 23:28:59 +03:00
|
|
|
if (!service->provider_name || !service->name)
|
|
|
|
goto fail;
|
|
|
|
if (av_dynarray_add_nofree(&ts->services, &ts->nb_services, service) < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2003-06-13 17:28:45 +03:00
|
|
|
return service;
|
2014-08-07 23:28:59 +03:00
|
|
|
fail:
|
|
|
|
av_freep(&service->provider_name);
|
|
|
|
av_freep(&service->name);
|
|
|
|
av_free(service);
|
|
|
|
return NULL;
|
2003-06-13 17:28:45 +03:00
|
|
|
}
|
|
|
|
|
2011-08-13 16:51:36 +03:00
|
|
|
static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb)
|
|
|
|
{
|
|
|
|
return av_rescale(avio_tell(pb) + 11, 8 * PCR_TIME_BASE, ts->mux_rate) +
|
|
|
|
ts->first_pcr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mpegts_prefix_m2ts_header(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
MpegTSWrite *ts = s->priv_data;
|
|
|
|
if (ts->m2ts_mode) {
|
|
|
|
int64_t pcr = get_pcr(s->priv_data, s->pb);
|
|
|
|
uint32_t tp_extra_header = pcr % 0x3fffffff;
|
|
|
|
tp_extra_header = AV_RB32(&tp_extra_header);
|
|
|
|
avio_write(s->pb, (unsigned char *) &tp_extra_header,
|
2014-11-29 18:53:45 +02:00
|
|
|
sizeof(tp_extra_header));
|
2011-08-13 16:51:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-13 17:28:45 +03:00
|
|
|
static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
|
|
|
|
{
|
|
|
|
AVFormatContext *ctx = s->opaque;
|
2011-08-13 16:51:36 +03:00
|
|
|
mpegts_prefix_m2ts_header(ctx);
|
2011-02-21 20:28:17 +02:00
|
|
|
avio_write(ctx->pb, packet, TS_PACKET_SIZE);
|
2003-06-13 17:28:45 +03:00
|
|
|
}
|
|
|
|
|
2015-11-30 11:00:41 +02:00
|
|
|
static int mpegts_init(AVFormatContext *s)
|
2003-06-13 17:28:45 +03:00
|
|
|
{
|
|
|
|
MpegTSWrite *ts = s->priv_data;
|
|
|
|
MpegTSWriteStream *ts_st;
|
|
|
|
MpegTSService *service;
|
2010-03-09 01:46:19 +02:00
|
|
|
AVStream *st, *pcr_st = NULL;
|
2011-05-22 13:46:29 +03:00
|
|
|
AVDictionaryEntry *title, *provider;
|
2010-07-01 01:39:13 +03:00
|
|
|
int i, j;
|
2005-06-03 17:05:41 +03:00
|
|
|
const char *service_name;
|
2011-01-20 09:26:40 +02:00
|
|
|
const char *provider_name;
|
2010-07-01 01:39:13 +03:00
|
|
|
int *pids;
|
2012-06-01 12:44:56 +03:00
|
|
|
int ret;
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2012-03-19 21:37:10 +03:00
|
|
|
if (s->max_delay < 0) /* Not set by the caller */
|
|
|
|
s->max_delay = 0;
|
|
|
|
|
2011-12-19 22:15:21 +03:00
|
|
|
// round up to a whole number of TS packets
|
|
|
|
ts->pes_payload_size = (ts->pes_payload_size + 14 + 183) / 184 * 184 - 14;
|
|
|
|
|
2011-01-31 13:19:51 +02:00
|
|
|
ts->tsid = ts->transport_stream_id;
|
|
|
|
ts->onid = ts->original_network_id;
|
2015-12-10 22:02:29 +02:00
|
|
|
if (!s->nb_programs) {
|
|
|
|
/* allocate a single DVB service */
|
|
|
|
title = av_dict_get(s->metadata, "service_name", NULL, 0);
|
|
|
|
if (!title)
|
|
|
|
title = av_dict_get(s->metadata, "title", NULL, 0);
|
|
|
|
service_name = title ? title->value : DEFAULT_SERVICE_NAME;
|
|
|
|
provider = av_dict_get(s->metadata, "service_provider", NULL, 0);
|
|
|
|
provider_name = provider ? provider->value : DEFAULT_PROVIDER_NAME;
|
|
|
|
service = mpegts_add_service(ts, ts->service_id,
|
|
|
|
provider_name, service_name);
|
|
|
|
|
|
|
|
if (!service)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
service->pmt.write_packet = section_write_packet;
|
|
|
|
service->pmt.opaque = s;
|
|
|
|
service->pmt.cc = 15;
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < s->nb_programs; i++) {
|
|
|
|
AVProgram *program = s->programs[i];
|
|
|
|
title = av_dict_get(program->metadata, "service_name", NULL, 0);
|
|
|
|
if (!title)
|
|
|
|
title = av_dict_get(program->metadata, "title", NULL, 0);
|
|
|
|
service_name = title ? title->value : DEFAULT_SERVICE_NAME;
|
|
|
|
provider = av_dict_get(program->metadata, "service_provider", NULL, 0);
|
|
|
|
provider_name = provider ? provider->value : DEFAULT_PROVIDER_NAME;
|
|
|
|
service = mpegts_add_service(ts, program->id,
|
|
|
|
provider_name, service_name);
|
|
|
|
|
|
|
|
if (!service)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
service->pmt.write_packet = section_write_packet;
|
|
|
|
service->pmt.opaque = s;
|
|
|
|
service->pmt.cc = 15;
|
2016-03-23 03:52:03 +02:00
|
|
|
service->program = program;
|
2015-12-10 22:02:29 +02:00
|
|
|
}
|
|
|
|
}
|
2003-06-13 17:28:45 +03:00
|
|
|
|
2014-08-06 04:02:18 +03:00
|
|
|
ts->pat.pid = PAT_PID;
|
|
|
|
/* Initialize at 15 so that it wraps and is equal to 0 for the
|
|
|
|
* first packet we write. */
|
|
|
|
ts->pat.cc = 15;
|
2003-06-13 17:28:45 +03:00
|
|
|
ts->pat.write_packet = section_write_packet;
|
2014-08-06 04:02:18 +03:00
|
|
|
ts->pat.opaque = s;
|
2003-06-13 17:28:45 +03:00
|
|
|
|
2014-08-06 04:02:18 +03:00
|
|
|
ts->sdt.pid = SDT_PID;
|
|
|
|
ts->sdt.cc = 15;
|
2003-06-13 17:28:45 +03:00
|
|
|
ts->sdt.write_packet = section_write_packet;
|
2014-08-06 04:02:18 +03:00
|
|
|
ts->sdt.opaque = s;
|
2003-06-13 17:28:45 +03:00
|
|
|
|
2014-04-15 14:42:46 +03:00
|
|
|
pids = av_malloc_array(s->nb_streams, sizeof(*pids));
|
2014-08-06 14:08:50 +03:00
|
|
|
if (!pids) {
|
2014-08-07 23:40:24 +03:00
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
2014-08-06 14:08:50 +03:00
|
|
|
}
|
2010-07-01 01:39:13 +03:00
|
|
|
|
2003-06-13 17:28:45 +03:00
|
|
|
/* assign pids to each stream */
|
2014-08-06 04:02:18 +03:00
|
|
|
for (i = 0; i < s->nb_streams; i++) {
|
2016-03-23 03:58:52 +02:00
|
|
|
AVProgram *program;
|
2003-06-13 17:28:45 +03:00
|
|
|
st = s->streams[i];
|
2014-05-18 13:12:59 +03:00
|
|
|
|
2003-06-13 17:28:45 +03:00
|
|
|
ts_st = av_mallocz(sizeof(MpegTSWriteStream));
|
2012-06-01 12:44:56 +03:00
|
|
|
if (!ts_st) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
2003-06-13 17:28:45 +03:00
|
|
|
goto fail;
|
2012-06-01 12:44:56 +03:00
|
|
|
}
|
2003-06-13 17:28:45 +03:00
|
|
|
st->priv_data = ts_st;
|
2014-05-18 13:12:59 +03:00
|
|
|
|
|
|
|
ts_st->user_tb = st->time_base;
|
|
|
|
avpriv_set_pts_info(st, 33, 1, 90000);
|
|
|
|
|
2011-12-19 22:15:21 +03:00
|
|
|
ts_st->payload = av_mallocz(ts->pes_payload_size);
|
2012-06-01 12:44:56 +03:00
|
|
|
if (!ts_st->payload) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
2011-12-19 22:15:21 +03:00
|
|
|
goto fail;
|
2012-06-01 12:44:56 +03:00
|
|
|
}
|
2016-03-23 03:58:52 +02:00
|
|
|
|
|
|
|
program = av_find_program_from_stream(s, NULL, i);
|
|
|
|
if (program) {
|
|
|
|
for (j = 0; j < ts->nb_services; j++) {
|
|
|
|
if (ts->services[j]->program == program) {
|
|
|
|
service = ts->services[j];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-03 17:05:41 +03:00
|
|
|
ts_st->service = service;
|
2010-07-01 01:39:13 +03:00
|
|
|
/* MPEG pid values < 16 are reserved. Applications which set st->id in
|
|
|
|
* this range are assigned a calculated pid. */
|
|
|
|
if (st->id < 16) {
|
2011-01-31 13:19:51 +02:00
|
|
|
ts_st->pid = ts->start_pid + i;
|
2010-07-01 01:39:13 +03:00
|
|
|
} else if (st->id < 0x1FFF) {
|
|
|
|
ts_st->pid = st->id;
|
|
|
|
} else {
|
2014-08-06 04:02:18 +03:00
|
|
|
av_log(s, AV_LOG_ERROR,
|
|
|
|
"Invalid stream id %d, must be less than 8191\n", st->id);
|
2012-06-01 12:44:56 +03:00
|
|
|
ret = AVERROR(EINVAL);
|
2010-07-01 01:39:13 +03:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (ts_st->pid == service->pmt.pid) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid);
|
2012-06-01 12:44:56 +03:00
|
|
|
ret = AVERROR(EINVAL);
|
2010-07-01 01:39:13 +03:00
|
|
|
goto fail;
|
|
|
|
}
|
2014-08-06 04:02:18 +03:00
|
|
|
for (j = 0; j < i; j++) {
|
2010-07-01 01:39:13 +03:00
|
|
|
if (pids[j] == ts_st->pid) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid);
|
2012-06-01 12:44:56 +03:00
|
|
|
ret = AVERROR(EINVAL);
|
2010-07-01 01:39:13 +03:00
|
|
|
goto fail;
|
|
|
|
}
|
2014-08-06 04:02:18 +03:00
|
|
|
}
|
|
|
|
pids[i] = ts_st->pid;
|
|
|
|
ts_st->payload_pts = AV_NOPTS_VALUE;
|
|
|
|
ts_st->payload_dts = AV_NOPTS_VALUE;
|
2009-05-10 04:57:21 +03:00
|
|
|
ts_st->first_pts_check = 1;
|
2014-08-06 04:02:18 +03:00
|
|
|
ts_st->cc = 15;
|
2005-06-03 17:05:41 +03:00
|
|
|
/* update PCR pid by using the first video stream */
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
|
2010-03-09 01:46:19 +02:00
|
|
|
service->pcr_pid == 0x1fff) {
|
2003-06-13 17:28:45 +03:00
|
|
|
service->pcr_pid = ts_st->pid;
|
2014-08-06 04:02:18 +03:00
|
|
|
pcr_st = st;
|
2009-09-17 22:07:09 +03:00
|
|
|
}
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
|
|
|
|
st->codecpar->extradata_size > 0) {
|
2012-06-01 13:42:20 +03:00
|
|
|
AVStream *ast;
|
|
|
|
ts_st->amux = avformat_alloc_context();
|
|
|
|
if (!ts_st->amux) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
2011-12-19 22:15:21 +03:00
|
|
|
goto fail;
|
2012-06-01 13:42:20 +03:00
|
|
|
}
|
2014-08-06 04:02:18 +03:00
|
|
|
ts_st->amux->oformat =
|
|
|
|
av_guess_format((ts->flags & MPEGTS_FLAG_AAC_LATM) ? "latm" : "adts",
|
|
|
|
NULL, NULL);
|
2012-06-01 11:42:48 +03:00
|
|
|
if (!ts_st->amux->oformat) {
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
2014-08-06 14:08:50 +03:00
|
|
|
if (!(ast = avformat_new_stream(ts_st->amux, NULL))) {
|
2014-04-26 23:08:10 +03:00
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
ret = avcodec_parameters_copy(ast->codecpar, st->codecpar);
|
2012-06-01 13:42:20 +03:00
|
|
|
if (ret != 0)
|
|
|
|
goto fail;
|
2014-10-06 11:41:33 +03:00
|
|
|
ast->time_base = st->time_base;
|
2012-06-01 13:42:20 +03:00
|
|
|
ret = avformat_write_header(ts_st->amux, NULL);
|
|
|
|
if (ret < 0)
|
2011-12-19 22:15:21 +03:00
|
|
|
goto fail;
|
2010-03-09 02:18:49 +02:00
|
|
|
}
|
2016-04-10 21:58:15 +02:00
|
|
|
if (st->codecpar->codec_id == AV_CODEC_ID_OPUS) {
|
|
|
|
ts_st->opus_pending_trim_start = st->codecpar->initial_padding * 48000 / st->codecpar->sample_rate;
|
2015-11-02 09:06:19 +02:00
|
|
|
}
|
2003-06-13 17:28:45 +03:00
|
|
|
}
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2014-08-08 00:04:32 +03:00
|
|
|
av_freep(&pids);
|
2010-07-01 01:39:13 +03:00
|
|
|
|
2005-06-03 17:05:41 +03:00
|
|
|
/* if no video stream, use the first stream as PCR */
|
|
|
|
if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
|
2014-08-06 04:02:18 +03:00
|
|
|
pcr_st = s->streams[0];
|
|
|
|
ts_st = pcr_st->priv_data;
|
2005-06-03 17:05:41 +03:00
|
|
|
service->pcr_pid = ts_st->pid;
|
2014-05-18 13:12:59 +03:00
|
|
|
} else
|
|
|
|
ts_st = pcr_st->priv_data;
|
2005-06-03 17:05:41 +03:00
|
|
|
|
2010-03-09 01:46:19 +02:00
|
|
|
if (ts->mux_rate > 1) {
|
2015-11-28 08:27:39 +02:00
|
|
|
service->pcr_packet_period = (int64_t)ts->mux_rate * ts->pcr_period /
|
2014-08-06 04:02:18 +03:00
|
|
|
(TS_PACKET_SIZE * 8 * 1000);
|
2015-11-28 08:27:39 +02:00
|
|
|
ts->sdt_packet_period = (int64_t)ts->mux_rate * SDT_RETRANS_TIME /
|
2014-08-06 04:02:18 +03:00
|
|
|
(TS_PACKET_SIZE * 8 * 1000);
|
2015-11-28 08:27:39 +02:00
|
|
|
ts->pat_packet_period = (int64_t)ts->mux_rate * PAT_RETRANS_TIME /
|
2014-08-06 04:02:18 +03:00
|
|
|
(TS_PACKET_SIZE * 8 * 1000);
|
2010-03-10 11:19:14 +02:00
|
|
|
|
2014-08-07 22:16:06 +03:00
|
|
|
if (ts->copyts < 1)
|
2012-06-08 04:30:47 +03:00
|
|
|
ts->first_pcr = av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE);
|
2010-03-09 01:46:19 +02:00
|
|
|
} else {
|
2012-02-11 04:19:08 +03:00
|
|
|
/* Arbitrary values, PAT/PMT will also be written on video key frames */
|
2010-03-09 01:46:19 +02:00
|
|
|
ts->sdt_packet_period = 200;
|
|
|
|
ts->pat_packet_period = 40;
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
if (pcr_st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
|
|
|
|
int frame_size = av_get_audio_frame_duration2(pcr_st->codecpar, 0);
|
|
|
|
if (!frame_size) {
|
2010-03-09 01:46:19 +02:00
|
|
|
av_log(s, AV_LOG_WARNING, "frame size not set\n");
|
|
|
|
service->pcr_packet_period =
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
pcr_st->codecpar->sample_rate / (10 * 512);
|
2010-03-09 01:46:19 +02:00
|
|
|
} else {
|
|
|
|
service->pcr_packet_period =
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
pcr_st->codecpar->sample_rate / (10 * frame_size);
|
2010-03-09 01:46:19 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// max delta PCR 0.1s
|
2014-05-18 13:12:59 +03:00
|
|
|
// TODO: should be avg_frame_rate
|
2010-03-09 01:46:19 +02:00
|
|
|
service->pcr_packet_period =
|
2014-05-18 13:12:59 +03:00
|
|
|
ts_st->user_tb.den / (10 * ts_st->user_tb.num);
|
2010-03-09 01:46:19 +02:00
|
|
|
}
|
2014-08-07 22:16:06 +03:00
|
|
|
if (!service->pcr_packet_period)
|
2012-06-10 17:50:06 +03:00
|
|
|
service->pcr_packet_period = 1;
|
2010-03-09 01:46:19 +02:00
|
|
|
}
|
|
|
|
|
2015-07-16 12:56:55 +02:00
|
|
|
ts->last_pat_ts = AV_NOPTS_VALUE;
|
2015-07-18 23:25:07 +02:00
|
|
|
ts->last_sdt_ts = AV_NOPTS_VALUE;
|
2015-07-16 12:56:55 +02:00
|
|
|
// The user specified a period, use only it
|
|
|
|
if (ts->pat_period < INT_MAX/2) {
|
|
|
|
ts->pat_packet_period = INT_MAX;
|
|
|
|
}
|
2015-07-18 23:25:07 +02:00
|
|
|
if (ts->sdt_period < INT_MAX/2) {
|
|
|
|
ts->sdt_packet_period = INT_MAX;
|
|
|
|
}
|
2015-07-16 12:56:55 +02:00
|
|
|
|
2010-01-13 22:26:59 +02:00
|
|
|
// output a PCR as soon as possible
|
|
|
|
service->pcr_packet_count = service->pcr_packet_period;
|
2014-08-06 04:02:18 +03:00
|
|
|
ts->pat_packet_count = ts->pat_packet_period - 1;
|
|
|
|
ts->sdt_packet_count = ts->sdt_packet_period - 1;
|
2010-01-13 22:26:59 +02:00
|
|
|
|
2010-07-09 01:01:31 +03:00
|
|
|
if (ts->mux_rate == 1)
|
2012-10-09 23:00:22 +03:00
|
|
|
av_log(s, AV_LOG_VERBOSE, "muxrate VBR, ");
|
2010-07-09 01:01:31 +03:00
|
|
|
else
|
2012-10-09 23:00:22 +03:00
|
|
|
av_log(s, AV_LOG_VERBOSE, "muxrate %d, ", ts->mux_rate);
|
2014-08-06 04:02:18 +03:00
|
|
|
av_log(s, AV_LOG_VERBOSE,
|
|
|
|
"pcr every %d pkts, sdt every %d, pat/pmt every %d pkts\n",
|
2010-07-09 01:01:31 +03:00
|
|
|
service->pcr_packet_period,
|
2010-03-09 01:46:19 +02:00
|
|
|
ts->sdt_packet_period, ts->pat_packet_period);
|
2010-01-13 23:42:55 +02:00
|
|
|
|
2011-08-13 16:51:36 +03:00
|
|
|
if (ts->m2ts_mode == -1) {
|
|
|
|
if (av_match_ext(s->filename, "m2ts")) {
|
|
|
|
ts->m2ts_mode = 1;
|
|
|
|
} else {
|
|
|
|
ts->m2ts_mode = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-13 17:28:45 +03:00
|
|
|
return 0;
|
|
|
|
|
2014-08-06 04:02:18 +03:00
|
|
|
fail:
|
2014-08-08 00:04:32 +03:00
|
|
|
av_freep(&pids);
|
2012-06-01 12:44:56 +03:00
|
|
|
return ret;
|
2003-06-13 17:28:45 +03:00
|
|
|
}
|
|
|
|
|
2016-04-27 19:45:23 +02:00
|
|
|
/* send SDT, PAT and PMT tables regularly */
|
2015-07-16 12:56:55 +02:00
|
|
|
static void retransmit_si_info(AVFormatContext *s, int force_pat, int64_t dts)
|
2003-06-13 17:28:45 +03:00
|
|
|
{
|
|
|
|
MpegTSWrite *ts = s->priv_data;
|
|
|
|
int i;
|
|
|
|
|
2015-07-18 23:25:07 +02:00
|
|
|
if (++ts->sdt_packet_count == ts->sdt_packet_period ||
|
|
|
|
(dts != AV_NOPTS_VALUE && ts->last_sdt_ts == AV_NOPTS_VALUE) ||
|
|
|
|
(dts != AV_NOPTS_VALUE && dts - ts->last_sdt_ts >= ts->sdt_period*90000.0)
|
|
|
|
) {
|
2003-06-13 17:28:45 +03:00
|
|
|
ts->sdt_packet_count = 0;
|
2015-07-18 23:25:07 +02:00
|
|
|
if (dts != AV_NOPTS_VALUE)
|
|
|
|
ts->last_sdt_ts = FFMAX(dts, ts->last_sdt_ts);
|
2003-06-13 17:28:45 +03:00
|
|
|
mpegts_write_sdt(s);
|
|
|
|
}
|
2015-07-16 12:56:55 +02:00
|
|
|
if (++ts->pat_packet_count == ts->pat_packet_period ||
|
|
|
|
(dts != AV_NOPTS_VALUE && ts->last_pat_ts == AV_NOPTS_VALUE) ||
|
|
|
|
(dts != AV_NOPTS_VALUE && dts - ts->last_pat_ts >= ts->pat_period*90000.0) ||
|
|
|
|
force_pat) {
|
2003-06-13 17:28:45 +03:00
|
|
|
ts->pat_packet_count = 0;
|
2015-07-16 12:56:55 +02:00
|
|
|
if (dts != AV_NOPTS_VALUE)
|
|
|
|
ts->last_pat_ts = FFMAX(dts, ts->last_pat_ts);
|
2003-06-13 17:28:45 +03:00
|
|
|
mpegts_write_pat(s);
|
2014-08-06 04:02:18 +03:00
|
|
|
for (i = 0; i < ts->nb_services; i++)
|
2003-06-13 17:28:45 +03:00
|
|
|
mpegts_write_pmt(s, ts->services[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-29 16:01:39 +03:00
|
|
|
static int write_pcr_bits(uint8_t *buf, int64_t pcr)
|
2010-12-03 15:26:42 +02:00
|
|
|
{
|
|
|
|
int64_t pcr_low = pcr % 300, pcr_high = pcr / 300;
|
|
|
|
|
|
|
|
*buf++ = pcr_high >> 25;
|
|
|
|
*buf++ = pcr_high >> 17;
|
2014-08-06 04:02:18 +03:00
|
|
|
*buf++ = pcr_high >> 9;
|
|
|
|
*buf++ = pcr_high >> 1;
|
|
|
|
*buf++ = pcr_high << 7 | pcr_low >> 8 | 0x7e;
|
2010-12-03 15:26:42 +02:00
|
|
|
*buf++ = pcr_low;
|
|
|
|
|
2011-06-29 16:01:39 +03:00
|
|
|
return 6;
|
2010-12-03 15:26:42 +02:00
|
|
|
}
|
|
|
|
|
2010-01-13 22:44:56 +02:00
|
|
|
/* Write a single null transport stream packet */
|
|
|
|
static void mpegts_insert_null_packet(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
uint8_t *q;
|
|
|
|
uint8_t buf[TS_PACKET_SIZE];
|
|
|
|
|
2014-08-06 04:02:18 +03:00
|
|
|
q = buf;
|
2010-01-13 22:44:56 +02:00
|
|
|
*q++ = 0x47;
|
|
|
|
*q++ = 0x00 | 0x1f;
|
|
|
|
*q++ = 0xff;
|
|
|
|
*q++ = 0x10;
|
|
|
|
memset(q, 0x0FF, TS_PACKET_SIZE - (q - buf));
|
2011-08-13 16:51:36 +03:00
|
|
|
mpegts_prefix_m2ts_header(s);
|
2011-02-21 20:28:17 +02:00
|
|
|
avio_write(s->pb, buf, TS_PACKET_SIZE);
|
2010-01-13 22:44:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Write a single transport stream packet with a PCR and no payload */
|
|
|
|
static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
|
|
|
|
{
|
|
|
|
MpegTSWrite *ts = s->priv_data;
|
|
|
|
MpegTSWriteStream *ts_st = st->priv_data;
|
|
|
|
uint8_t *q;
|
|
|
|
uint8_t buf[TS_PACKET_SIZE];
|
|
|
|
|
2014-08-06 04:02:18 +03:00
|
|
|
q = buf;
|
2010-01-13 22:44:56 +02:00
|
|
|
*q++ = 0x47;
|
|
|
|
*q++ = ts_st->pid >> 8;
|
|
|
|
*q++ = ts_st->pid;
|
|
|
|
*q++ = 0x20 | ts_st->cc; /* Adaptation only */
|
|
|
|
/* Continuity Count field does not increment (see 13818-1 section 2.4.3.3) */
|
|
|
|
*q++ = TS_PACKET_SIZE - 5; /* Adaptation Field Length */
|
|
|
|
*q++ = 0x10; /* Adaptation flags: PCR present */
|
|
|
|
|
|
|
|
/* PCR coded into 6 bytes */
|
2011-06-29 16:01:39 +03:00
|
|
|
q += write_pcr_bits(q, get_pcr(ts, s->pb));
|
2010-01-13 22:44:56 +02:00
|
|
|
|
|
|
|
/* stuffing bytes */
|
|
|
|
memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
|
2011-08-13 16:51:36 +03:00
|
|
|
mpegts_prefix_m2ts_header(s);
|
2011-02-21 20:28:17 +02:00
|
|
|
avio_write(s->pb, buf, TS_PACKET_SIZE);
|
2010-01-13 22:44:56 +02:00
|
|
|
}
|
|
|
|
|
2007-03-02 23:47:24 +02:00
|
|
|
static void write_pts(uint8_t *q, int fourbits, int64_t pts)
|
|
|
|
{
|
|
|
|
int val;
|
|
|
|
|
2014-08-06 04:02:18 +03:00
|
|
|
val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
|
2007-03-02 23:47:24 +02:00
|
|
|
*q++ = val;
|
2014-08-06 04:02:18 +03:00
|
|
|
val = (((pts >> 15) & 0x7fff) << 1) | 1;
|
2007-03-02 23:47:24 +02:00
|
|
|
*q++ = val >> 8;
|
|
|
|
*q++ = val;
|
2014-08-06 04:02:18 +03:00
|
|
|
val = (((pts) & 0x7fff) << 1) | 1;
|
2007-03-02 23:47:24 +02:00
|
|
|
*q++ = val >> 8;
|
|
|
|
*q++ = val;
|
|
|
|
}
|
|
|
|
|
2011-06-29 16:01:39 +03:00
|
|
|
/* Set an adaptation field flag in an MPEG-TS packet*/
|
|
|
|
static void set_af_flag(uint8_t *pkt, int flag)
|
|
|
|
{
|
|
|
|
// expect at least one flag to set
|
2012-06-13 19:51:38 +03:00
|
|
|
av_assert0(flag);
|
2011-06-29 16:01:39 +03:00
|
|
|
|
|
|
|
if ((pkt[3] & 0x20) == 0) {
|
|
|
|
// no AF yet, set adaptation field flag
|
|
|
|
pkt[3] |= 0x20;
|
|
|
|
// 1 byte length, no flags
|
|
|
|
pkt[4] = 1;
|
|
|
|
pkt[5] = 0;
|
|
|
|
}
|
|
|
|
pkt[5] |= flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extend the adaptation field by size bytes */
|
|
|
|
static void extend_af(uint8_t *pkt, int size)
|
|
|
|
{
|
|
|
|
// expect already existing adaptation field
|
2012-06-13 19:51:38 +03:00
|
|
|
av_assert0(pkt[3] & 0x20);
|
2011-06-29 16:01:39 +03:00
|
|
|
pkt[4] += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a pointer to MPEG-TS payload (right after TS packet header) */
|
|
|
|
static uint8_t *get_ts_payload_start(uint8_t *pkt)
|
|
|
|
{
|
|
|
|
if (pkt[3] & 0x20)
|
|
|
|
return pkt + 5 + pkt[4];
|
|
|
|
else
|
|
|
|
return pkt + 4;
|
|
|
|
}
|
|
|
|
|
2014-08-06 04:02:18 +03:00
|
|
|
/* Add a PES header to the front of the payload, and segment into an integer
|
|
|
|
* number of TS packets. The final TS packet is padded using an oversized
|
|
|
|
* adaptation header to exactly fill the last TS packet.
|
|
|
|
* NOTE: 'payload' contains a complete PES payload. */
|
2003-09-16 15:56:42 +03:00
|
|
|
static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
|
|
|
|
const uint8_t *payload, int payload_size,
|
2015-11-18 20:23:03 +02:00
|
|
|
int64_t pts, int64_t dts, int key, int stream_id)
|
2003-06-13 17:28:45 +03:00
|
|
|
{
|
|
|
|
MpegTSWriteStream *ts_st = st->priv_data;
|
2009-01-14 04:20:26 +02:00
|
|
|
MpegTSWrite *ts = s->priv_data;
|
2003-09-16 15:56:42 +03:00
|
|
|
uint8_t buf[TS_PACKET_SIZE];
|
2003-06-13 17:28:45 +03:00
|
|
|
uint8_t *q;
|
2014-01-24 18:34:58 +03:00
|
|
|
int val, is_start, len, header_len, write_pcr, is_dvb_subtitle, is_dvb_teletext, flags;
|
2005-06-03 17:05:41 +03:00
|
|
|
int afc_len, stuffing_len;
|
|
|
|
int64_t pcr = -1; /* avoid warning */
|
2010-01-13 22:44:56 +02:00
|
|
|
int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
|
2016-04-10 21:58:15 +02:00
|
|
|
int force_pat = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && key && !ts_st->prev_payload_key;
|
2003-06-13 17:28:45 +03:00
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
av_assert0(ts_st->payload != buf || st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO);
|
|
|
|
if (ts->flags & MPEGTS_FLAG_PAT_PMT_AT_FRAMES && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
|
2015-07-16 01:54:56 +02:00
|
|
|
force_pat = 1;
|
|
|
|
}
|
|
|
|
|
2003-09-16 15:56:42 +03:00
|
|
|
is_start = 1;
|
|
|
|
while (payload_size > 0) {
|
2015-07-16 12:56:55 +02:00
|
|
|
retransmit_si_info(s, force_pat, dts);
|
2012-02-11 04:19:08 +03:00
|
|
|
force_pat = 0;
|
2003-06-13 17:28:45 +03:00
|
|
|
|
2005-06-03 17:05:41 +03:00
|
|
|
write_pcr = 0;
|
|
|
|
if (ts_st->pid == ts_st->service->pcr_pid) {
|
2010-03-09 01:46:19 +02:00
|
|
|
if (ts->mux_rate > 1 || is_start) // VBR pcr period is based on frames
|
|
|
|
ts_st->service->pcr_packet_count++;
|
2005-12-17 20:14:38 +02:00
|
|
|
if (ts_st->service->pcr_packet_count >=
|
2009-07-28 20:51:12 +03:00
|
|
|
ts_st->service->pcr_packet_period) {
|
2005-06-03 17:05:41 +03:00
|
|
|
ts_st->service->pcr_packet_count = 0;
|
|
|
|
write_pcr = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-09 01:46:19 +02:00
|
|
|
if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE &&
|
2014-08-06 04:02:18 +03:00
|
|
|
(dts - get_pcr(ts, s->pb) / 300) > delay) {
|
2010-01-13 22:44:56 +02:00
|
|
|
/* pcr insert gets priority over null packet insert */
|
|
|
|
if (write_pcr)
|
|
|
|
mpegts_insert_pcr_only(s, st);
|
|
|
|
else
|
|
|
|
mpegts_insert_null_packet(s);
|
2014-08-06 04:02:18 +03:00
|
|
|
/* recalculate write_pcr and possibly retransmit si_info */
|
|
|
|
continue;
|
2010-01-13 22:44:56 +02:00
|
|
|
}
|
|
|
|
|
2003-09-16 15:56:42 +03:00
|
|
|
/* prepare packet header */
|
2014-08-06 04:02:18 +03:00
|
|
|
q = buf;
|
2003-09-16 15:56:42 +03:00
|
|
|
*q++ = 0x47;
|
2014-08-06 14:39:24 +03:00
|
|
|
val = ts_st->pid >> 8;
|
2003-09-16 15:56:42 +03:00
|
|
|
if (is_start)
|
|
|
|
val |= 0x40;
|
2014-08-06 04:02:18 +03:00
|
|
|
*q++ = val;
|
|
|
|
*q++ = ts_st->pid;
|
2014-08-06 14:39:24 +03:00
|
|
|
ts_st->cc = ts_st->cc + 1 & 0xf;
|
2014-08-06 04:02:18 +03:00
|
|
|
*q++ = 0x10 | ts_st->cc; // payload indicator + CC
|
2011-06-29 16:01:39 +03:00
|
|
|
if (key && is_start && pts != AV_NOPTS_VALUE) {
|
|
|
|
// set Random Access for key frames
|
|
|
|
if (ts_st->pid == ts_st->service->pcr_pid)
|
|
|
|
write_pcr = 1;
|
|
|
|
set_af_flag(buf, 0x40);
|
|
|
|
q = get_ts_payload_start(buf);
|
|
|
|
}
|
2005-06-03 17:05:41 +03:00
|
|
|
if (write_pcr) {
|
2011-06-29 16:01:39 +03:00
|
|
|
set_af_flag(buf, 0x10);
|
|
|
|
q = get_ts_payload_start(buf);
|
2009-01-14 04:20:26 +02:00
|
|
|
// add 11, pcr references the last byte of program clock reference base
|
2010-03-09 01:46:19 +02:00
|
|
|
if (ts->mux_rate > 1)
|
2010-12-03 15:26:42 +02:00
|
|
|
pcr = get_pcr(ts, s->pb);
|
2010-03-09 01:46:19 +02:00
|
|
|
else
|
2014-08-06 04:02:18 +03:00
|
|
|
pcr = (dts - delay) * 300;
|
2010-12-03 15:26:42 +02:00
|
|
|
if (dts != AV_NOPTS_VALUE && dts < pcr / 300)
|
2009-01-15 00:40:37 +02:00
|
|
|
av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
|
2011-06-29 16:01:39 +03:00
|
|
|
extend_af(buf, write_pcr_bits(q, pcr));
|
|
|
|
q = get_ts_payload_start(buf);
|
2005-06-03 17:05:41 +03:00
|
|
|
}
|
2003-09-16 15:56:42 +03:00
|
|
|
if (is_start) {
|
2008-08-13 22:29:35 +03:00
|
|
|
int pes_extension = 0;
|
2014-01-24 18:34:58 +03:00
|
|
|
int pes_header_stuffing_bytes = 0;
|
2003-09-16 15:56:42 +03:00
|
|
|
/* write PES header */
|
|
|
|
*q++ = 0x00;
|
|
|
|
*q++ = 0x00;
|
|
|
|
*q++ = 0x01;
|
2014-01-11 23:54:29 +03:00
|
|
|
is_dvb_subtitle = 0;
|
2014-01-24 18:34:58 +03:00
|
|
|
is_dvb_teletext = 0;
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
|
|
|
|
if (st->codecpar->codec_id == AV_CODEC_ID_DIRAC)
|
2008-08-13 22:29:35 +03:00
|
|
|
*q++ = 0xfd;
|
2014-08-06 04:02:18 +03:00
|
|
|
else
|
2008-08-13 22:29:35 +03:00
|
|
|
*q++ = 0xe0;
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
} else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
|
|
|
|
(st->codecpar->codec_id == AV_CODEC_ID_MP2 ||
|
|
|
|
st->codecpar->codec_id == AV_CODEC_ID_MP3 ||
|
|
|
|
st->codecpar->codec_id == AV_CODEC_ID_AAC)) {
|
2005-12-17 20:14:38 +02:00
|
|
|
*q++ = 0xc0;
|
2016-04-10 21:58:15 +02:00
|
|
|
} else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
|
|
|
|
st->codecpar->codec_id == AV_CODEC_ID_AC3 &&
|
2012-08-02 17:01:56 +03:00
|
|
|
ts->m2ts_mode) {
|
|
|
|
*q++ = 0xfd;
|
2016-04-19 10:19:55 +02:00
|
|
|
} else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA &&
|
|
|
|
st->codecpar->codec_id == AV_CODEC_ID_TIMED_ID3) {
|
|
|
|
*q++ = 0xbd;
|
2016-04-10 21:58:15 +02:00
|
|
|
} else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA) {
|
2015-11-18 20:23:03 +02:00
|
|
|
*q++ = stream_id != -1 ? stream_id : 0xfc;
|
|
|
|
|
|
|
|
if (stream_id == 0xbd) /* asynchronous KLV */
|
|
|
|
pts = dts = AV_NOPTS_VALUE;
|
2005-06-03 17:05:41 +03:00
|
|
|
} else {
|
|
|
|
*q++ = 0xbd;
|
2016-04-10 21:58:15 +02:00
|
|
|
if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
|
|
|
|
if (st->codecpar->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
|
2014-01-24 18:34:58 +03:00
|
|
|
is_dvb_subtitle = 1;
|
2016-04-10 21:58:15 +02:00
|
|
|
} else if (st->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT) {
|
2014-01-24 18:34:58 +03:00
|
|
|
is_dvb_teletext = 1;
|
|
|
|
}
|
2005-06-03 17:05:41 +03:00
|
|
|
}
|
|
|
|
}
|
2007-03-02 23:47:24 +02:00
|
|
|
header_len = 0;
|
2014-08-06 04:02:18 +03:00
|
|
|
flags = 0;
|
2007-03-02 23:47:24 +02:00
|
|
|
if (pts != AV_NOPTS_VALUE) {
|
|
|
|
header_len += 5;
|
2014-08-06 04:02:18 +03:00
|
|
|
flags |= 0x80;
|
2007-03-02 23:47:24 +02:00
|
|
|
}
|
2009-05-10 04:37:58 +03:00
|
|
|
if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
|
2007-03-02 23:47:24 +02:00
|
|
|
header_len += 5;
|
2014-08-06 04:02:18 +03:00
|
|
|
flags |= 0x40;
|
2007-03-02 23:47:24 +02:00
|
|
|
}
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
|
|
|
|
st->codecpar->codec_id == AV_CODEC_ID_DIRAC) {
|
2008-08-13 22:29:35 +03:00
|
|
|
/* set PES_extension_flag */
|
|
|
|
pes_extension = 1;
|
2014-08-06 04:02:18 +03:00
|
|
|
flags |= 0x01;
|
2008-08-13 22:29:35 +03:00
|
|
|
|
2014-08-06 04:02:18 +03:00
|
|
|
/* One byte for PES2 extension flag +
|
|
|
|
* one byte for extension length +
|
|
|
|
* one byte for extension id */
|
2008-08-13 22:29:35 +03:00
|
|
|
header_len += 3;
|
|
|
|
}
|
2012-08-02 17:01:56 +03:00
|
|
|
/* for Blu-ray AC3 Audio the PES Extension flag should be as follow
|
|
|
|
* otherwise it will not play sound on blu-ray
|
|
|
|
*/
|
|
|
|
if (ts->m2ts_mode &&
|
2016-04-10 21:58:15 +02:00
|
|
|
st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
|
|
|
|
st->codecpar->codec_id == AV_CODEC_ID_AC3) {
|
2012-08-02 17:01:56 +03:00
|
|
|
/* set PES_extension_flag */
|
|
|
|
pes_extension = 1;
|
|
|
|
flags |= 0x01;
|
|
|
|
header_len += 3;
|
|
|
|
}
|
2014-01-24 18:34:58 +03:00
|
|
|
if (is_dvb_teletext) {
|
|
|
|
pes_header_stuffing_bytes = 0x24 - header_len;
|
|
|
|
header_len = 0x24;
|
|
|
|
}
|
2007-03-02 23:47:24 +02:00
|
|
|
len = payload_size + header_len + 3;
|
2014-01-11 23:54:29 +03:00
|
|
|
/* 3 extra bytes should be added to DVB subtitle payload: 0x20 0x00 at the beginning and trailing 0xff */
|
|
|
|
if (is_dvb_subtitle) {
|
|
|
|
len += 3;
|
|
|
|
payload_size++;
|
|
|
|
}
|
2009-05-11 05:30:29 +03:00
|
|
|
if (len > 0xffff)
|
|
|
|
len = 0;
|
2016-04-10 21:58:15 +02:00
|
|
|
if (ts->omit_video_pes_length && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
|
2014-04-12 07:26:46 +03:00
|
|
|
len = 0;
|
|
|
|
}
|
2003-09-16 15:56:42 +03:00
|
|
|
*q++ = len >> 8;
|
|
|
|
*q++ = len;
|
2014-08-06 04:02:18 +03:00
|
|
|
val = 0x80;
|
2013-04-11 19:57:15 +03:00
|
|
|
/* data alignment indicator is required for subtitle and data streams */
|
2016-04-10 21:58:15 +02:00
|
|
|
if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE || st->codecpar->codec_type == AVMEDIA_TYPE_DATA)
|
2005-06-03 17:05:41 +03:00
|
|
|
val |= 0x04;
|
|
|
|
*q++ = val;
|
2007-03-02 23:47:24 +02:00
|
|
|
*q++ = flags;
|
|
|
|
*q++ = header_len;
|
2003-09-16 15:56:42 +03:00
|
|
|
if (pts != AV_NOPTS_VALUE) {
|
2007-03-02 23:47:24 +02:00
|
|
|
write_pts(q, flags >> 6, pts);
|
|
|
|
q += 5;
|
|
|
|
}
|
2009-05-10 04:37:58 +03:00
|
|
|
if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
|
2007-03-02 23:47:24 +02:00
|
|
|
write_pts(q, 1, dts);
|
|
|
|
q += 5;
|
2003-06-13 17:28:45 +03:00
|
|
|
}
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
if (pes_extension && st->codecpar->codec_id == AV_CODEC_ID_DIRAC) {
|
2008-08-13 22:29:35 +03:00
|
|
|
flags = 0x01; /* set PES_extension_flag_2 */
|
2014-08-06 04:02:18 +03:00
|
|
|
*q++ = flags;
|
|
|
|
*q++ = 0x80 | 0x01; /* marker bit + extension length */
|
|
|
|
/* Set the stream ID extension flag bit to 0 and
|
|
|
|
* write the extended stream ID. */
|
2008-08-13 22:29:35 +03:00
|
|
|
*q++ = 0x00 | 0x60;
|
|
|
|
}
|
2012-08-02 17:01:56 +03:00
|
|
|
/* For Blu-ray AC3 Audio Setting extended flags */
|
|
|
|
if (ts->m2ts_mode &&
|
|
|
|
pes_extension &&
|
2016-04-10 21:58:15 +02:00
|
|
|
st->codecpar->codec_id == AV_CODEC_ID_AC3) {
|
2012-08-02 17:01:56 +03:00
|
|
|
flags = 0x01; /* set PES_extension_flag_2 */
|
|
|
|
*q++ = flags;
|
|
|
|
*q++ = 0x80 | 0x01; /* marker bit + extension length */
|
|
|
|
*q++ = 0x00 | 0x71; /* for AC3 Audio (specifically on blue-rays) */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-11 23:54:29 +03:00
|
|
|
if (is_dvb_subtitle) {
|
|
|
|
/* First two fields of DVB subtitles PES data:
|
|
|
|
* data_identifier: for DVB subtitle streams shall be coded with the value 0x20
|
|
|
|
* subtitle_stream_id: for DVB subtitle stream shall be identified by the value 0x00 */
|
|
|
|
*q++ = 0x20;
|
|
|
|
*q++ = 0x00;
|
|
|
|
}
|
2014-01-24 18:34:58 +03:00
|
|
|
if (is_dvb_teletext) {
|
|
|
|
memset(q, 0xff, pes_header_stuffing_bytes);
|
|
|
|
q += pes_header_stuffing_bytes;
|
|
|
|
}
|
2003-09-16 15:56:42 +03:00
|
|
|
is_start = 0;
|
2003-06-13 17:28:45 +03:00
|
|
|
}
|
2005-06-03 17:05:41 +03:00
|
|
|
/* header size */
|
|
|
|
header_len = q - buf;
|
|
|
|
/* data len */
|
|
|
|
len = TS_PACKET_SIZE - header_len;
|
2003-09-16 15:56:42 +03:00
|
|
|
if (len > payload_size)
|
|
|
|
len = payload_size;
|
2005-06-03 17:05:41 +03:00
|
|
|
stuffing_len = TS_PACKET_SIZE - header_len - len;
|
|
|
|
if (stuffing_len > 0) {
|
|
|
|
/* add stuffing with AFC */
|
|
|
|
if (buf[3] & 0x20) {
|
|
|
|
/* stuffing already present: increase its size */
|
|
|
|
afc_len = buf[4] + 1;
|
|
|
|
memmove(buf + 4 + afc_len + stuffing_len,
|
2005-12-17 20:14:38 +02:00
|
|
|
buf + 4 + afc_len,
|
2005-06-03 17:05:41 +03:00
|
|
|
header_len - (4 + afc_len));
|
|
|
|
buf[4] += stuffing_len;
|
|
|
|
memset(buf + 4 + afc_len, 0xff, stuffing_len);
|
|
|
|
} else {
|
|
|
|
/* add stuffing */
|
|
|
|
memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
|
|
|
|
buf[3] |= 0x20;
|
2014-08-06 04:02:18 +03:00
|
|
|
buf[4] = stuffing_len - 1;
|
2005-06-03 17:05:41 +03:00
|
|
|
if (stuffing_len >= 2) {
|
|
|
|
buf[5] = 0x00;
|
|
|
|
memset(buf + 6, 0xff, stuffing_len - 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-11 23:54:29 +03:00
|
|
|
|
|
|
|
if (is_dvb_subtitle && payload_size == len) {
|
|
|
|
memcpy(buf + TS_PACKET_SIZE - len, payload, len - 1);
|
|
|
|
buf[TS_PACKET_SIZE - 1] = 0xff; /* end_of_PES_data_field_marker: an 8-bit field with fixed contents 0xff for DVB subtitle */
|
|
|
|
} else {
|
|
|
|
memcpy(buf + TS_PACKET_SIZE - len, payload, len);
|
|
|
|
}
|
|
|
|
|
2014-08-06 04:02:18 +03:00
|
|
|
payload += len;
|
2003-09-16 15:56:42 +03:00
|
|
|
payload_size -= len;
|
2011-08-13 16:51:36 +03:00
|
|
|
mpegts_prefix_m2ts_header(s);
|
2011-02-21 20:28:17 +02:00
|
|
|
avio_write(s->pb, buf, TS_PACKET_SIZE);
|
2003-06-13 17:28:45 +03:00
|
|
|
}
|
2012-02-11 04:19:08 +03:00
|
|
|
ts_st->prev_payload_key = key;
|
2003-09-16 15:56:42 +03:00
|
|
|
}
|
|
|
|
|
2014-03-30 04:41:01 +03:00
|
|
|
int ff_check_h264_startcode(AVFormatContext *s, const AVStream *st, const AVPacket *pkt)
|
|
|
|
{
|
2015-02-25 16:08:33 +02:00
|
|
|
if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001 && AV_RB24(pkt->data) != 0x000001) {
|
2014-03-30 04:41:01 +03:00
|
|
|
if (!st->nb_frames) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "H.264 bitstream malformed, "
|
2014-08-19 15:28:35 +03:00
|
|
|
"no startcode found, use the video bitstream filter 'h264_mp4toannexb' to fix it "
|
|
|
|
"('-bsf:v h264_mp4toannexb' option with ffmpeg)\n");
|
2014-08-10 21:34:28 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2014-03-30 04:41:01 +03:00
|
|
|
}
|
2014-09-16 04:56:03 +03:00
|
|
|
av_log(s, AV_LOG_WARNING, "H.264 bitstream error, startcode missing, size %d", pkt->size);
|
|
|
|
if (pkt->size) av_log(s, AV_LOG_WARNING, " data %08X", AV_RB32(pkt->data));
|
|
|
|
av_log(s, AV_LOG_WARNING, "\n");
|
2014-03-30 04:41:01 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-12 09:48:20 +03:00
|
|
|
static int check_hevc_startcode(AVFormatContext *s, const AVStream *st, const AVPacket *pkt)
|
|
|
|
{
|
2015-04-14 01:57:38 +02:00
|
|
|
if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001 && AV_RB24(pkt->data) != 0x000001) {
|
2014-08-12 09:48:20 +03:00
|
|
|
if (!st->nb_frames) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "HEVC bitstream malformed, no startcode found\n");
|
|
|
|
return AVERROR_PATCHWELCOME;
|
|
|
|
}
|
2014-09-16 04:56:03 +03:00
|
|
|
av_log(s, AV_LOG_WARNING, "HEVC bitstream error, startcode missing, size %d", pkt->size);
|
|
|
|
if (pkt->size) av_log(s, AV_LOG_WARNING, " data %08X", AV_RB32(pkt->data));
|
|
|
|
av_log(s, AV_LOG_WARNING, "\n");
|
2014-08-12 09:48:20 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-06 00:35:42 +02:00
|
|
|
/* Based on GStreamer's gst-plugins-base/ext/ogg/gstoggstream.c
|
|
|
|
* Released under the LGPL v2.1+, written by
|
|
|
|
* Vincent Penquerc'h <vincent.penquerch@collabora.co.uk>
|
|
|
|
*/
|
|
|
|
static int opus_get_packet_samples(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
static const int durations[32] = {
|
|
|
|
480, 960, 1920, 2880, /* Silk NB */
|
|
|
|
480, 960, 1920, 2880, /* Silk MB */
|
|
|
|
480, 960, 1920, 2880, /* Silk WB */
|
|
|
|
480, 960, /* Hybrid SWB */
|
|
|
|
480, 960, /* Hybrid FB */
|
|
|
|
120, 240, 480, 960, /* CELT NB */
|
|
|
|
120, 240, 480, 960, /* CELT NB */
|
|
|
|
120, 240, 480, 960, /* CELT NB */
|
|
|
|
120, 240, 480, 960, /* CELT NB */
|
|
|
|
};
|
|
|
|
int toc, frame_duration, nframes, duration;
|
|
|
|
|
|
|
|
if (pkt->size < 1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
toc = pkt->data[0];
|
|
|
|
|
|
|
|
frame_duration = durations[toc >> 3];
|
|
|
|
switch (toc & 3) {
|
|
|
|
case 0:
|
|
|
|
nframes = 1;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
nframes = 2;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
nframes = 2;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
if (pkt->size < 2)
|
|
|
|
return 0;
|
|
|
|
nframes = pkt->data[1] & 63;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
duration = nframes * frame_duration;
|
|
|
|
if (duration > 5760) {
|
|
|
|
av_log(s, AV_LOG_WARNING,
|
|
|
|
"Opus packet duration > 120 ms, invalid");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return duration;
|
|
|
|
}
|
|
|
|
|
2012-03-21 14:35:15 +03:00
|
|
|
static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
|
2003-09-16 15:56:42 +03:00
|
|
|
{
|
2004-05-29 05:06:32 +03:00
|
|
|
AVStream *st = s->streams[pkt->stream_index];
|
2010-01-14 00:21:45 +02:00
|
|
|
int size = pkt->size;
|
2014-08-06 04:02:18 +03:00
|
|
|
uint8_t *buf = pkt->data;
|
|
|
|
uint8_t *data = NULL;
|
2011-12-19 22:15:21 +03:00
|
|
|
MpegTSWrite *ts = s->priv_data;
|
2003-09-16 15:56:42 +03:00
|
|
|
MpegTSWriteStream *ts_st = st->priv_data;
|
2014-08-07 22:16:06 +03:00
|
|
|
const int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) * 2;
|
2012-06-08 04:30:47 +03:00
|
|
|
int64_t dts = pkt->dts, pts = pkt->pts;
|
2015-11-06 00:35:42 +02:00
|
|
|
int opus_samples = 0;
|
2015-11-18 20:23:03 +02:00
|
|
|
int side_data_size;
|
|
|
|
char *side_data = NULL;
|
|
|
|
int stream_id = -1;
|
|
|
|
|
|
|
|
side_data = av_packet_get_side_data(pkt,
|
|
|
|
AV_PKT_DATA_MPEGTS_STREAM_ID,
|
|
|
|
&side_data_size);
|
|
|
|
if (side_data)
|
|
|
|
stream_id = side_data[0];
|
2009-01-15 03:47:24 +02:00
|
|
|
|
2012-01-29 03:12:06 +03:00
|
|
|
if (ts->reemit_pat_pmt) {
|
2014-08-06 04:02:18 +03:00
|
|
|
av_log(s, AV_LOG_WARNING,
|
|
|
|
"resend_headers option is deprecated, use -mpegts_flags resend_headers\n");
|
2012-06-01 11:42:48 +03:00
|
|
|
ts->reemit_pat_pmt = 0;
|
2014-08-06 04:02:18 +03:00
|
|
|
ts->flags |= MPEGTS_FLAG_REEMIT_PAT_PMT;
|
2012-06-01 11:42:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ts->flags & MPEGTS_FLAG_REEMIT_PAT_PMT) {
|
2012-01-29 03:12:06 +03:00
|
|
|
ts->pat_packet_count = ts->pat_packet_period - 1;
|
|
|
|
ts->sdt_packet_count = ts->sdt_packet_period - 1;
|
2014-08-06 04:02:18 +03:00
|
|
|
ts->flags &= ~MPEGTS_FLAG_REEMIT_PAT_PMT;
|
2012-01-29 03:12:06 +03:00
|
|
|
}
|
|
|
|
|
2014-08-07 22:16:06 +03:00
|
|
|
if (ts->copyts < 1) {
|
2012-06-08 04:30:47 +03:00
|
|
|
if (pts != AV_NOPTS_VALUE)
|
|
|
|
pts += delay;
|
|
|
|
if (dts != AV_NOPTS_VALUE)
|
|
|
|
dts += delay;
|
|
|
|
}
|
2003-09-16 15:56:42 +03:00
|
|
|
|
2009-05-10 04:57:21 +03:00
|
|
|
if (ts_st->first_pts_check && pts == AV_NOPTS_VALUE) {
|
2013-01-03 02:04:41 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "first pts value must be set\n");
|
2012-01-12 21:45:10 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2009-05-10 04:57:21 +03:00
|
|
|
}
|
|
|
|
ts_st->first_pts_check = 0;
|
|
|
|
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
|
2014-08-06 04:02:18 +03:00
|
|
|
const uint8_t *p = buf, *buf_end = p + size;
|
2010-03-09 01:59:05 +02:00
|
|
|
uint32_t state = -1;
|
2016-04-10 21:58:15 +02:00
|
|
|
int extradd = (pkt->flags & AV_PKT_FLAG_KEY) ? st->codecpar->extradata_size : 0;
|
2014-03-30 04:41:01 +03:00
|
|
|
int ret = ff_check_h264_startcode(s, st, pkt);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2010-03-09 01:59:05 +02:00
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
if (extradd && AV_RB24(st->codecpar->extradata) > 1)
|
2015-02-12 23:31:47 +02:00
|
|
|
extradd = 0;
|
|
|
|
|
2010-03-09 01:59:05 +02:00
|
|
|
do {
|
2013-03-25 15:11:41 +03:00
|
|
|
p = avpriv_find_start_code(p, buf_end, &state);
|
2015-03-16 10:57:35 +02:00
|
|
|
av_log(s, AV_LOG_TRACE, "nal %d\n", state & 0x1f);
|
2015-02-12 23:31:47 +02:00
|
|
|
if ((state & 0x1f) == 7)
|
|
|
|
extradd = 0;
|
2010-03-09 01:59:05 +02:00
|
|
|
} while (p < buf_end && (state & 0x1f) != 9 &&
|
|
|
|
(state & 0x1f) != 5 && (state & 0x1f) != 1);
|
|
|
|
|
2015-02-12 23:31:47 +02:00
|
|
|
if ((state & 0x1f) != 5)
|
|
|
|
extradd = 0;
|
2010-03-09 01:59:05 +02:00
|
|
|
if ((state & 0x1f) != 9) { // AUD NAL
|
2015-02-12 23:31:47 +02:00
|
|
|
data = av_malloc(pkt->size + 6 + extradd);
|
2009-01-15 03:22:31 +02:00
|
|
|
if (!data)
|
2012-01-12 21:45:10 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2016-04-10 21:58:15 +02:00
|
|
|
memcpy(data + 6, st->codecpar->extradata, extradd);
|
2015-02-12 23:31:47 +02:00
|
|
|
memcpy(data + 6 + extradd, pkt->data, pkt->size);
|
2009-01-15 03:22:31 +02:00
|
|
|
AV_WB32(data, 0x00000001);
|
|
|
|
data[4] = 0x09;
|
2010-07-23 03:27:17 +03:00
|
|
|
data[5] = 0xf0; // any slice type (0xe) + rbsp stop one bit
|
2014-08-06 04:02:18 +03:00
|
|
|
buf = data;
|
2015-02-12 23:31:47 +02:00
|
|
|
size = pkt->size + 6 + extradd;
|
2009-01-15 03:22:31 +02:00
|
|
|
}
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
} else if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
|
2012-06-01 13:42:20 +03:00
|
|
|
if (pkt->size < 2) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "AAC packet too short\n");
|
2012-01-12 21:45:10 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2012-06-01 13:42:20 +03:00
|
|
|
}
|
2010-03-09 02:18:49 +02:00
|
|
|
if ((AV_RB16(pkt->data) & 0xfff0) != 0xfff0) {
|
2012-06-01 13:42:20 +03:00
|
|
|
int ret;
|
|
|
|
AVPacket pkt2;
|
|
|
|
|
|
|
|
if (!ts_st->amux) {
|
2012-01-12 21:46:39 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "AAC bitstream not in ADTS format "
|
2014-08-06 04:02:18 +03:00
|
|
|
"and extradata missing\n");
|
2015-07-07 10:44:46 +02:00
|
|
|
} else {
|
2012-06-01 13:42:20 +03:00
|
|
|
av_init_packet(&pkt2);
|
|
|
|
pkt2.data = pkt->data;
|
|
|
|
pkt2.size = pkt->size;
|
2014-05-19 17:30:01 +03:00
|
|
|
av_assert0(pkt->dts != AV_NOPTS_VALUE);
|
|
|
|
pkt2.dts = av_rescale_q(pkt->dts, st->time_base, ts_st->amux->streams[0]->time_base);
|
2014-08-06 04:02:18 +03:00
|
|
|
|
2012-06-03 07:11:10 +03:00
|
|
|
ret = avio_open_dyn_buf(&ts_st->amux->pb);
|
|
|
|
if (ret < 0)
|
2010-04-03 17:15:00 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2012-06-01 13:42:20 +03:00
|
|
|
|
|
|
|
ret = av_write_frame(ts_st->amux, &pkt2);
|
|
|
|
if (ret < 0) {
|
2015-02-24 14:07:54 +02:00
|
|
|
ffio_free_dyn_buf(&ts_st->amux->pb);
|
2012-06-01 13:42:20 +03:00
|
|
|
return ret;
|
2011-11-28 11:48:53 +03:00
|
|
|
}
|
2014-08-06 04:02:18 +03:00
|
|
|
size = avio_close_dyn_buf(ts_st->amux->pb, &data);
|
2012-06-03 07:11:10 +03:00
|
|
|
ts_st->amux->pb = NULL;
|
2014-08-06 04:02:18 +03:00
|
|
|
buf = data;
|
2015-07-07 10:44:46 +02:00
|
|
|
}
|
2010-03-09 02:18:49 +02:00
|
|
|
}
|
2016-04-10 21:58:15 +02:00
|
|
|
} else if (st->codecpar->codec_id == AV_CODEC_ID_HEVC) {
|
2014-08-12 09:48:20 +03:00
|
|
|
int ret = check_hevc_startcode(s, st, pkt);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2016-04-10 21:58:15 +02:00
|
|
|
} else if (st->codecpar->codec_id == AV_CODEC_ID_OPUS) {
|
2015-11-06 00:35:42 +02:00
|
|
|
if (pkt->size < 2) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Opus packet too short\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add Opus control header */
|
|
|
|
if ((AV_RB16(pkt->data) >> 5) != 0x3ff) {
|
2015-11-02 09:06:19 +02:00
|
|
|
uint8_t *side_data;
|
|
|
|
int side_data_size;
|
2015-11-06 00:35:42 +02:00
|
|
|
int i, n;
|
2015-11-02 09:06:19 +02:00
|
|
|
int ctrl_header_size;
|
|
|
|
int trim_start = 0, trim_end = 0;
|
2015-11-06 00:35:42 +02:00
|
|
|
|
|
|
|
opus_samples = opus_get_packet_samples(s, pkt);
|
|
|
|
|
2015-11-02 09:06:19 +02:00
|
|
|
side_data = av_packet_get_side_data(pkt,
|
|
|
|
AV_PKT_DATA_SKIP_SAMPLES,
|
|
|
|
&side_data_size);
|
|
|
|
|
|
|
|
if (side_data && side_data_size >= 10) {
|
2016-04-10 21:58:15 +02:00
|
|
|
trim_end = AV_RL32(side_data + 4) * 48000 / st->codecpar->sample_rate;
|
2015-11-02 09:06:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ctrl_header_size = pkt->size + 2 + pkt->size / 255 + 1;
|
|
|
|
if (ts_st->opus_pending_trim_start)
|
|
|
|
ctrl_header_size += 2;
|
|
|
|
if (trim_end)
|
|
|
|
ctrl_header_size += 2;
|
|
|
|
|
|
|
|
data = av_malloc(ctrl_header_size);
|
2015-11-06 00:35:42 +02:00
|
|
|
if (!data)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
data[0] = 0x7f;
|
|
|
|
data[1] = 0xe0;
|
2015-11-02 09:06:19 +02:00
|
|
|
if (ts_st->opus_pending_trim_start)
|
|
|
|
data[1] |= 0x10;
|
|
|
|
if (trim_end)
|
|
|
|
data[1] |= 0x08;
|
2015-11-06 00:35:42 +02:00
|
|
|
|
|
|
|
n = pkt->size;
|
|
|
|
i = 2;
|
|
|
|
do {
|
|
|
|
data[i] = FFMIN(n, 255);
|
|
|
|
n -= 255;
|
|
|
|
i++;
|
|
|
|
} while (n >= 0);
|
|
|
|
|
|
|
|
av_assert0(2 + pkt->size / 255 + 1 == i);
|
|
|
|
|
2015-11-02 09:06:19 +02:00
|
|
|
if (ts_st->opus_pending_trim_start) {
|
|
|
|
trim_start = FFMIN(ts_st->opus_pending_trim_start, opus_samples);
|
|
|
|
AV_WB16(data + i, trim_start);
|
|
|
|
i += 2;
|
|
|
|
ts_st->opus_pending_trim_start -= trim_start;
|
|
|
|
}
|
|
|
|
if (trim_end) {
|
|
|
|
trim_end = FFMIN(trim_end, opus_samples - trim_start);
|
|
|
|
AV_WB16(data + i, trim_end);
|
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
|
2015-11-06 00:35:42 +02:00
|
|
|
memcpy(data + i, pkt->data, pkt->size);
|
|
|
|
buf = data;
|
2015-11-02 09:06:19 +02:00
|
|
|
size = ctrl_header_size;
|
2015-11-06 00:35:42 +02:00
|
|
|
} else {
|
|
|
|
/* TODO: Can we get TS formatted data here? If so we will
|
|
|
|
* need to count the samples of that too! */
|
|
|
|
av_log(s, AV_LOG_WARNING, "Got MPEG-TS formatted Opus data, unhandled");
|
|
|
|
}
|
2009-01-14 23:57:10 +02:00
|
|
|
}
|
|
|
|
|
2012-05-11 14:41:06 +03:00
|
|
|
if (pkt->dts != AV_NOPTS_VALUE) {
|
|
|
|
int i;
|
2014-08-07 22:16:06 +03:00
|
|
|
for(i=0; i<s->nb_streams; i++) {
|
2012-05-11 14:41:06 +03:00
|
|
|
AVStream *st2 = s->streams[i];
|
|
|
|
MpegTSWriteStream *ts_st2 = st2->priv_data;
|
2014-08-07 22:16:06 +03:00
|
|
|
if ( ts_st2->payload_size
|
|
|
|
&& (ts_st2->payload_dts == AV_NOPTS_VALUE || dts - ts_st2->payload_dts > delay/2)) {
|
2012-05-11 15:04:33 +03:00
|
|
|
mpegts_write_pes(s, st2, ts_st2->payload, ts_st2->payload_size,
|
2015-11-18 20:23:03 +02:00
|
|
|
ts_st2->payload_pts, ts_st2->payload_dts,
|
|
|
|
ts_st2->payload_flags & AV_PKT_FLAG_KEY, stream_id);
|
2012-05-11 15:04:33 +03:00
|
|
|
ts_st2->payload_size = 0;
|
2012-05-11 14:41:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 20:53:52 +02:00
|
|
|
if (ts_st->payload_size && (ts_st->payload_size + size > ts->pes_payload_size ||
|
|
|
|
(dts != AV_NOPTS_VALUE && ts_st->payload_dts != AV_NOPTS_VALUE &&
|
|
|
|
av_compare_ts(dts - ts_st->payload_dts, st->time_base,
|
2015-11-06 00:35:42 +02:00
|
|
|
s->max_delay, AV_TIME_BASE_Q) >= 0) ||
|
|
|
|
ts_st->opus_queued_samples + opus_samples >= 5760 /* 120ms */)) {
|
2011-12-17 16:08:11 +03:00
|
|
|
mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
|
2011-06-29 16:01:39 +03:00
|
|
|
ts_st->payload_pts, ts_st->payload_dts,
|
2015-11-18 20:23:03 +02:00
|
|
|
ts_st->payload_flags & AV_PKT_FLAG_KEY, stream_id);
|
2011-12-17 16:08:11 +03:00
|
|
|
ts_st->payload_size = 0;
|
2015-11-06 00:35:42 +02:00
|
|
|
ts_st->opus_queued_samples = 0;
|
2009-05-23 09:25:44 +03:00
|
|
|
}
|
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO || size > ts->pes_payload_size) {
|
2011-12-20 03:54:41 +03:00
|
|
|
av_assert0(!ts_st->payload_size);
|
2011-10-19 23:57:36 +03:00
|
|
|
// for video and subtitle, write a single pes packet
|
2014-08-06 04:02:18 +03:00
|
|
|
mpegts_write_pes(s, st, buf, size, pts, dts,
|
2015-11-18 20:23:03 +02:00
|
|
|
pkt->flags & AV_PKT_FLAG_KEY, stream_id);
|
2015-11-06 00:35:42 +02:00
|
|
|
ts_st->opus_queued_samples = 0;
|
2011-10-19 23:57:36 +03:00
|
|
|
av_free(data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-17 16:08:11 +03:00
|
|
|
if (!ts_st->payload_size) {
|
2014-08-06 04:02:18 +03:00
|
|
|
ts_st->payload_pts = pts;
|
|
|
|
ts_st->payload_dts = dts;
|
2011-06-29 16:01:39 +03:00
|
|
|
ts_st->payload_flags = pkt->flags;
|
2003-09-16 15:56:42 +03:00
|
|
|
}
|
2009-01-15 03:22:31 +02:00
|
|
|
|
2011-12-17 16:08:11 +03:00
|
|
|
memcpy(ts_st->payload + ts_st->payload_size, buf, size);
|
|
|
|
ts_st->payload_size += size;
|
2015-11-06 00:35:42 +02:00
|
|
|
ts_st->opus_queued_samples += opus_samples;
|
2010-01-14 00:21:45 +02:00
|
|
|
|
2010-03-10 11:20:32 +02:00
|
|
|
av_free(data);
|
|
|
|
|
2003-06-13 17:28:45 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-21 14:35:15 +03:00
|
|
|
static void mpegts_write_flush(AVFormatContext *s)
|
2003-06-13 17:28:45 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* flush current packets */
|
2014-08-06 04:02:18 +03:00
|
|
|
for (i = 0; i < s->nb_streams; i++) {
|
2012-03-21 14:35:15 +03:00
|
|
|
AVStream *st = s->streams[i];
|
|
|
|
MpegTSWriteStream *ts_st = st->priv_data;
|
2011-12-17 16:08:11 +03:00
|
|
|
if (ts_st->payload_size > 0) {
|
|
|
|
mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
|
2011-06-29 16:01:39 +03:00
|
|
|
ts_st->payload_pts, ts_st->payload_dts,
|
2015-11-18 20:23:03 +02:00
|
|
|
ts_st->payload_flags & AV_PKT_FLAG_KEY, -1);
|
2012-03-21 14:35:15 +03:00
|
|
|
ts_st->payload_size = 0;
|
2015-11-06 00:35:42 +02:00
|
|
|
ts_st->opus_queued_samples = 0;
|
2003-06-13 17:28:45 +03:00
|
|
|
}
|
2012-03-21 14:35:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
if (!pkt) {
|
|
|
|
mpegts_write_flush(s);
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return mpegts_write_packet_internal(s, pkt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mpegts_write_end(AVFormatContext *s)
|
2015-11-30 11:00:41 +02:00
|
|
|
{
|
|
|
|
if (s->pb)
|
|
|
|
mpegts_write_flush(s);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mpegts_deinit(AVFormatContext *s)
|
2012-03-21 14:35:15 +03:00
|
|
|
{
|
|
|
|
MpegTSWrite *ts = s->priv_data;
|
|
|
|
MpegTSService *service;
|
|
|
|
int i;
|
|
|
|
|
2014-08-06 04:02:18 +03:00
|
|
|
for (i = 0; i < s->nb_streams; i++) {
|
2012-03-21 14:35:15 +03:00
|
|
|
AVStream *st = s->streams[i];
|
|
|
|
MpegTSWriteStream *ts_st = st->priv_data;
|
2015-11-30 11:00:41 +02:00
|
|
|
if (ts_st) {
|
|
|
|
av_freep(&ts_st->payload);
|
|
|
|
if (ts_st->amux) {
|
|
|
|
avformat_free_context(ts_st->amux);
|
|
|
|
ts_st->amux = NULL;
|
|
|
|
}
|
2012-06-01 13:42:20 +03:00
|
|
|
}
|
2003-06-13 17:28:45 +03:00
|
|
|
}
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2014-08-06 04:02:18 +03:00
|
|
|
for (i = 0; i < ts->nb_services; i++) {
|
2003-06-13 17:28:45 +03:00
|
|
|
service = ts->services[i];
|
|
|
|
av_freep(&service->provider_name);
|
|
|
|
av_freep(&service->name);
|
2014-12-21 20:24:32 +02:00
|
|
|
av_freep(&service);
|
2003-06-13 17:28:45 +03:00
|
|
|
}
|
2014-12-21 20:24:32 +02:00
|
|
|
av_freep(&ts->services);
|
2015-11-30 11:00:41 +02:00
|
|
|
}
|
2003-06-13 17:28:45 +03:00
|
|
|
|
2015-11-30 11:00:41 +02:00
|
|
|
static int mpegts_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
|
|
|
|
{
|
|
|
|
int ret = 1;
|
|
|
|
AVStream *st = s->streams[pkt->stream_index];
|
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
|
2015-11-30 11:00:41 +02:00
|
|
|
if (pkt->size >= 5 && AV_RB32(pkt->data) != 0x0000001 &&
|
|
|
|
AV_RB24(pkt->data) != 0x000001)
|
|
|
|
ret = ff_stream_add_bitstream_filter(st, "h264_mp4toannexb", NULL);
|
2016-04-10 21:58:15 +02:00
|
|
|
} else if (st->codecpar->codec_id == AV_CODEC_ID_HEVC) {
|
2015-11-30 11:00:41 +02:00
|
|
|
if (pkt->size >= 5 && AV_RB32(pkt->data) != 0x0000001 &&
|
|
|
|
AV_RB24(pkt->data) != 0x000001)
|
|
|
|
ret = ff_stream_add_bitstream_filter(st, "hevc_mp4toannexb", NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2003-06-13 17:28:45 +03:00
|
|
|
}
|
|
|
|
|
2014-06-10 12:57:17 +03:00
|
|
|
static const AVOption options[] = {
|
|
|
|
{ "mpegts_transport_stream_id", "Set transport_stream_id field.",
|
2014-08-06 04:02:18 +03:00
|
|
|
offsetof(MpegTSWrite, transport_stream_id), AV_OPT_TYPE_INT,
|
|
|
|
{ .i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM },
|
2014-06-10 12:57:17 +03:00
|
|
|
{ "mpegts_original_network_id", "Set original_network_id field.",
|
2014-08-06 04:02:18 +03:00
|
|
|
offsetof(MpegTSWrite, original_network_id), AV_OPT_TYPE_INT,
|
2016-05-17 09:54:30 +02:00
|
|
|
{ .i64 = DVB_PRIVATE_NETWORK_START }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM },
|
2014-06-10 12:57:17 +03:00
|
|
|
{ "mpegts_service_id", "Set service_id field.",
|
2014-08-06 04:02:18 +03:00
|
|
|
offsetof(MpegTSWrite, service_id), AV_OPT_TYPE_INT,
|
|
|
|
{ .i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM },
|
2015-02-02 11:56:52 +02:00
|
|
|
{ "mpegts_service_type", "Set service_type field.",
|
|
|
|
offsetof(MpegTSWrite, service_type), AV_OPT_TYPE_INT,
|
|
|
|
{ .i64 = 0x01 }, 0x01, 0xff, AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
|
|
|
|
{ "digital_tv", "Digital Television.",
|
|
|
|
0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_DIGITAL_TV }, 0x01, 0xff,
|
|
|
|
AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
|
|
|
|
{ "digital_radio", "Digital Radio.",
|
|
|
|
0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_DIGITAL_RADIO }, 0x01, 0xff,
|
|
|
|
AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
|
|
|
|
{ "teletext", "Teletext.",
|
|
|
|
0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_TELETEXT }, 0x01, 0xff,
|
|
|
|
AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
|
|
|
|
{ "advanced_codec_digital_radio", "Advanced Codec Digital Radio.",
|
|
|
|
0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_RADIO }, 0x01, 0xff,
|
|
|
|
AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
|
|
|
|
{ "mpeg2_digital_hdtv", "MPEG2 Digital HDTV.",
|
|
|
|
0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_MPEG2_DIGITAL_HDTV }, 0x01, 0xff,
|
|
|
|
AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
|
|
|
|
{ "advanced_codec_digital_sdtv", "Advanced Codec Digital SDTV.",
|
|
|
|
0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_SDTV }, 0x01, 0xff,
|
|
|
|
AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
|
|
|
|
{ "advanced_codec_digital_hdtv", "Advanced Codec Digital HDTV.",
|
|
|
|
0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_HDTV }, 0x01, 0xff,
|
|
|
|
AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
|
2016-05-03 14:25:22 +02:00
|
|
|
{ "hevc_digital_hdtv", "HEVC Digital Television Service.",
|
|
|
|
0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_HEVC_DIGITAL_HDTV }, 0x01, 0xff,
|
|
|
|
AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
|
2014-06-10 12:57:17 +03:00
|
|
|
{ "mpegts_pmt_start_pid", "Set the first pid of the PMT.",
|
2014-08-06 04:02:18 +03:00
|
|
|
offsetof(MpegTSWrite, pmt_start_pid), AV_OPT_TYPE_INT,
|
2014-08-07 22:16:06 +03:00
|
|
|
{ .i64 = 0x1000 }, 0x0010, 0x1f00, AV_OPT_FLAG_ENCODING_PARAM },
|
2014-06-10 12:57:17 +03:00
|
|
|
{ "mpegts_start_pid", "Set the first pid.",
|
2014-08-06 04:02:18 +03:00
|
|
|
offsetof(MpegTSWrite, start_pid), AV_OPT_TYPE_INT,
|
2014-11-18 23:43:07 +02:00
|
|
|
{ .i64 = 0x0100 }, 0x0020, 0x0f00, AV_OPT_FLAG_ENCODING_PARAM },
|
2014-08-07 22:16:06 +03:00
|
|
|
{ "mpegts_m2ts_mode", "Enable m2ts mode.",
|
2015-11-21 23:05:07 +02:00
|
|
|
offsetof(MpegTSWrite, m2ts_mode), AV_OPT_TYPE_BOOL,
|
2014-08-07 22:16:06 +03:00
|
|
|
{ .i64 = -1 }, -1, 1, AV_OPT_FLAG_ENCODING_PARAM },
|
2014-08-06 04:02:18 +03:00
|
|
|
{ "muxrate", NULL,
|
|
|
|
offsetof(MpegTSWrite, mux_rate), AV_OPT_TYPE_INT,
|
|
|
|
{ .i64 = 1 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
|
2014-06-10 12:57:17 +03:00
|
|
|
{ "pes_payload_size", "Minimum PES packet payload in bytes",
|
2014-08-06 04:02:18 +03:00
|
|
|
offsetof(MpegTSWrite, pes_payload_size), AV_OPT_TYPE_INT,
|
|
|
|
{ .i64 = DEFAULT_PES_PAYLOAD_SIZE }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
|
|
|
|
{ "mpegts_flags", "MPEG-TS muxing flags",
|
|
|
|
offsetof(MpegTSWrite, flags), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, 0, INT_MAX,
|
2014-06-10 12:57:17 +03:00
|
|
|
AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
|
|
|
|
{ "resend_headers", "Reemit PAT/PMT before writing the next packet",
|
2014-08-06 04:02:18 +03:00
|
|
|
0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_REEMIT_PAT_PMT }, 0, INT_MAX,
|
|
|
|
AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
|
2014-06-10 12:57:17 +03:00
|
|
|
{ "latm", "Use LATM packetization for AAC",
|
2014-08-06 04:02:18 +03:00
|
|
|
0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_AAC_LATM }, 0, INT_MAX,
|
|
|
|
AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
|
2015-07-16 01:54:56 +02:00
|
|
|
{ "pat_pmt_at_frames", "Reemit PAT and PMT at each video frame",
|
|
|
|
0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_PAT_PMT_AT_FRAMES}, 0, INT_MAX,
|
|
|
|
AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
|
2015-12-13 12:54:32 +02:00
|
|
|
{ "system_b", "Conform to System B (DVB) instead of System A (ATSC)",
|
|
|
|
0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_FLAG_SYSTEM_B }, 0, INT_MAX,
|
|
|
|
AV_OPT_FLAG_ENCODING_PARAM, "mpegts_flags" },
|
2014-06-10 12:57:17 +03:00
|
|
|
// backward compatibility
|
|
|
|
{ "resend_headers", "Reemit PAT/PMT before writing the next packet",
|
2014-08-06 04:02:18 +03:00
|
|
|
offsetof(MpegTSWrite, reemit_pat_pmt), AV_OPT_TYPE_INT,
|
|
|
|
{ .i64 = 0 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
|
2014-06-11 00:27:16 +03:00
|
|
|
{ "mpegts_copyts", "don't offset dts/pts",
|
2015-11-21 23:05:07 +02:00
|
|
|
offsetof(MpegTSWrite, copyts), AV_OPT_TYPE_BOOL,
|
2014-08-07 22:16:06 +03:00
|
|
|
{ .i64 = -1 }, -1, 1, AV_OPT_FLAG_ENCODING_PARAM },
|
2014-06-11 00:27:16 +03:00
|
|
|
{ "tables_version", "set PAT, PMT and SDT version",
|
2014-08-07 22:16:06 +03:00
|
|
|
offsetof(MpegTSWrite, tables_version), AV_OPT_TYPE_INT,
|
|
|
|
{ .i64 = 0 }, 0, 31, AV_OPT_FLAG_ENCODING_PARAM },
|
2014-06-11 00:27:16 +03:00
|
|
|
{ "omit_video_pes_length", "Omit the PES packet length for video packets",
|
2015-11-21 23:05:07 +02:00
|
|
|
offsetof(MpegTSWrite, omit_video_pes_length), AV_OPT_TYPE_BOOL,
|
2014-08-07 22:16:06 +03:00
|
|
|
{ .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
|
2014-06-06 16:59:25 +03:00
|
|
|
{ "pcr_period", "PCR retransmission time",
|
2014-08-06 04:02:18 +03:00
|
|
|
offsetof(MpegTSWrite, pcr_period), AV_OPT_TYPE_INT,
|
|
|
|
{ .i64 = PCR_RETRANS_TIME }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
|
2015-07-16 12:56:55 +02:00
|
|
|
{ "pat_period", "PAT/PMT retransmission time limit in seconds",
|
2015-10-16 21:09:19 +02:00
|
|
|
offsetof(MpegTSWrite, pat_period), AV_OPT_TYPE_DOUBLE,
|
2015-07-16 12:56:55 +02:00
|
|
|
{ .dbl = INT_MAX }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
|
2015-07-18 23:25:07 +02:00
|
|
|
{ "sdt_period", "SDT retransmission time limit in seconds",
|
2015-10-16 21:09:20 +02:00
|
|
|
offsetof(MpegTSWrite, sdt_period), AV_OPT_TYPE_DOUBLE,
|
2015-07-18 23:25:07 +02:00
|
|
|
{ .dbl = INT_MAX }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
|
2014-06-10 12:57:17 +03:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass mpegts_muxer_class = {
|
2014-08-06 04:02:18 +03:00
|
|
|
.class_name = "MPEGTS muxer",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
2014-06-10 12:57:17 +03:00
|
|
|
};
|
|
|
|
|
2011-01-26 00:03:28 +02:00
|
|
|
AVOutputFormat ff_mpegts_muxer = {
|
2014-08-06 04:02:18 +03:00
|
|
|
.name = "mpegts",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
|
2014-08-07 22:16:06 +03:00
|
|
|
.mime_type = "video/MP2T",
|
|
|
|
.extensions = "ts,m2t,m2ts,mts",
|
2014-08-06 04:02:18 +03:00
|
|
|
.priv_data_size = sizeof(MpegTSWrite),
|
|
|
|
.audio_codec = AV_CODEC_ID_MP2,
|
|
|
|
.video_codec = AV_CODEC_ID_MPEG2VIDEO,
|
2015-11-30 11:00:41 +02:00
|
|
|
.init = mpegts_init,
|
2014-08-06 04:02:18 +03:00
|
|
|
.write_packet = mpegts_write_packet,
|
|
|
|
.write_trailer = mpegts_write_end,
|
2015-11-30 11:00:41 +02:00
|
|
|
.deinit = mpegts_deinit,
|
|
|
|
.check_bitstream = mpegts_check_bitstream,
|
2015-06-30 16:24:12 +02:00
|
|
|
.flags = AVFMT_ALLOW_FLUSH | AVFMT_VARIABLE_FPS,
|
2014-08-06 04:02:18 +03:00
|
|
|
.priv_class = &mpegts_muxer_class,
|
2003-06-13 17:28:45 +03:00
|
|
|
};
|