You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

921 lines
29KB

  1. /*
  2. * MPEG2 transport stream (aka DVB) muxer
  3. * Copyright (c) 2003 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/bswap.h"
  22. #include "libavutil/crc.h"
  23. #include "libavcodec/mpegvideo.h"
  24. #include "avformat.h"
  25. #include "internal.h"
  26. #include "mpegts.h"
  27. #include "adts.h"
  28. /* write DVB SI sections */
  29. /*********************************************/
  30. /* mpegts section writer */
  31. typedef struct MpegTSSection {
  32. int pid;
  33. int cc;
  34. void (*write_packet)(struct MpegTSSection *s, const uint8_t *packet);
  35. void *opaque;
  36. } MpegTSSection;
  37. typedef struct MpegTSService {
  38. MpegTSSection pmt; /* MPEG2 pmt table context */
  39. int sid; /* service ID */
  40. char *name;
  41. char *provider_name;
  42. int pcr_pid;
  43. int pcr_packet_count;
  44. int pcr_packet_period;
  45. } MpegTSService;
  46. typedef struct MpegTSWrite {
  47. MpegTSSection pat; /* MPEG2 pat table */
  48. MpegTSSection sdt; /* MPEG2 sdt table context */
  49. MpegTSService **services;
  50. int sdt_packet_count;
  51. int sdt_packet_period;
  52. int pat_packet_count;
  53. int pat_packet_period;
  54. int nb_services;
  55. int onid;
  56. int tsid;
  57. uint64_t cur_pcr;
  58. int mux_rate; ///< set to 1 when VBR
  59. } MpegTSWrite;
  60. /* NOTE: 4 bytes must be left at the end for the crc32 */
  61. static void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
  62. {
  63. MpegTSWrite *ts = ((AVFormatContext*)s->opaque)->priv_data;
  64. unsigned int crc;
  65. unsigned char packet[TS_PACKET_SIZE];
  66. const unsigned char *buf_ptr;
  67. unsigned char *q;
  68. int first, b, len1, left;
  69. crc = bswap_32(av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1, buf, len - 4));
  70. buf[len - 4] = (crc >> 24) & 0xff;
  71. buf[len - 3] = (crc >> 16) & 0xff;
  72. buf[len - 2] = (crc >> 8) & 0xff;
  73. buf[len - 1] = (crc) & 0xff;
  74. /* send each packet */
  75. buf_ptr = buf;
  76. while (len > 0) {
  77. first = (buf == buf_ptr);
  78. q = packet;
  79. *q++ = 0x47;
  80. b = (s->pid >> 8);
  81. if (first)
  82. b |= 0x40;
  83. *q++ = b;
  84. *q++ = s->pid;
  85. s->cc = (s->cc + 1) & 0xf;
  86. *q++ = 0x10 | s->cc;
  87. if (first)
  88. *q++ = 0; /* 0 offset */
  89. len1 = TS_PACKET_SIZE - (q - packet);
  90. if (len1 > len)
  91. len1 = len;
  92. memcpy(q, buf_ptr, len1);
  93. q += len1;
  94. /* add known padding data */
  95. left = TS_PACKET_SIZE - (q - packet);
  96. if (left > 0)
  97. memset(q, 0xff, left);
  98. s->write_packet(s, packet);
  99. buf_ptr += len1;
  100. len -= len1;
  101. ts->cur_pcr += TS_PACKET_SIZE*8*90000LL/ts->mux_rate;
  102. }
  103. }
  104. static inline void put16(uint8_t **q_ptr, int val)
  105. {
  106. uint8_t *q;
  107. q = *q_ptr;
  108. *q++ = val >> 8;
  109. *q++ = val;
  110. *q_ptr = q;
  111. }
  112. static int mpegts_write_section1(MpegTSSection *s, int tid, int id,
  113. int version, int sec_num, int last_sec_num,
  114. uint8_t *buf, int len)
  115. {
  116. uint8_t section[1024], *q;
  117. unsigned int tot_len;
  118. tot_len = 3 + 5 + len + 4;
  119. /* check if not too big */
  120. if (tot_len > 1024)
  121. return -1;
  122. q = section;
  123. *q++ = tid;
  124. put16(&q, 0xb000 | (len + 5 + 4)); /* 5 byte header + 4 byte CRC */
  125. put16(&q, id);
  126. *q++ = 0xc1 | (version << 1); /* current_next_indicator = 1 */
  127. *q++ = sec_num;
  128. *q++ = last_sec_num;
  129. memcpy(q, buf, len);
  130. mpegts_write_section(s, section, tot_len);
  131. return 0;
  132. }
  133. /*********************************************/
  134. /* mpegts writer */
  135. #define DEFAULT_PMT_START_PID 0x1000
  136. #define DEFAULT_START_PID 0x0100
  137. #define DEFAULT_PROVIDER_NAME "FFmpeg"
  138. #define DEFAULT_SERVICE_NAME "Service01"
  139. /* default network id, transport stream and service identifiers */
  140. #define DEFAULT_ONID 0x0001
  141. #define DEFAULT_TSID 0x0001
  142. #define DEFAULT_SID 0x0001
  143. /* a PES packet header is generated every DEFAULT_PES_HEADER_FREQ packets */
  144. #define DEFAULT_PES_HEADER_FREQ 16
  145. #define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
  146. /* we retransmit the SI info at this rate */
  147. #define SDT_RETRANS_TIME 500
  148. #define PAT_RETRANS_TIME 100
  149. #define PCR_RETRANS_TIME 20
  150. typedef struct MpegTSWriteStream {
  151. struct MpegTSService *service;
  152. int pid; /* stream associated pid */
  153. int cc;
  154. int payload_index;
  155. int first_pts_check; ///< first pts check needed
  156. int64_t payload_pts;
  157. int64_t payload_dts;
  158. uint8_t payload[DEFAULT_PES_PAYLOAD_SIZE];
  159. ADTSContext *adts;
  160. } MpegTSWriteStream;
  161. static void mpegts_write_pat(AVFormatContext *s)
  162. {
  163. MpegTSWrite *ts = s->priv_data;
  164. MpegTSService *service;
  165. uint8_t data[1012], *q;
  166. int i;
  167. q = data;
  168. for(i = 0; i < ts->nb_services; i++) {
  169. service = ts->services[i];
  170. put16(&q, service->sid);
  171. put16(&q, 0xe000 | service->pmt.pid);
  172. }
  173. mpegts_write_section1(&ts->pat, PAT_TID, ts->tsid, 0, 0, 0,
  174. data, q - data);
  175. }
  176. static void mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
  177. {
  178. // MpegTSWrite *ts = s->priv_data;
  179. uint8_t data[1012], *q, *desc_length_ptr, *program_info_length_ptr;
  180. int val, stream_type, i;
  181. q = data;
  182. put16(&q, 0xe000 | service->pcr_pid);
  183. program_info_length_ptr = q;
  184. q += 2; /* patched after */
  185. /* put program info here */
  186. val = 0xf000 | (q - program_info_length_ptr - 2);
  187. program_info_length_ptr[0] = val >> 8;
  188. program_info_length_ptr[1] = val;
  189. for(i = 0; i < s->nb_streams; i++) {
  190. AVStream *st = s->streams[i];
  191. MpegTSWriteStream *ts_st = st->priv_data;
  192. AVMetadataTag *lang = av_metadata_get(st->metadata, "language", NULL,0);
  193. switch(st->codec->codec_id) {
  194. case CODEC_ID_MPEG1VIDEO:
  195. case CODEC_ID_MPEG2VIDEO:
  196. stream_type = STREAM_TYPE_VIDEO_MPEG2;
  197. break;
  198. case CODEC_ID_MPEG4:
  199. stream_type = STREAM_TYPE_VIDEO_MPEG4;
  200. break;
  201. case CODEC_ID_H264:
  202. stream_type = STREAM_TYPE_VIDEO_H264;
  203. break;
  204. case CODEC_ID_DIRAC:
  205. stream_type = STREAM_TYPE_VIDEO_DIRAC;
  206. break;
  207. case CODEC_ID_MP2:
  208. case CODEC_ID_MP3:
  209. stream_type = STREAM_TYPE_AUDIO_MPEG1;
  210. break;
  211. case CODEC_ID_AAC:
  212. stream_type = STREAM_TYPE_AUDIO_AAC;
  213. break;
  214. case CODEC_ID_AC3:
  215. stream_type = STREAM_TYPE_AUDIO_AC3;
  216. break;
  217. default:
  218. stream_type = STREAM_TYPE_PRIVATE_DATA;
  219. break;
  220. }
  221. *q++ = stream_type;
  222. put16(&q, 0xe000 | ts_st->pid);
  223. desc_length_ptr = q;
  224. q += 2; /* patched after */
  225. /* write optional descriptors here */
  226. switch(st->codec->codec_type) {
  227. case AVMEDIA_TYPE_AUDIO:
  228. if (lang && strlen(lang->value) == 3) {
  229. *q++ = 0x0a; /* ISO 639 language descriptor */
  230. *q++ = 4;
  231. *q++ = lang->value[0];
  232. *q++ = lang->value[1];
  233. *q++ = lang->value[2];
  234. *q++ = 0; /* undefined type */
  235. }
  236. break;
  237. case AVMEDIA_TYPE_SUBTITLE:
  238. {
  239. const char *language;
  240. language = lang && strlen(lang->value)==3 ? lang->value : "eng";
  241. *q++ = 0x59;
  242. *q++ = 8;
  243. *q++ = language[0];
  244. *q++ = language[1];
  245. *q++ = language[2];
  246. *q++ = 0x10; /* normal subtitles (0x20 = if hearing pb) */
  247. put16(&q, 1); /* page id */
  248. put16(&q, 1); /* ancillary page id */
  249. }
  250. break;
  251. case AVMEDIA_TYPE_VIDEO:
  252. if (stream_type == STREAM_TYPE_VIDEO_DIRAC) {
  253. *q++ = 0x05; /*MPEG-2 registration descriptor*/
  254. *q++ = 4;
  255. *q++ = 'd';
  256. *q++ = 'r';
  257. *q++ = 'a';
  258. *q++ = 'c';
  259. }
  260. break;
  261. }
  262. val = 0xf000 | (q - desc_length_ptr - 2);
  263. desc_length_ptr[0] = val >> 8;
  264. desc_length_ptr[1] = val;
  265. }
  266. mpegts_write_section1(&service->pmt, PMT_TID, service->sid, 0, 0, 0,
  267. data, q - data);
  268. }
  269. /* NOTE: str == NULL is accepted for an empty string */
  270. static void putstr8(uint8_t **q_ptr, const char *str)
  271. {
  272. uint8_t *q;
  273. int len;
  274. q = *q_ptr;
  275. if (!str)
  276. len = 0;
  277. else
  278. len = strlen(str);
  279. *q++ = len;
  280. memcpy(q, str, len);
  281. q += len;
  282. *q_ptr = q;
  283. }
  284. static void mpegts_write_sdt(AVFormatContext *s)
  285. {
  286. MpegTSWrite *ts = s->priv_data;
  287. MpegTSService *service;
  288. uint8_t data[1012], *q, *desc_list_len_ptr, *desc_len_ptr;
  289. int i, running_status, free_ca_mode, val;
  290. q = data;
  291. put16(&q, ts->onid);
  292. *q++ = 0xff;
  293. for(i = 0; i < ts->nb_services; i++) {
  294. service = ts->services[i];
  295. put16(&q, service->sid);
  296. *q++ = 0xfc | 0x00; /* currently no EIT info */
  297. desc_list_len_ptr = q;
  298. q += 2;
  299. running_status = 4; /* running */
  300. free_ca_mode = 0;
  301. /* write only one descriptor for the service name and provider */
  302. *q++ = 0x48;
  303. desc_len_ptr = q;
  304. q++;
  305. *q++ = 0x01; /* digital television service */
  306. putstr8(&q, service->provider_name);
  307. putstr8(&q, service->name);
  308. desc_len_ptr[0] = q - desc_len_ptr - 1;
  309. /* fill descriptor length */
  310. val = (running_status << 13) | (free_ca_mode << 12) |
  311. (q - desc_list_len_ptr - 2);
  312. desc_list_len_ptr[0] = val >> 8;
  313. desc_list_len_ptr[1] = val;
  314. }
  315. mpegts_write_section1(&ts->sdt, SDT_TID, ts->tsid, 0, 0, 0,
  316. data, q - data);
  317. }
  318. static MpegTSService *mpegts_add_service(MpegTSWrite *ts,
  319. int sid,
  320. const char *provider_name,
  321. const char *name)
  322. {
  323. MpegTSService *service;
  324. service = av_mallocz(sizeof(MpegTSService));
  325. if (!service)
  326. return NULL;
  327. service->pmt.pid = DEFAULT_PMT_START_PID + ts->nb_services - 1;
  328. service->sid = sid;
  329. service->provider_name = av_strdup(provider_name);
  330. service->name = av_strdup(name);
  331. service->pcr_pid = 0x1fff;
  332. dynarray_add(&ts->services, &ts->nb_services, service);
  333. return service;
  334. }
  335. static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
  336. {
  337. AVFormatContext *ctx = s->opaque;
  338. put_buffer(ctx->pb, packet, TS_PACKET_SIZE);
  339. }
  340. static int mpegts_write_header(AVFormatContext *s)
  341. {
  342. MpegTSWrite *ts = s->priv_data;
  343. MpegTSWriteStream *ts_st;
  344. MpegTSService *service;
  345. AVStream *st, *pcr_st = NULL;
  346. AVMetadataTag *title;
  347. int i;
  348. const char *service_name;
  349. ts->tsid = DEFAULT_TSID;
  350. ts->onid = DEFAULT_ONID;
  351. /* allocate a single DVB service */
  352. title = av_metadata_get(s->metadata, "title", NULL, 0);
  353. service_name = title ? title->value : DEFAULT_SERVICE_NAME;
  354. service = mpegts_add_service(ts, DEFAULT_SID,
  355. DEFAULT_PROVIDER_NAME, service_name);
  356. service->pmt.write_packet = section_write_packet;
  357. service->pmt.opaque = s;
  358. service->pmt.cc = 15;
  359. ts->pat.pid = PAT_PID;
  360. ts->pat.cc = 15; // Initialize at 15 so that it wraps and be equal to 0 for the first packet we write
  361. ts->pat.write_packet = section_write_packet;
  362. ts->pat.opaque = s;
  363. ts->sdt.pid = SDT_PID;
  364. ts->sdt.cc = 15;
  365. ts->sdt.write_packet = section_write_packet;
  366. ts->sdt.opaque = s;
  367. /* assign pids to each stream */
  368. for(i = 0;i < s->nb_streams; i++) {
  369. st = s->streams[i];
  370. ts_st = av_mallocz(sizeof(MpegTSWriteStream));
  371. if (!ts_st)
  372. goto fail;
  373. st->priv_data = ts_st;
  374. ts_st->service = service;
  375. ts_st->pid = DEFAULT_START_PID + i;
  376. ts_st->payload_pts = AV_NOPTS_VALUE;
  377. ts_st->payload_dts = AV_NOPTS_VALUE;
  378. ts_st->first_pts_check = 1;
  379. ts_st->cc = 15;
  380. /* update PCR pid by using the first video stream */
  381. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
  382. service->pcr_pid == 0x1fff) {
  383. service->pcr_pid = ts_st->pid;
  384. pcr_st = st;
  385. }
  386. if (st->codec->codec_id == CODEC_ID_AAC &&
  387. st->codec->extradata_size > 0) {
  388. ts_st->adts = av_mallocz(sizeof(*ts_st->adts));
  389. if (!ts_st->adts)
  390. return AVERROR(ENOMEM);
  391. if (ff_adts_decode_extradata(s, ts_st->adts, st->codec->extradata,
  392. st->codec->extradata_size) < 0)
  393. return -1;
  394. }
  395. }
  396. /* if no video stream, use the first stream as PCR */
  397. if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
  398. pcr_st = s->streams[0];
  399. ts_st = pcr_st->priv_data;
  400. service->pcr_pid = ts_st->pid;
  401. }
  402. ts->mux_rate = s->mux_rate ? s->mux_rate : 1;
  403. if (ts->mux_rate > 1) {
  404. service->pcr_packet_period = (ts->mux_rate * PCR_RETRANS_TIME) /
  405. (TS_PACKET_SIZE * 8 * 1000);
  406. ts->sdt_packet_period = (ts->mux_rate * SDT_RETRANS_TIME) /
  407. (TS_PACKET_SIZE * 8 * 1000);
  408. ts->pat_packet_period = (ts->mux_rate * PAT_RETRANS_TIME) /
  409. (TS_PACKET_SIZE * 8 * 1000);
  410. ts->cur_pcr = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
  411. } else {
  412. /* Arbitrary values, PAT/PMT could be written on key frames */
  413. ts->sdt_packet_period = 200;
  414. ts->pat_packet_period = 40;
  415. if (pcr_st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  416. if (!pcr_st->codec->frame_size) {
  417. av_log(s, AV_LOG_WARNING, "frame size not set\n");
  418. service->pcr_packet_period =
  419. pcr_st->codec->sample_rate/(10*512);
  420. } else {
  421. service->pcr_packet_period =
  422. pcr_st->codec->sample_rate/(10*pcr_st->codec->frame_size);
  423. }
  424. } else {
  425. // max delta PCR 0.1s
  426. service->pcr_packet_period =
  427. pcr_st->codec->time_base.den/(10*pcr_st->codec->time_base.num);
  428. }
  429. }
  430. // output a PCR as soon as possible
  431. service->pcr_packet_count = service->pcr_packet_period;
  432. ts->pat_packet_count = ts->pat_packet_period-1;
  433. ts->sdt_packet_count = ts->sdt_packet_period-1;
  434. av_log(s, AV_LOG_INFO,
  435. "muxrate %d bps, pcr every %d pkts, "
  436. "sdt every %d, pat/pmt every %d pkts\n",
  437. ts->mux_rate, service->pcr_packet_period,
  438. ts->sdt_packet_period, ts->pat_packet_period);
  439. put_flush_packet(s->pb);
  440. return 0;
  441. fail:
  442. for(i = 0;i < s->nb_streams; i++) {
  443. st = s->streams[i];
  444. av_free(st->priv_data);
  445. }
  446. return -1;
  447. }
  448. /* send SDT, PAT and PMT tables regulary */
  449. static void retransmit_si_info(AVFormatContext *s)
  450. {
  451. MpegTSWrite *ts = s->priv_data;
  452. int i;
  453. if (++ts->sdt_packet_count == ts->sdt_packet_period) {
  454. ts->sdt_packet_count = 0;
  455. mpegts_write_sdt(s);
  456. }
  457. if (++ts->pat_packet_count == ts->pat_packet_period) {
  458. ts->pat_packet_count = 0;
  459. mpegts_write_pat(s);
  460. for(i = 0; i < ts->nb_services; i++) {
  461. mpegts_write_pmt(s, ts->services[i]);
  462. }
  463. }
  464. }
  465. /* Write a single null transport stream packet */
  466. static void mpegts_insert_null_packet(AVFormatContext *s)
  467. {
  468. MpegTSWrite *ts = s->priv_data;
  469. uint8_t *q;
  470. uint8_t buf[TS_PACKET_SIZE];
  471. q = buf;
  472. *q++ = 0x47;
  473. *q++ = 0x00 | 0x1f;
  474. *q++ = 0xff;
  475. *q++ = 0x10;
  476. memset(q, 0x0FF, TS_PACKET_SIZE - (q - buf));
  477. put_buffer(s->pb, buf, TS_PACKET_SIZE);
  478. ts->cur_pcr += TS_PACKET_SIZE*8*90000LL/ts->mux_rate;
  479. }
  480. /* Write a single transport stream packet with a PCR and no payload */
  481. static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
  482. {
  483. MpegTSWrite *ts = s->priv_data;
  484. MpegTSWriteStream *ts_st = st->priv_data;
  485. uint8_t *q;
  486. uint64_t pcr = ts->cur_pcr;
  487. uint8_t buf[TS_PACKET_SIZE];
  488. q = buf;
  489. *q++ = 0x47;
  490. *q++ = ts_st->pid >> 8;
  491. *q++ = ts_st->pid;
  492. *q++ = 0x20 | ts_st->cc; /* Adaptation only */
  493. /* Continuity Count field does not increment (see 13818-1 section 2.4.3.3) */
  494. *q++ = TS_PACKET_SIZE - 5; /* Adaptation Field Length */
  495. *q++ = 0x10; /* Adaptation flags: PCR present */
  496. /* PCR coded into 6 bytes */
  497. *q++ = pcr >> 25;
  498. *q++ = pcr >> 17;
  499. *q++ = pcr >> 9;
  500. *q++ = pcr >> 1;
  501. *q++ = (pcr & 1) << 7;
  502. *q++ = 0;
  503. /* stuffing bytes */
  504. memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
  505. put_buffer(s->pb, buf, TS_PACKET_SIZE);
  506. ts->cur_pcr += TS_PACKET_SIZE*8*90000LL/ts->mux_rate;
  507. }
  508. static void write_pts(uint8_t *q, int fourbits, int64_t pts)
  509. {
  510. int val;
  511. val = fourbits << 4 | (((pts >> 30) & 0x07) << 1) | 1;
  512. *q++ = val;
  513. val = (((pts >> 15) & 0x7fff) << 1) | 1;
  514. *q++ = val >> 8;
  515. *q++ = val;
  516. val = (((pts) & 0x7fff) << 1) | 1;
  517. *q++ = val >> 8;
  518. *q++ = val;
  519. }
  520. /* Add a pes header to the front of payload, and segment into an integer number of
  521. * ts packets. The final ts packet is padded using an over-sized adaptation header
  522. * to exactly fill the last ts packet.
  523. * NOTE: 'payload' contains a complete PES payload.
  524. */
  525. static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
  526. const uint8_t *payload, int payload_size,
  527. int64_t pts, int64_t dts)
  528. {
  529. MpegTSWriteStream *ts_st = st->priv_data;
  530. MpegTSWrite *ts = s->priv_data;
  531. uint8_t buf[TS_PACKET_SIZE];
  532. uint8_t *q;
  533. int val, is_start, len, header_len, write_pcr, private_code, flags;
  534. int afc_len, stuffing_len;
  535. int64_t pcr = -1; /* avoid warning */
  536. int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
  537. is_start = 1;
  538. while (payload_size > 0) {
  539. retransmit_si_info(s);
  540. write_pcr = 0;
  541. if (ts_st->pid == ts_st->service->pcr_pid) {
  542. if (ts->mux_rate > 1 || is_start) // VBR pcr period is based on frames
  543. ts_st->service->pcr_packet_count++;
  544. if (ts_st->service->pcr_packet_count >=
  545. ts_st->service->pcr_packet_period) {
  546. ts_st->service->pcr_packet_count = 0;
  547. write_pcr = 1;
  548. }
  549. }
  550. if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE &&
  551. (dts - (int64_t)ts->cur_pcr) > delay) {
  552. /* pcr insert gets priority over null packet insert */
  553. if (write_pcr)
  554. mpegts_insert_pcr_only(s, st);
  555. else
  556. mpegts_insert_null_packet(s);
  557. continue; /* recalculate write_pcr and possibly retransmit si_info */
  558. }
  559. /* prepare packet header */
  560. q = buf;
  561. *q++ = 0x47;
  562. val = (ts_st->pid >> 8);
  563. if (is_start)
  564. val |= 0x40;
  565. *q++ = val;
  566. *q++ = ts_st->pid;
  567. ts_st->cc = (ts_st->cc + 1) & 0xf;
  568. *q++ = 0x10 | ts_st->cc | (write_pcr ? 0x20 : 0);
  569. if (write_pcr) {
  570. // add 11, pcr references the last byte of program clock reference base
  571. if (ts->mux_rate > 1)
  572. pcr = ts->cur_pcr + (4+7)*8*90000LL / ts->mux_rate;
  573. else
  574. pcr = dts - delay;
  575. if (dts != AV_NOPTS_VALUE && dts < pcr)
  576. av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
  577. *q++ = 7; /* AFC length */
  578. *q++ = 0x10; /* flags: PCR present */
  579. *q++ = pcr >> 25;
  580. *q++ = pcr >> 17;
  581. *q++ = pcr >> 9;
  582. *q++ = pcr >> 1;
  583. *q++ = (pcr & 1) << 7;
  584. *q++ = 0;
  585. }
  586. if (is_start) {
  587. int pes_extension = 0;
  588. /* write PES header */
  589. *q++ = 0x00;
  590. *q++ = 0x00;
  591. *q++ = 0x01;
  592. private_code = 0;
  593. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  594. if (st->codec->codec_id == CODEC_ID_DIRAC) {
  595. *q++ = 0xfd;
  596. } else
  597. *q++ = 0xe0;
  598. } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
  599. (st->codec->codec_id == CODEC_ID_MP2 ||
  600. st->codec->codec_id == CODEC_ID_MP3)) {
  601. *q++ = 0xc0;
  602. } else {
  603. *q++ = 0xbd;
  604. if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  605. private_code = 0x20;
  606. }
  607. }
  608. header_len = 0;
  609. flags = 0;
  610. if (pts != AV_NOPTS_VALUE) {
  611. header_len += 5;
  612. flags |= 0x80;
  613. }
  614. if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
  615. header_len += 5;
  616. flags |= 0x40;
  617. }
  618. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
  619. st->codec->codec_id == CODEC_ID_DIRAC) {
  620. /* set PES_extension_flag */
  621. pes_extension = 1;
  622. flags |= 0x01;
  623. /*
  624. * One byte for PES2 extension flag +
  625. * one byte for extension length +
  626. * one byte for extension id
  627. */
  628. header_len += 3;
  629. }
  630. len = payload_size + header_len + 3;
  631. if (private_code != 0)
  632. len++;
  633. if (len > 0xffff)
  634. len = 0;
  635. *q++ = len >> 8;
  636. *q++ = len;
  637. val = 0x80;
  638. /* data alignment indicator is required for subtitle data */
  639. if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE)
  640. val |= 0x04;
  641. *q++ = val;
  642. *q++ = flags;
  643. *q++ = header_len;
  644. if (pts != AV_NOPTS_VALUE) {
  645. write_pts(q, flags >> 6, pts);
  646. q += 5;
  647. }
  648. if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) {
  649. write_pts(q, 1, dts);
  650. q += 5;
  651. }
  652. if (pes_extension && st->codec->codec_id == CODEC_ID_DIRAC) {
  653. flags = 0x01; /* set PES_extension_flag_2 */
  654. *q++ = flags;
  655. *q++ = 0x80 | 0x01; /* marker bit + extension length */
  656. /*
  657. * Set the stream id extension flag bit to 0 and
  658. * write the extended stream id
  659. */
  660. *q++ = 0x00 | 0x60;
  661. }
  662. if (private_code != 0)
  663. *q++ = private_code;
  664. is_start = 0;
  665. }
  666. /* header size */
  667. header_len = q - buf;
  668. /* data len */
  669. len = TS_PACKET_SIZE - header_len;
  670. if (len > payload_size)
  671. len = payload_size;
  672. stuffing_len = TS_PACKET_SIZE - header_len - len;
  673. if (stuffing_len > 0) {
  674. /* add stuffing with AFC */
  675. if (buf[3] & 0x20) {
  676. /* stuffing already present: increase its size */
  677. afc_len = buf[4] + 1;
  678. memmove(buf + 4 + afc_len + stuffing_len,
  679. buf + 4 + afc_len,
  680. header_len - (4 + afc_len));
  681. buf[4] += stuffing_len;
  682. memset(buf + 4 + afc_len, 0xff, stuffing_len);
  683. } else {
  684. /* add stuffing */
  685. memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
  686. buf[3] |= 0x20;
  687. buf[4] = stuffing_len - 1;
  688. if (stuffing_len >= 2) {
  689. buf[5] = 0x00;
  690. memset(buf + 6, 0xff, stuffing_len - 2);
  691. }
  692. }
  693. }
  694. memcpy(buf + TS_PACKET_SIZE - len, payload, len);
  695. payload += len;
  696. payload_size -= len;
  697. put_buffer(s->pb, buf, TS_PACKET_SIZE);
  698. ts->cur_pcr += TS_PACKET_SIZE*8*90000LL/ts->mux_rate;
  699. }
  700. put_flush_packet(s->pb);
  701. }
  702. static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
  703. {
  704. AVStream *st = s->streams[pkt->stream_index];
  705. int size = pkt->size;
  706. uint8_t *buf= pkt->data;
  707. uint8_t *data= NULL;
  708. MpegTSWriteStream *ts_st = st->priv_data;
  709. const uint64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE)*2;
  710. int64_t dts = AV_NOPTS_VALUE, pts = AV_NOPTS_VALUE;
  711. if (pkt->pts != AV_NOPTS_VALUE)
  712. pts = pkt->pts + delay;
  713. if (pkt->dts != AV_NOPTS_VALUE)
  714. dts = pkt->dts + delay;
  715. if (ts_st->first_pts_check && pts == AV_NOPTS_VALUE) {
  716. av_log(s, AV_LOG_ERROR, "first pts value must set\n");
  717. return -1;
  718. }
  719. ts_st->first_pts_check = 0;
  720. if (st->codec->codec_id == CODEC_ID_H264) {
  721. const uint8_t *p = buf, *buf_end = p+size;
  722. uint32_t state = -1;
  723. if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001) {
  724. av_log(s, AV_LOG_ERROR, "h264 bitstream malformated, "
  725. "no startcode found, use -vbsf h264_mp4toannexb\n");
  726. return -1;
  727. }
  728. do {
  729. p = ff_find_start_code(p, buf_end, &state);
  730. //av_log(s, AV_LOG_INFO, "nal %d\n", state & 0x1f);
  731. } while (p < buf_end && (state & 0x1f) != 9 &&
  732. (state & 0x1f) != 5 && (state & 0x1f) != 1);
  733. if ((state & 0x1f) != 9) { // AUD NAL
  734. data = av_malloc(pkt->size+6);
  735. if (!data)
  736. return -1;
  737. memcpy(data+6, pkt->data, pkt->size);
  738. AV_WB32(data, 0x00000001);
  739. data[4] = 0x09;
  740. data[5] = 0xe0; // any slice type
  741. buf = data;
  742. size = pkt->size+6;
  743. }
  744. } else if (st->codec->codec_id == CODEC_ID_AAC) {
  745. if (pkt->size < 2)
  746. return -1;
  747. if ((AV_RB16(pkt->data) & 0xfff0) != 0xfff0) {
  748. ADTSContext *adts = ts_st->adts;
  749. int new_size;
  750. if (!adts) {
  751. av_log(s, AV_LOG_ERROR, "aac bitstream not in adts format "
  752. "and extradata missing\n");
  753. return -1;
  754. }
  755. new_size = ADTS_HEADER_SIZE+adts->pce_size+pkt->size;
  756. if ((unsigned)new_size >= INT_MAX)
  757. return -1;
  758. data = av_malloc(new_size);
  759. if (!data)
  760. return AVERROR(ENOMEM);
  761. ff_adts_write_frame_header(adts, data, pkt->size, adts->pce_size);
  762. if (adts->pce_size) {
  763. memcpy(data+ADTS_HEADER_SIZE, adts->pce_data, adts->pce_size);
  764. adts->pce_size = 0;
  765. }
  766. memcpy(data+ADTS_HEADER_SIZE+adts->pce_size, pkt->data, pkt->size);
  767. buf = data;
  768. size = new_size;
  769. }
  770. }
  771. if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO) {
  772. // for video and subtitle, write a single pes packet
  773. mpegts_write_pes(s, st, buf, size, pts, dts);
  774. av_free(data);
  775. return 0;
  776. }
  777. if (ts_st->payload_index + size > DEFAULT_PES_PAYLOAD_SIZE) {
  778. mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_index,
  779. ts_st->payload_pts, ts_st->payload_dts);
  780. ts_st->payload_index = 0;
  781. }
  782. if (!ts_st->payload_index) {
  783. ts_st->payload_pts = pts;
  784. ts_st->payload_dts = dts;
  785. }
  786. memcpy(ts_st->payload + ts_st->payload_index, buf, size);
  787. ts_st->payload_index += size;
  788. av_free(data);
  789. return 0;
  790. }
  791. static int mpegts_write_end(AVFormatContext *s)
  792. {
  793. MpegTSWrite *ts = s->priv_data;
  794. MpegTSWriteStream *ts_st;
  795. MpegTSService *service;
  796. AVStream *st;
  797. int i;
  798. /* flush current packets */
  799. for(i = 0; i < s->nb_streams; i++) {
  800. st = s->streams[i];
  801. ts_st = st->priv_data;
  802. if (ts_st->payload_index > 0) {
  803. mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_index,
  804. ts_st->payload_pts, ts_st->payload_dts);
  805. }
  806. av_freep(&ts_st->adts);
  807. }
  808. put_flush_packet(s->pb);
  809. for(i = 0; i < ts->nb_services; i++) {
  810. service = ts->services[i];
  811. av_freep(&service->provider_name);
  812. av_freep(&service->name);
  813. av_free(service);
  814. }
  815. av_free(ts->services);
  816. return 0;
  817. }
  818. AVOutputFormat mpegts_muxer = {
  819. "mpegts",
  820. NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
  821. "video/x-mpegts",
  822. "ts,m2t",
  823. sizeof(MpegTSWrite),
  824. CODEC_ID_MP2,
  825. CODEC_ID_MPEG2VIDEO,
  826. mpegts_write_header,
  827. mpegts_write_packet,
  828. mpegts_write_end,
  829. };