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.

734 lines
24KB

  1. /*
  2. * MPEG2 transport stream (aka DVB) mux
  3. * Copyright (c) 2003 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "avformat.h"
  20. #include "mpegts.h"
  21. /* write DVB SI sections */
  22. static const uint32_t crc_table[256] = {
  23. 0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b,
  24. 0x1a864db2, 0x1e475005, 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61,
  25. 0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd, 0x4c11db70, 0x48d0c6c7,
  26. 0x4593e01e, 0x4152fda9, 0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75,
  27. 0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011, 0x791d4014, 0x7ddc5da3,
  28. 0x709f7b7a, 0x745e66cd, 0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039,
  29. 0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5, 0xbe2b5b58, 0xbaea46ef,
  30. 0xb7a96036, 0xb3687d81, 0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d,
  31. 0xd4326d90, 0xd0f37027, 0xddb056fe, 0xd9714b49, 0xc7361b4c, 0xc3f706fb,
  32. 0xceb42022, 0xca753d95, 0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1,
  33. 0xe13ef6f4, 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d, 0x34867077, 0x30476dc0,
  34. 0x3d044b19, 0x39c556ae, 0x278206ab, 0x23431b1c, 0x2e003dc5, 0x2ac12072,
  35. 0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16, 0x018aeb13, 0x054bf6a4,
  36. 0x0808d07d, 0x0cc9cdca, 0x7897ab07, 0x7c56b6b0, 0x71159069, 0x75d48dde,
  37. 0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02, 0x5e9f46bf, 0x5a5e5b08,
  38. 0x571d7dd1, 0x53dc6066, 0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba,
  39. 0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e, 0xbfa1b04b, 0xbb60adfc,
  40. 0xb6238b25, 0xb2e29692, 0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6,
  41. 0x99a95df3, 0x9d684044, 0x902b669d, 0x94ea7b2a, 0xe0b41de7, 0xe4750050,
  42. 0xe9362689, 0xedf73b3e, 0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2,
  43. 0xc6bcf05f, 0xc27dede8, 0xcf3ecb31, 0xcbffd686, 0xd5b88683, 0xd1799b34,
  44. 0xdc3abded, 0xd8fba05a, 0x690ce0ee, 0x6dcdfd59, 0x608edb80, 0x644fc637,
  45. 0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb, 0x4f040d56, 0x4bc510e1,
  46. 0x46863638, 0x42472b8f, 0x5c007b8a, 0x58c1663d, 0x558240e4, 0x51435d53,
  47. 0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47, 0x36194d42, 0x32d850f5,
  48. 0x3f9b762c, 0x3b5a6b9b, 0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff,
  49. 0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623, 0xf12f560e, 0xf5ee4bb9,
  50. 0xf8ad6d60, 0xfc6c70d7, 0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b,
  51. 0xd727bbb6, 0xd3e6a601, 0xdea580d8, 0xda649d6f, 0xc423cd6a, 0xc0e2d0dd,
  52. 0xcda1f604, 0xc960ebb3, 0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7,
  53. 0xae3afba2, 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b, 0x9b3660c6, 0x9ff77d71,
  54. 0x92b45ba8, 0x9675461f, 0x8832161a, 0x8cf30bad, 0x81b02d74, 0x857130c3,
  55. 0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640, 0x4e8ee645, 0x4a4ffbf2,
  56. 0x470cdd2b, 0x43cdc09c, 0x7b827d21, 0x7f436096, 0x7200464f, 0x76c15bf8,
  57. 0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24, 0x119b4be9, 0x155a565e,
  58. 0x18197087, 0x1cd86d30, 0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec,
  59. 0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088, 0x2497d08d, 0x2056cd3a,
  60. 0x2d15ebe3, 0x29d4f654, 0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0,
  61. 0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb, 0xdbee767c, 0xe3a1cbc1, 0xe760d676,
  62. 0xea23f0af, 0xeee2ed18, 0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4,
  63. 0x89b8fd09, 0x8d79e0be, 0x803ac667, 0x84fbdbd0, 0x9abc8bd5, 0x9e7d9662,
  64. 0x933eb0bb, 0x97ffad0c, 0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668,
  65. 0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4
  66. };
  67. unsigned int mpegts_crc32(const uint8_t *data, int len)
  68. {
  69. register int i;
  70. unsigned int crc = 0xffffffff;
  71. for (i=0; i<len; i++)
  72. crc = (crc << 8) ^ crc_table[((crc >> 24) ^ *data++) & 0xff];
  73. return crc;
  74. }
  75. #ifdef CONFIG_MUXERS
  76. /*********************************************/
  77. /* mpegts section writer */
  78. typedef struct MpegTSSection {
  79. int pid;
  80. int cc;
  81. void (*write_packet)(struct MpegTSSection *s, const uint8_t *packet);
  82. void *opaque;
  83. } MpegTSSection;
  84. /* NOTE: 4 bytes must be left at the end for the crc32 */
  85. void mpegts_write_section(MpegTSSection *s, uint8_t *buf, int len)
  86. {
  87. unsigned int crc;
  88. unsigned char packet[TS_PACKET_SIZE];
  89. const unsigned char *buf_ptr;
  90. unsigned char *q;
  91. int first, b, len1, left;
  92. crc = mpegts_crc32(buf, len - 4);
  93. buf[len - 4] = (crc >> 24) & 0xff;
  94. buf[len - 3] = (crc >> 16) & 0xff;
  95. buf[len - 2] = (crc >> 8) & 0xff;
  96. buf[len - 1] = (crc) & 0xff;
  97. /* send each packet */
  98. buf_ptr = buf;
  99. while (len > 0) {
  100. first = (buf == buf_ptr);
  101. q = packet;
  102. *q++ = 0x47;
  103. b = (s->pid >> 8);
  104. if (first)
  105. b |= 0x40;
  106. *q++ = b;
  107. *q++ = s->pid;
  108. s->cc = (s->cc + 1) & 0xf;
  109. *q++ = 0x10 | s->cc;
  110. if (first)
  111. *q++ = 0; /* 0 offset */
  112. len1 = TS_PACKET_SIZE - (q - packet);
  113. if (len1 > len)
  114. len1 = len;
  115. memcpy(q, buf_ptr, len1);
  116. q += len1;
  117. /* add known padding data */
  118. left = TS_PACKET_SIZE - (q - packet);
  119. if (left > 0)
  120. memset(q, 0xff, left);
  121. s->write_packet(s, packet);
  122. buf_ptr += len1;
  123. len -= len1;
  124. }
  125. }
  126. static inline void put16(uint8_t **q_ptr, int val)
  127. {
  128. uint8_t *q;
  129. q = *q_ptr;
  130. *q++ = val >> 8;
  131. *q++ = val;
  132. *q_ptr = q;
  133. }
  134. int mpegts_write_section1(MpegTSSection *s, int tid, int id,
  135. int version, int sec_num, int last_sec_num,
  136. uint8_t *buf, int len)
  137. {
  138. uint8_t section[1024], *q;
  139. unsigned int tot_len;
  140. tot_len = 3 + 5 + len + 4;
  141. /* check if not too big */
  142. if (tot_len > 1024)
  143. return -1;
  144. q = section;
  145. *q++ = tid;
  146. put16(&q, 0xb000 | (len + 5 + 4)); /* 5 byte header + 4 byte CRC */
  147. put16(&q, id);
  148. *q++ = 0xc1 | (version << 1); /* current_next_indicator = 1 */
  149. *q++ = sec_num;
  150. *q++ = last_sec_num;
  151. memcpy(q, buf, len);
  152. mpegts_write_section(s, section, tot_len);
  153. return 0;
  154. }
  155. /*********************************************/
  156. /* mpegts writer */
  157. #define DEFAULT_PMT_START_PID 0x1000
  158. #define DEFAULT_START_PID 0x0100
  159. #define DEFAULT_PROVIDER_NAME "FFmpeg"
  160. #define DEFAULT_SERVICE_NAME "Service01"
  161. /* default network id, transport stream and service identifiers */
  162. #define DEFAULT_ONID 0x0001
  163. #define DEFAULT_TSID 0x0001
  164. #define DEFAULT_SID 0x0001
  165. /* a PES packet header is generated every DEFAULT_PES_HEADER_FREQ packets */
  166. #define DEFAULT_PES_HEADER_FREQ 16
  167. #define DEFAULT_PES_PAYLOAD_SIZE ((DEFAULT_PES_HEADER_FREQ - 1) * 184 + 170)
  168. /* we retransmit the SI info at this rate */
  169. #define SDT_RETRANS_TIME 500
  170. #define PAT_RETRANS_TIME 100
  171. #define PCR_RETRANS_TIME 20
  172. typedef struct MpegTSWriteStream {
  173. struct MpegTSService *service;
  174. int pid; /* stream associated pid */
  175. int cc;
  176. int payload_index;
  177. int64_t payload_pts;
  178. uint8_t payload[DEFAULT_PES_PAYLOAD_SIZE];
  179. } MpegTSWriteStream;
  180. typedef struct MpegTSService {
  181. MpegTSSection pmt; /* MPEG2 pmt table context */
  182. int sid; /* service ID */
  183. char *name;
  184. char *provider_name;
  185. int pcr_pid;
  186. int pcr_packet_count;
  187. int pcr_packet_freq;
  188. } MpegTSService;
  189. typedef struct MpegTSWrite {
  190. MpegTSSection pat; /* MPEG2 pat table */
  191. MpegTSSection sdt; /* MPEG2 sdt table context */
  192. MpegTSService **services;
  193. int sdt_packet_count;
  194. int sdt_packet_freq;
  195. int pat_packet_count;
  196. int pat_packet_freq;
  197. int nb_services;
  198. int onid;
  199. int tsid;
  200. } MpegTSWrite;
  201. static void mpegts_write_pat(AVFormatContext *s)
  202. {
  203. MpegTSWrite *ts = s->priv_data;
  204. MpegTSService *service;
  205. uint8_t data[1012], *q;
  206. int i;
  207. q = data;
  208. for(i = 0; i < ts->nb_services; i++) {
  209. service = ts->services[i];
  210. put16(&q, service->sid);
  211. put16(&q, 0xe000 | service->pmt.pid);
  212. }
  213. mpegts_write_section1(&ts->pat, PAT_TID, ts->tsid, 0, 0, 0,
  214. data, q - data);
  215. }
  216. static void mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
  217. {
  218. // MpegTSWrite *ts = s->priv_data;
  219. uint8_t data[1012], *q, *desc_length_ptr, *program_info_length_ptr;
  220. int val, stream_type, i;
  221. q = data;
  222. put16(&q, 0xe000 | service->pcr_pid);
  223. program_info_length_ptr = q;
  224. q += 2; /* patched after */
  225. /* put program info here */
  226. val = 0xf000 | (q - program_info_length_ptr - 2);
  227. program_info_length_ptr[0] = val >> 8;
  228. program_info_length_ptr[1] = val;
  229. for(i = 0; i < s->nb_streams; i++) {
  230. AVStream *st = s->streams[i];
  231. MpegTSWriteStream *ts_st = st->priv_data;
  232. switch(st->codec->codec_id) {
  233. case CODEC_ID_MPEG1VIDEO:
  234. case CODEC_ID_MPEG2VIDEO:
  235. stream_type = STREAM_TYPE_VIDEO_MPEG2;
  236. break;
  237. case CODEC_ID_MPEG4:
  238. stream_type = STREAM_TYPE_VIDEO_MPEG4;
  239. break;
  240. case CODEC_ID_H264:
  241. stream_type = STREAM_TYPE_VIDEO_H264;
  242. break;
  243. case CODEC_ID_MP2:
  244. case CODEC_ID_MP3:
  245. stream_type = STREAM_TYPE_AUDIO_MPEG1;
  246. break;
  247. case CODEC_ID_AAC:
  248. stream_type = STREAM_TYPE_AUDIO_AAC;
  249. break;
  250. case CODEC_ID_AC3:
  251. stream_type = STREAM_TYPE_AUDIO_AC3;
  252. break;
  253. default:
  254. stream_type = STREAM_TYPE_PRIVATE_DATA;
  255. break;
  256. }
  257. *q++ = stream_type;
  258. put16(&q, 0xe000 | ts_st->pid);
  259. desc_length_ptr = q;
  260. q += 2; /* patched after */
  261. /* write optional descriptors here */
  262. switch(st->codec->codec_type) {
  263. case CODEC_TYPE_AUDIO:
  264. if (strlen(st->language) == 3) {
  265. *q++ = 0x0a; /* ISO 639 language descriptor */
  266. *q++ = 4;
  267. *q++ = st->language[0];
  268. *q++ = st->language[1];
  269. *q++ = st->language[2];
  270. *q++ = 0; /* undefined type */
  271. }
  272. break;
  273. case CODEC_TYPE_SUBTITLE:
  274. {
  275. const char *language;
  276. language = st->language;
  277. if (strlen(language) != 3)
  278. language = "eng";
  279. *q++ = 0x59;
  280. *q++ = 8;
  281. *q++ = language[0];
  282. *q++ = language[1];
  283. *q++ = language[2];
  284. *q++ = 0x10; /* normal subtitles (0x20 = if hearing pb) */
  285. put16(&q, 1); /* page id */
  286. put16(&q, 1); /* ancillary page id */
  287. }
  288. break;
  289. }
  290. val = 0xf000 | (q - desc_length_ptr - 2);
  291. desc_length_ptr[0] = val >> 8;
  292. desc_length_ptr[1] = val;
  293. }
  294. mpegts_write_section1(&service->pmt, PMT_TID, service->sid, 0, 0, 0,
  295. data, q - data);
  296. }
  297. /* NOTE: str == NULL is accepted for an empty string */
  298. static void putstr8(uint8_t **q_ptr, const char *str)
  299. {
  300. uint8_t *q;
  301. int len;
  302. q = *q_ptr;
  303. if (!str)
  304. len = 0;
  305. else
  306. len = strlen(str);
  307. *q++ = len;
  308. memcpy(q, str, len);
  309. q += len;
  310. *q_ptr = q;
  311. }
  312. static void mpegts_write_sdt(AVFormatContext *s)
  313. {
  314. MpegTSWrite *ts = s->priv_data;
  315. MpegTSService *service;
  316. uint8_t data[1012], *q, *desc_list_len_ptr, *desc_len_ptr;
  317. int i, running_status, free_ca_mode, val;
  318. q = data;
  319. put16(&q, ts->onid);
  320. *q++ = 0xff;
  321. for(i = 0; i < ts->nb_services; i++) {
  322. service = ts->services[i];
  323. put16(&q, service->sid);
  324. *q++ = 0xfc | 0x00; /* currently no EIT info */
  325. desc_list_len_ptr = q;
  326. q += 2;
  327. running_status = 4; /* running */
  328. free_ca_mode = 0;
  329. /* write only one descriptor for the service name and provider */
  330. *q++ = 0x48;
  331. desc_len_ptr = q;
  332. q++;
  333. *q++ = 0x01; /* digital television service */
  334. putstr8(&q, service->provider_name);
  335. putstr8(&q, service->name);
  336. desc_len_ptr[0] = q - desc_len_ptr - 1;
  337. /* fill descriptor length */
  338. val = (running_status << 13) | (free_ca_mode << 12) |
  339. (q - desc_list_len_ptr - 2);
  340. desc_list_len_ptr[0] = val >> 8;
  341. desc_list_len_ptr[1] = val;
  342. }
  343. mpegts_write_section1(&ts->sdt, SDT_TID, ts->tsid, 0, 0, 0,
  344. data, q - data);
  345. }
  346. static MpegTSService *mpegts_add_service(MpegTSWrite *ts,
  347. int sid,
  348. const char *provider_name,
  349. const char *name)
  350. {
  351. MpegTSService *service;
  352. service = av_mallocz(sizeof(MpegTSService));
  353. if (!service)
  354. return NULL;
  355. service->pmt.pid = DEFAULT_PMT_START_PID + ts->nb_services - 1;
  356. service->sid = sid;
  357. service->provider_name = av_strdup(provider_name);
  358. service->name = av_strdup(name);
  359. service->pcr_pid = 0x1fff;
  360. dynarray_add(&ts->services, &ts->nb_services, service);
  361. return service;
  362. }
  363. static void section_write_packet(MpegTSSection *s, const uint8_t *packet)
  364. {
  365. AVFormatContext *ctx = s->opaque;
  366. put_buffer(&ctx->pb, packet, TS_PACKET_SIZE);
  367. }
  368. static int mpegts_write_header(AVFormatContext *s)
  369. {
  370. MpegTSWrite *ts = s->priv_data;
  371. MpegTSWriteStream *ts_st;
  372. MpegTSService *service;
  373. AVStream *st;
  374. int i, total_bit_rate;
  375. const char *service_name;
  376. ts->tsid = DEFAULT_TSID;
  377. ts->onid = DEFAULT_ONID;
  378. /* allocate a single DVB service */
  379. service_name = s->title;
  380. if (service_name[0] == '\0')
  381. service_name = DEFAULT_SERVICE_NAME;
  382. service = mpegts_add_service(ts, DEFAULT_SID,
  383. DEFAULT_PROVIDER_NAME, service_name);
  384. service->pmt.write_packet = section_write_packet;
  385. service->pmt.opaque = s;
  386. ts->pat.pid = PAT_PID;
  387. ts->pat.cc = 0;
  388. ts->pat.write_packet = section_write_packet;
  389. ts->pat.opaque = s;
  390. ts->sdt.pid = SDT_PID;
  391. ts->sdt.cc = 0;
  392. ts->sdt.write_packet = section_write_packet;
  393. ts->sdt.opaque = s;
  394. /* assign pids to each stream */
  395. total_bit_rate = 0;
  396. for(i = 0;i < s->nb_streams; i++) {
  397. st = s->streams[i];
  398. ts_st = av_mallocz(sizeof(MpegTSWriteStream));
  399. if (!ts_st)
  400. goto fail;
  401. st->priv_data = ts_st;
  402. ts_st->service = service;
  403. ts_st->pid = DEFAULT_START_PID + i;
  404. ts_st->payload_pts = AV_NOPTS_VALUE;
  405. /* update PCR pid by using the first video stream */
  406. if (st->codec->codec_type == CODEC_TYPE_VIDEO &&
  407. service->pcr_pid == 0x1fff)
  408. service->pcr_pid = ts_st->pid;
  409. total_bit_rate += st->codec->bit_rate;
  410. }
  411. /* if no video stream, use the first stream as PCR */
  412. if (service->pcr_pid == 0x1fff && s->nb_streams > 0) {
  413. ts_st = s->streams[0]->priv_data;
  414. service->pcr_pid = ts_st->pid;
  415. }
  416. if (total_bit_rate <= 8 * 1024)
  417. total_bit_rate = 8 * 1024;
  418. service->pcr_packet_freq = (total_bit_rate * PCR_RETRANS_TIME) /
  419. (TS_PACKET_SIZE * 8 * 1000);
  420. ts->sdt_packet_freq = (total_bit_rate * SDT_RETRANS_TIME) /
  421. (TS_PACKET_SIZE * 8 * 1000);
  422. ts->pat_packet_freq = (total_bit_rate * PAT_RETRANS_TIME) /
  423. (TS_PACKET_SIZE * 8 * 1000);
  424. #if 0
  425. printf("%d %d %d\n",
  426. total_bit_rate, ts->sdt_packet_freq, ts->pat_packet_freq);
  427. #endif
  428. /* write info at the start of the file, so that it will be fast to
  429. find them */
  430. mpegts_write_sdt(s);
  431. mpegts_write_pat(s);
  432. for(i = 0; i < ts->nb_services; i++) {
  433. mpegts_write_pmt(s, ts->services[i]);
  434. }
  435. put_flush_packet(&s->pb);
  436. return 0;
  437. fail:
  438. for(i = 0;i < s->nb_streams; i++) {
  439. st = s->streams[i];
  440. av_free(st->priv_data);
  441. }
  442. return -1;
  443. }
  444. /* send SDT, PAT and PMT tables regulary */
  445. static void retransmit_si_info(AVFormatContext *s)
  446. {
  447. MpegTSWrite *ts = s->priv_data;
  448. int i;
  449. if (++ts->sdt_packet_count == ts->sdt_packet_freq) {
  450. ts->sdt_packet_count = 0;
  451. mpegts_write_sdt(s);
  452. }
  453. if (++ts->pat_packet_count == ts->pat_packet_freq) {
  454. ts->pat_packet_count = 0;
  455. mpegts_write_pat(s);
  456. for(i = 0; i < ts->nb_services; i++) {
  457. mpegts_write_pmt(s, ts->services[i]);
  458. }
  459. }
  460. }
  461. /* NOTE: pes_data contains all the PES packet */
  462. static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
  463. const uint8_t *payload, int payload_size,
  464. int64_t pts)
  465. {
  466. MpegTSWriteStream *ts_st = st->priv_data;
  467. uint8_t buf[TS_PACKET_SIZE];
  468. uint8_t *q;
  469. int val, is_start, len, header_len, write_pcr, private_code;
  470. int afc_len, stuffing_len;
  471. int64_t pcr = -1; /* avoid warning */
  472. is_start = 1;
  473. while (payload_size > 0) {
  474. retransmit_si_info(s);
  475. write_pcr = 0;
  476. if (ts_st->pid == ts_st->service->pcr_pid) {
  477. ts_st->service->pcr_packet_count++;
  478. if (ts_st->service->pcr_packet_count >=
  479. ts_st->service->pcr_packet_freq) {
  480. ts_st->service->pcr_packet_count = 0;
  481. write_pcr = 1;
  482. /* XXX: this is incorrect, but at least we have a PCR
  483. value */
  484. pcr = pts;
  485. }
  486. }
  487. /* prepare packet header */
  488. q = buf;
  489. *q++ = 0x47;
  490. val = (ts_st->pid >> 8);
  491. if (is_start)
  492. val |= 0x40;
  493. *q++ = val;
  494. *q++ = ts_st->pid;
  495. *q++ = 0x10 | ts_st->cc | (write_pcr ? 0x20 : 0);
  496. ts_st->cc = (ts_st->cc + 1) & 0xf;
  497. if (write_pcr) {
  498. *q++ = 7; /* AFC length */
  499. *q++ = 0x10; /* flags: PCR present */
  500. *q++ = pcr >> 25;
  501. *q++ = pcr >> 17;
  502. *q++ = pcr >> 9;
  503. *q++ = pcr >> 1;
  504. *q++ = (pcr & 1) << 7;
  505. *q++ = 0;
  506. }
  507. if (is_start) {
  508. /* write PES header */
  509. *q++ = 0x00;
  510. *q++ = 0x00;
  511. *q++ = 0x01;
  512. private_code = 0;
  513. if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
  514. *q++ = 0xe0;
  515. } else if (st->codec->codec_type == CODEC_TYPE_AUDIO &&
  516. (st->codec->codec_id == CODEC_ID_MP2 ||
  517. st->codec->codec_id == CODEC_ID_MP3)) {
  518. *q++ = 0xc0;
  519. } else {
  520. *q++ = 0xbd;
  521. if (st->codec->codec_type == CODEC_TYPE_SUBTITLE) {
  522. private_code = 0x20;
  523. }
  524. }
  525. if (pts != AV_NOPTS_VALUE)
  526. header_len = 8;
  527. else
  528. header_len = 3;
  529. if (private_code != 0)
  530. header_len++;
  531. len = payload_size + header_len;
  532. *q++ = len >> 8;
  533. *q++ = len;
  534. val = 0x80;
  535. /* data alignment indicator is required for subtitle data */
  536. if (st->codec->codec_type == CODEC_TYPE_SUBTITLE)
  537. val |= 0x04;
  538. *q++ = val;
  539. if (pts != AV_NOPTS_VALUE) {
  540. *q++ = 0x80; /* PTS only */
  541. *q++ = 0x05; /* header len */
  542. val = (0x02 << 4) |
  543. (((pts >> 30) & 0x07) << 1) | 1;
  544. *q++ = val;
  545. val = (((pts >> 15) & 0x7fff) << 1) | 1;
  546. *q++ = val >> 8;
  547. *q++ = val;
  548. val = (((pts) & 0x7fff) << 1) | 1;
  549. *q++ = val >> 8;
  550. *q++ = val;
  551. } else {
  552. *q++ = 0x00;
  553. *q++ = 0x00;
  554. }
  555. if (private_code != 0)
  556. *q++ = private_code;
  557. is_start = 0;
  558. }
  559. /* header size */
  560. header_len = q - buf;
  561. /* data len */
  562. len = TS_PACKET_SIZE - header_len;
  563. if (len > payload_size)
  564. len = payload_size;
  565. stuffing_len = TS_PACKET_SIZE - header_len - len;
  566. if (stuffing_len > 0) {
  567. /* add stuffing with AFC */
  568. if (buf[3] & 0x20) {
  569. /* stuffing already present: increase its size */
  570. afc_len = buf[4] + 1;
  571. memmove(buf + 4 + afc_len + stuffing_len,
  572. buf + 4 + afc_len,
  573. header_len - (4 + afc_len));
  574. buf[4] += stuffing_len;
  575. memset(buf + 4 + afc_len, 0xff, stuffing_len);
  576. } else {
  577. /* add stuffing */
  578. memmove(buf + 4 + stuffing_len, buf + 4, header_len - 4);
  579. buf[3] |= 0x20;
  580. buf[4] = stuffing_len - 1;
  581. if (stuffing_len >= 2) {
  582. buf[5] = 0x00;
  583. memset(buf + 6, 0xff, stuffing_len - 2);
  584. }
  585. }
  586. }
  587. memcpy(buf + TS_PACKET_SIZE - len, payload, len);
  588. payload += len;
  589. payload_size -= len;
  590. put_buffer(&s->pb, buf, TS_PACKET_SIZE);
  591. }
  592. put_flush_packet(&s->pb);
  593. }
  594. static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
  595. {
  596. AVStream *st = s->streams[pkt->stream_index];
  597. int size= pkt->size;
  598. uint8_t *buf= pkt->data;
  599. MpegTSWriteStream *ts_st = st->priv_data;
  600. int len, max_payload_size;
  601. if (st->codec->codec_type == CODEC_TYPE_SUBTITLE) {
  602. /* for subtitle, a single PES packet must be generated */
  603. mpegts_write_pes(s, st, buf, size, pkt->pts);
  604. return 0;
  605. }
  606. max_payload_size = DEFAULT_PES_PAYLOAD_SIZE;
  607. while (size > 0) {
  608. len = max_payload_size - ts_st->payload_index;
  609. if (len > size)
  610. len = size;
  611. memcpy(ts_st->payload + ts_st->payload_index, buf, len);
  612. buf += len;
  613. size -= len;
  614. ts_st->payload_index += len;
  615. if (ts_st->payload_pts == AV_NOPTS_VALUE)
  616. ts_st->payload_pts = pkt->pts;
  617. if (ts_st->payload_index >= max_payload_size) {
  618. mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_index,
  619. ts_st->payload_pts);
  620. ts_st->payload_pts = AV_NOPTS_VALUE;
  621. ts_st->payload_index = 0;
  622. }
  623. }
  624. return 0;
  625. }
  626. static int mpegts_write_end(AVFormatContext *s)
  627. {
  628. MpegTSWrite *ts = s->priv_data;
  629. MpegTSWriteStream *ts_st;
  630. MpegTSService *service;
  631. AVStream *st;
  632. int i;
  633. /* flush current packets */
  634. for(i = 0; i < s->nb_streams; i++) {
  635. st = s->streams[i];
  636. ts_st = st->priv_data;
  637. if (ts_st->payload_index > 0) {
  638. mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_index,
  639. ts_st->payload_pts);
  640. }
  641. }
  642. put_flush_packet(&s->pb);
  643. for(i = 0; i < ts->nb_services; i++) {
  644. service = ts->services[i];
  645. av_freep(&service->provider_name);
  646. av_freep(&service->name);
  647. av_free(service);
  648. }
  649. av_free(ts->services);
  650. return 0;
  651. }
  652. AVOutputFormat mpegts_mux = {
  653. "mpegts",
  654. "MPEG2 transport stream format",
  655. "video/x-mpegts",
  656. "ts",
  657. sizeof(MpegTSWrite),
  658. CODEC_ID_MP2,
  659. CODEC_ID_MPEG2VIDEO,
  660. mpegts_write_header,
  661. mpegts_write_packet,
  662. mpegts_write_end,
  663. };
  664. #endif // CONFIG_MUXERS