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.

1855 lines
58KB

  1. /*
  2. * MPEG1/2 muxer and demuxer
  3. * Copyright (c) 2000, 2001, 2002 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 "avformat.h"
  22. #include "bitstream.h"
  23. #include "fifo.h"
  24. #define MAX_PAYLOAD_SIZE 4096
  25. //#define DEBUG_SEEK
  26. #undef NDEBUG
  27. #include <assert.h>
  28. typedef struct PacketDesc {
  29. int64_t pts;
  30. int64_t dts;
  31. int size;
  32. int unwritten_size;
  33. int flags;
  34. struct PacketDesc *next;
  35. } PacketDesc;
  36. typedef struct {
  37. AVFifoBuffer fifo;
  38. uint8_t id;
  39. int max_buffer_size; /* in bytes */
  40. int buffer_index;
  41. PacketDesc *predecode_packet;
  42. PacketDesc *premux_packet;
  43. PacketDesc **next_packet;
  44. int packet_number;
  45. uint8_t lpcm_header[3];
  46. int lpcm_align;
  47. int bytes_to_iframe;
  48. int align_iframe;
  49. int64_t vobu_start_pts;
  50. } StreamInfo;
  51. typedef struct {
  52. int packet_size; /* required packet size */
  53. int packet_number;
  54. int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */
  55. int system_header_freq;
  56. int system_header_size;
  57. int mux_rate; /* bitrate in units of 50 bytes/s */
  58. /* stream info */
  59. int audio_bound;
  60. int video_bound;
  61. int is_mpeg2;
  62. int is_vcd;
  63. int is_svcd;
  64. int is_dvd;
  65. int64_t last_scr; /* current system clock */
  66. double vcd_padding_bitrate; //FIXME floats
  67. int64_t vcd_padding_bytes_written;
  68. } MpegMuxContext;
  69. #define PACK_START_CODE ((unsigned int)0x000001ba)
  70. #define SYSTEM_HEADER_START_CODE ((unsigned int)0x000001bb)
  71. #define SEQUENCE_END_CODE ((unsigned int)0x000001b7)
  72. #define PACKET_START_CODE_MASK ((unsigned int)0xffffff00)
  73. #define PACKET_START_CODE_PREFIX ((unsigned int)0x00000100)
  74. #define ISO_11172_END_CODE ((unsigned int)0x000001b9)
  75. /* mpeg2 */
  76. #define PROGRAM_STREAM_MAP 0x1bc
  77. #define PRIVATE_STREAM_1 0x1bd
  78. #define PADDING_STREAM 0x1be
  79. #define PRIVATE_STREAM_2 0x1bf
  80. #define AUDIO_ID 0xc0
  81. #define VIDEO_ID 0xe0
  82. #define AC3_ID 0x80
  83. #define DTS_ID 0x8a
  84. #define LPCM_ID 0xa0
  85. #define SUB_ID 0x20
  86. #define STREAM_TYPE_VIDEO_MPEG1 0x01
  87. #define STREAM_TYPE_VIDEO_MPEG2 0x02
  88. #define STREAM_TYPE_AUDIO_MPEG1 0x03
  89. #define STREAM_TYPE_AUDIO_MPEG2 0x04
  90. #define STREAM_TYPE_PRIVATE_SECTION 0x05
  91. #define STREAM_TYPE_PRIVATE_DATA 0x06
  92. #define STREAM_TYPE_AUDIO_AAC 0x0f
  93. #define STREAM_TYPE_VIDEO_MPEG4 0x10
  94. #define STREAM_TYPE_VIDEO_H264 0x1b
  95. #define STREAM_TYPE_AUDIO_AC3 0x81
  96. #define STREAM_TYPE_AUDIO_DTS 0x8a
  97. static const int lpcm_freq_tab[4] = { 48000, 96000, 44100, 32000 };
  98. #ifdef CONFIG_MUXERS
  99. AVOutputFormat mpeg1system_muxer;
  100. AVOutputFormat mpeg1vcd_muxer;
  101. AVOutputFormat mpeg2vob_muxer;
  102. AVOutputFormat mpeg2svcd_muxer;
  103. AVOutputFormat mpeg2dvd_muxer;
  104. static int put_pack_header(AVFormatContext *ctx,
  105. uint8_t *buf, int64_t timestamp)
  106. {
  107. MpegMuxContext *s = ctx->priv_data;
  108. PutBitContext pb;
  109. init_put_bits(&pb, buf, 128);
  110. put_bits(&pb, 32, PACK_START_CODE);
  111. if (s->is_mpeg2) {
  112. put_bits(&pb, 2, 0x1);
  113. } else {
  114. put_bits(&pb, 4, 0x2);
  115. }
  116. put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
  117. put_bits(&pb, 1, 1);
  118. put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
  119. put_bits(&pb, 1, 1);
  120. put_bits(&pb, 15, (uint32_t)((timestamp) & 0x7fff));
  121. put_bits(&pb, 1, 1);
  122. if (s->is_mpeg2) {
  123. /* clock extension */
  124. put_bits(&pb, 9, 0);
  125. }
  126. put_bits(&pb, 1, 1);
  127. put_bits(&pb, 22, s->mux_rate);
  128. put_bits(&pb, 1, 1);
  129. if (s->is_mpeg2) {
  130. put_bits(&pb, 1, 1);
  131. put_bits(&pb, 5, 0x1f); /* reserved */
  132. put_bits(&pb, 3, 0); /* stuffing length */
  133. }
  134. flush_put_bits(&pb);
  135. return pbBufPtr(&pb) - pb.buf;
  136. }
  137. static int put_system_header(AVFormatContext *ctx, uint8_t *buf,int only_for_stream_id)
  138. {
  139. MpegMuxContext *s = ctx->priv_data;
  140. int size, i, private_stream_coded, id;
  141. PutBitContext pb;
  142. init_put_bits(&pb, buf, 128);
  143. put_bits(&pb, 32, SYSTEM_HEADER_START_CODE);
  144. put_bits(&pb, 16, 0);
  145. put_bits(&pb, 1, 1);
  146. put_bits(&pb, 22, s->mux_rate); /* maximum bit rate of the multiplexed stream */
  147. put_bits(&pb, 1, 1); /* marker */
  148. if (s->is_vcd && only_for_stream_id==VIDEO_ID) {
  149. /* This header applies only to the video stream (see VCD standard p. IV-7)*/
  150. put_bits(&pb, 6, 0);
  151. } else
  152. put_bits(&pb, 6, s->audio_bound);
  153. if (s->is_vcd) {
  154. /* see VCD standard, p. IV-7*/
  155. put_bits(&pb, 1, 0);
  156. put_bits(&pb, 1, 1);
  157. } else {
  158. put_bits(&pb, 1, 0); /* variable bitrate*/
  159. put_bits(&pb, 1, 0); /* non constrainted bit stream */
  160. }
  161. if (s->is_vcd || s->is_dvd) {
  162. /* see VCD standard p IV-7 */
  163. put_bits(&pb, 1, 1); /* audio locked */
  164. put_bits(&pb, 1, 1); /* video locked */
  165. } else {
  166. put_bits(&pb, 1, 0); /* audio locked */
  167. put_bits(&pb, 1, 0); /* video locked */
  168. }
  169. put_bits(&pb, 1, 1); /* marker */
  170. if (s->is_vcd && only_for_stream_id==AUDIO_ID) {
  171. /* This header applies only to the audio stream (see VCD standard p. IV-7)*/
  172. put_bits(&pb, 5, 0);
  173. } else
  174. put_bits(&pb, 5, s->video_bound);
  175. if (s->is_dvd) {
  176. put_bits(&pb, 1, 0); /* packet_rate_restriction_flag */
  177. put_bits(&pb, 7, 0x7f); /* reserved byte */
  178. } else
  179. put_bits(&pb, 8, 0xff); /* reserved byte */
  180. /* DVD-Video Stream_bound entries
  181. id (0xB9) video, maximum P-STD for stream 0xE0. (P-STD_buffer_bound_scale = 1)
  182. id (0xB8) audio, maximum P-STD for any MPEG audio (0xC0 to 0xC7) streams. If there are none set to 4096 (32x128). (P-STD_buffer_bound_scale = 0)
  183. id (0xBD) private stream 1 (audio other than MPEG and subpictures). (P-STD_buffer_bound_scale = 1)
  184. id (0xBF) private stream 2, NAV packs, set to 2x1024. */
  185. if (s->is_dvd) {
  186. int P_STD_max_video = 0;
  187. int P_STD_max_mpeg_audio = 0;
  188. int P_STD_max_mpeg_PS1 = 0;
  189. for(i=0;i<ctx->nb_streams;i++) {
  190. StreamInfo *stream = ctx->streams[i]->priv_data;
  191. id = stream->id;
  192. if (id == 0xbd && stream->max_buffer_size > P_STD_max_mpeg_PS1) {
  193. P_STD_max_mpeg_PS1 = stream->max_buffer_size;
  194. } else if (id >= 0xc0 && id <= 0xc7 && stream->max_buffer_size > P_STD_max_mpeg_audio) {
  195. P_STD_max_mpeg_audio = stream->max_buffer_size;
  196. } else if (id == 0xe0 && stream->max_buffer_size > P_STD_max_video) {
  197. P_STD_max_video = stream->max_buffer_size;
  198. }
  199. }
  200. /* video */
  201. put_bits(&pb, 8, 0xb9); /* stream ID */
  202. put_bits(&pb, 2, 3);
  203. put_bits(&pb, 1, 1);
  204. put_bits(&pb, 13, P_STD_max_video / 1024);
  205. /* audio */
  206. if (P_STD_max_mpeg_audio == 0)
  207. P_STD_max_mpeg_audio = 4096;
  208. put_bits(&pb, 8, 0xb8); /* stream ID */
  209. put_bits(&pb, 2, 3);
  210. put_bits(&pb, 1, 0);
  211. put_bits(&pb, 13, P_STD_max_mpeg_audio / 128);
  212. /* private stream 1 */
  213. put_bits(&pb, 8, 0xbd); /* stream ID */
  214. put_bits(&pb, 2, 3);
  215. put_bits(&pb, 1, 0);
  216. put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128);
  217. /* private stream 2 */
  218. put_bits(&pb, 8, 0xbf); /* stream ID */
  219. put_bits(&pb, 2, 3);
  220. put_bits(&pb, 1, 1);
  221. put_bits(&pb, 13, 2);
  222. }
  223. else {
  224. /* audio stream info */
  225. private_stream_coded = 0;
  226. for(i=0;i<ctx->nb_streams;i++) {
  227. StreamInfo *stream = ctx->streams[i]->priv_data;
  228. /* For VCDs, only include the stream info for the stream
  229. that the pack which contains this system belongs to.
  230. (see VCD standard p. IV-7) */
  231. if ( !s->is_vcd || stream->id==only_for_stream_id
  232. || only_for_stream_id==0) {
  233. id = stream->id;
  234. if (id < 0xc0) {
  235. /* special case for private streams (AC3 use that) */
  236. if (private_stream_coded)
  237. continue;
  238. private_stream_coded = 1;
  239. id = 0xbd;
  240. }
  241. put_bits(&pb, 8, id); /* stream ID */
  242. put_bits(&pb, 2, 3);
  243. if (id < 0xe0) {
  244. /* audio */
  245. put_bits(&pb, 1, 0);
  246. put_bits(&pb, 13, stream->max_buffer_size / 128);
  247. } else {
  248. /* video */
  249. put_bits(&pb, 1, 1);
  250. put_bits(&pb, 13, stream->max_buffer_size / 1024);
  251. }
  252. }
  253. }
  254. }
  255. flush_put_bits(&pb);
  256. size = pbBufPtr(&pb) - pb.buf;
  257. /* patch packet size */
  258. buf[4] = (size - 6) >> 8;
  259. buf[5] = (size - 6) & 0xff;
  260. return size;
  261. }
  262. static int get_system_header_size(AVFormatContext *ctx)
  263. {
  264. int buf_index, i, private_stream_coded;
  265. StreamInfo *stream;
  266. MpegMuxContext *s = ctx->priv_data;
  267. if (s->is_dvd)
  268. return 18; // DVD-Video system headers are 18 bytes fixed length.
  269. buf_index = 12;
  270. private_stream_coded = 0;
  271. for(i=0;i<ctx->nb_streams;i++) {
  272. stream = ctx->streams[i]->priv_data;
  273. if (stream->id < 0xc0) {
  274. if (private_stream_coded)
  275. continue;
  276. private_stream_coded = 1;
  277. }
  278. buf_index += 3;
  279. }
  280. return buf_index;
  281. }
  282. static int mpeg_mux_init(AVFormatContext *ctx)
  283. {
  284. MpegMuxContext *s = ctx->priv_data;
  285. int bitrate, i, mpa_id, mpv_id, mps_id, ac3_id, dts_id, lpcm_id, j;
  286. AVStream *st;
  287. StreamInfo *stream;
  288. int audio_bitrate;
  289. int video_bitrate;
  290. s->packet_number = 0;
  291. s->is_vcd = (ctx->oformat == &mpeg1vcd_muxer);
  292. s->is_svcd = (ctx->oformat == &mpeg2svcd_muxer);
  293. s->is_mpeg2 = (ctx->oformat == &mpeg2vob_muxer || ctx->oformat == &mpeg2svcd_muxer || ctx->oformat == &mpeg2dvd_muxer);
  294. s->is_dvd = (ctx->oformat == &mpeg2dvd_muxer);
  295. if(ctx->packet_size)
  296. s->packet_size = ctx->packet_size;
  297. else
  298. s->packet_size = 2048;
  299. s->vcd_padding_bytes_written = 0;
  300. s->vcd_padding_bitrate=0;
  301. s->audio_bound = 0;
  302. s->video_bound = 0;
  303. mpa_id = AUDIO_ID;
  304. ac3_id = AC3_ID;
  305. dts_id = DTS_ID;
  306. mpv_id = VIDEO_ID;
  307. mps_id = SUB_ID;
  308. lpcm_id = LPCM_ID;
  309. for(i=0;i<ctx->nb_streams;i++) {
  310. st = ctx->streams[i];
  311. stream = av_mallocz(sizeof(StreamInfo));
  312. if (!stream)
  313. goto fail;
  314. st->priv_data = stream;
  315. av_set_pts_info(st, 64, 1, 90000);
  316. switch(st->codec->codec_type) {
  317. case CODEC_TYPE_AUDIO:
  318. if (st->codec->codec_id == CODEC_ID_AC3) {
  319. stream->id = ac3_id++;
  320. } else if (st->codec->codec_id == CODEC_ID_DTS) {
  321. stream->id = dts_id++;
  322. } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
  323. stream->id = lpcm_id++;
  324. for(j = 0; j < 4; j++) {
  325. if (lpcm_freq_tab[j] == st->codec->sample_rate)
  326. break;
  327. }
  328. if (j == 4)
  329. goto fail;
  330. if (st->codec->channels > 8)
  331. return -1;
  332. stream->lpcm_header[0] = 0x0c;
  333. stream->lpcm_header[1] = (st->codec->channels - 1) | (j << 4);
  334. stream->lpcm_header[2] = 0x80;
  335. stream->lpcm_align = st->codec->channels * 2;
  336. } else {
  337. stream->id = mpa_id++;
  338. }
  339. /* This value HAS to be used for VCD (see VCD standard, p. IV-7).
  340. Right now it is also used for everything else.*/
  341. stream->max_buffer_size = 4 * 1024;
  342. s->audio_bound++;
  343. break;
  344. case CODEC_TYPE_VIDEO:
  345. stream->id = mpv_id++;
  346. if (st->codec->rc_buffer_size)
  347. stream->max_buffer_size = 6*1024 + st->codec->rc_buffer_size/8;
  348. else
  349. stream->max_buffer_size = 230*1024; //FIXME this is probably too small as default
  350. #if 0
  351. /* see VCD standard, p. IV-7*/
  352. stream->max_buffer_size = 46 * 1024;
  353. else
  354. /* This value HAS to be used for SVCD (see SVCD standard, p. 26 V.2.3.2).
  355. Right now it is also used for everything else.*/
  356. stream->max_buffer_size = 230 * 1024;
  357. #endif
  358. s->video_bound++;
  359. break;
  360. case CODEC_TYPE_SUBTITLE:
  361. stream->id = mps_id++;
  362. stream->max_buffer_size = 16 * 1024;
  363. break;
  364. default:
  365. return -1;
  366. }
  367. av_fifo_init(&stream->fifo, 16);
  368. }
  369. bitrate = 0;
  370. audio_bitrate = 0;
  371. video_bitrate = 0;
  372. for(i=0;i<ctx->nb_streams;i++) {
  373. int codec_rate;
  374. st = ctx->streams[i];
  375. stream = (StreamInfo*) st->priv_data;
  376. if(st->codec->rc_max_rate || stream->id==VIDEO_ID)
  377. codec_rate= st->codec->rc_max_rate;
  378. else
  379. codec_rate= st->codec->bit_rate;
  380. if(!codec_rate)
  381. codec_rate= (1<<21)*8*50/ctx->nb_streams;
  382. bitrate += codec_rate;
  383. if (stream->id==AUDIO_ID)
  384. audio_bitrate += codec_rate;
  385. else if (stream->id==VIDEO_ID)
  386. video_bitrate += codec_rate;
  387. }
  388. if(ctx->mux_rate){
  389. s->mux_rate= (ctx->mux_rate + (8 * 50) - 1) / (8 * 50);
  390. } else {
  391. /* we increase slightly the bitrate to take into account the
  392. headers. XXX: compute it exactly */
  393. bitrate += bitrate*5/100;
  394. bitrate += 10000;
  395. s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
  396. }
  397. if (s->is_vcd) {
  398. double overhead_rate;
  399. /* The VCD standard mandates that the mux_rate field is 3528
  400. (see standard p. IV-6).
  401. The value is actually "wrong", i.e. if you calculate
  402. it using the normal formula and the 75 sectors per second transfer
  403. rate you get a different value because the real pack size is 2324,
  404. not 2352. But the standard explicitly specifies that the mux_rate
  405. field in the header must have this value.*/
  406. // s->mux_rate=2352 * 75 / 50; /* = 3528*/
  407. /* The VCD standard states that the muxed stream must be
  408. exactly 75 packs / second (the data rate of a single speed cdrom).
  409. Since the video bitrate (probably 1150000 bits/sec) will be below
  410. the theoretical maximum we have to add some padding packets
  411. to make up for the lower data rate.
  412. (cf. VCD standard p. IV-6 )*/
  413. /* Add the header overhead to the data rate.
  414. 2279 data bytes per audio pack, 2294 data bytes per video pack*/
  415. overhead_rate = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279);
  416. overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294);
  417. overhead_rate *= 8;
  418. /* Add padding so that the full bitrate is 2324*75 bytes/sec */
  419. s->vcd_padding_bitrate = 2324 * 75 * 8 - (bitrate + overhead_rate);
  420. }
  421. if (s->is_vcd || s->is_mpeg2)
  422. /* every packet */
  423. s->pack_header_freq = 1;
  424. else
  425. /* every 2 seconds */
  426. s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
  427. /* the above seems to make pack_header_freq zero sometimes */
  428. if (s->pack_header_freq == 0)
  429. s->pack_header_freq = 1;
  430. if (s->is_mpeg2)
  431. /* every 200 packets. Need to look at the spec. */
  432. s->system_header_freq = s->pack_header_freq * 40;
  433. else if (s->is_vcd)
  434. /* the standard mandates that there are only two system headers
  435. in the whole file: one in the first packet of each stream.
  436. (see standard p. IV-7 and IV-8) */
  437. s->system_header_freq = 0x7fffffff;
  438. else
  439. s->system_header_freq = s->pack_header_freq * 5;
  440. for(i=0;i<ctx->nb_streams;i++) {
  441. stream = ctx->streams[i]->priv_data;
  442. stream->packet_number = 0;
  443. }
  444. s->system_header_size = get_system_header_size(ctx);
  445. s->last_scr = 0;
  446. return 0;
  447. fail:
  448. for(i=0;i<ctx->nb_streams;i++) {
  449. av_free(ctx->streams[i]->priv_data);
  450. }
  451. return AVERROR(ENOMEM);
  452. }
  453. static inline void put_timestamp(ByteIOContext *pb, int id, int64_t timestamp)
  454. {
  455. put_byte(pb,
  456. (id << 4) |
  457. (((timestamp >> 30) & 0x07) << 1) |
  458. 1);
  459. put_be16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
  460. put_be16(pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
  461. }
  462. /* return the number of padding bytes that should be inserted into
  463. the multiplexed stream.*/
  464. static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts)
  465. {
  466. MpegMuxContext *s = ctx->priv_data;
  467. int pad_bytes = 0;
  468. if (s->vcd_padding_bitrate > 0 && pts!=AV_NOPTS_VALUE)
  469. {
  470. int64_t full_pad_bytes;
  471. full_pad_bytes = (int64_t)((s->vcd_padding_bitrate * (pts / 90000.0)) / 8.0); //FIXME this is wrong
  472. pad_bytes = (int) (full_pad_bytes - s->vcd_padding_bytes_written);
  473. if (pad_bytes<0)
  474. /* might happen if we have already padded to a later timestamp. This
  475. can occur if another stream has already advanced further.*/
  476. pad_bytes=0;
  477. }
  478. return pad_bytes;
  479. }
  480. #if 0 /* unused, remove? */
  481. /* return the exact available payload size for the next packet for
  482. stream 'stream_index'. 'pts' and 'dts' are only used to know if
  483. timestamps are needed in the packet header. */
  484. static int get_packet_payload_size(AVFormatContext *ctx, int stream_index,
  485. int64_t pts, int64_t dts)
  486. {
  487. MpegMuxContext *s = ctx->priv_data;
  488. int buf_index;
  489. StreamInfo *stream;
  490. stream = ctx->streams[stream_index]->priv_data;
  491. buf_index = 0;
  492. if (((s->packet_number % s->pack_header_freq) == 0)) {
  493. /* pack header size */
  494. if (s->is_mpeg2)
  495. buf_index += 14;
  496. else
  497. buf_index += 12;
  498. if (s->is_vcd) {
  499. /* there is exactly one system header for each stream in a VCD MPEG,
  500. One in the very first video packet and one in the very first
  501. audio packet (see VCD standard p. IV-7 and IV-8).*/
  502. if (stream->packet_number==0)
  503. /* The system headers refer only to the stream they occur in,
  504. so they have a constant size.*/
  505. buf_index += 15;
  506. } else {
  507. if ((s->packet_number % s->system_header_freq) == 0)
  508. buf_index += s->system_header_size;
  509. }
  510. }
  511. if ((s->is_vcd && stream->packet_number==0)
  512. || (s->is_svcd && s->packet_number==0))
  513. /* the first pack of each stream contains only the pack header,
  514. the system header and some padding (see VCD standard p. IV-6)
  515. Add the padding size, so that the actual payload becomes 0.*/
  516. buf_index += s->packet_size - buf_index;
  517. else {
  518. /* packet header size */
  519. buf_index += 6;
  520. if (s->is_mpeg2) {
  521. buf_index += 3;
  522. if (stream->packet_number==0)
  523. buf_index += 3; /* PES extension */
  524. buf_index += 1; /* obligatory stuffing byte */
  525. }
  526. if (pts != AV_NOPTS_VALUE) {
  527. if (dts != pts)
  528. buf_index += 5 + 5;
  529. else
  530. buf_index += 5;
  531. } else {
  532. if (!s->is_mpeg2)
  533. buf_index++;
  534. }
  535. if (stream->id < 0xc0) {
  536. /* AC3/LPCM private data header */
  537. buf_index += 4;
  538. if (stream->id >= 0xa0) {
  539. int n;
  540. buf_index += 3;
  541. /* NOTE: we round the payload size to an integer number of
  542. LPCM samples */
  543. n = (s->packet_size - buf_index) % stream->lpcm_align;
  544. if (n)
  545. buf_index += (stream->lpcm_align - n);
  546. }
  547. }
  548. if (s->is_vcd && stream->id == AUDIO_ID)
  549. /* The VCD standard demands that 20 zero bytes follow
  550. each audio packet (see standard p. IV-8).*/
  551. buf_index+=20;
  552. }
  553. return s->packet_size - buf_index;
  554. }
  555. #endif
  556. /* Write an MPEG padding packet header. */
  557. static void put_padding_packet(AVFormatContext *ctx, ByteIOContext *pb,int packet_bytes)
  558. {
  559. MpegMuxContext *s = ctx->priv_data;
  560. int i;
  561. put_be32(pb, PADDING_STREAM);
  562. put_be16(pb, packet_bytes - 6);
  563. if (!s->is_mpeg2) {
  564. put_byte(pb, 0x0f);
  565. packet_bytes -= 7;
  566. } else
  567. packet_bytes -= 6;
  568. for(i=0;i<packet_bytes;i++)
  569. put_byte(pb, 0xff);
  570. }
  571. static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len){
  572. int nb_frames=0;
  573. PacketDesc *pkt_desc= stream->premux_packet;
  574. while(len>0){
  575. if(pkt_desc->size == pkt_desc->unwritten_size)
  576. nb_frames++;
  577. len -= pkt_desc->unwritten_size;
  578. pkt_desc= pkt_desc->next;
  579. }
  580. return nb_frames;
  581. }
  582. /* flush the packet on stream stream_index */
  583. static int flush_packet(AVFormatContext *ctx, int stream_index,
  584. int64_t pts, int64_t dts, int64_t scr, int trailer_size)
  585. {
  586. MpegMuxContext *s = ctx->priv_data;
  587. StreamInfo *stream = ctx->streams[stream_index]->priv_data;
  588. uint8_t *buf_ptr;
  589. int size, payload_size, startcode, id, stuffing_size, i, header_len;
  590. int packet_size;
  591. uint8_t buffer[128];
  592. int zero_trail_bytes = 0;
  593. int pad_packet_bytes = 0;
  594. int pes_flags;
  595. int general_pack = 0; /*"general" pack without data specific to one stream?*/
  596. int nb_frames;
  597. id = stream->id;
  598. #if 0
  599. printf("packet ID=%2x PTS=%0.3f\n",
  600. id, pts / 90000.0);
  601. #endif
  602. buf_ptr = buffer;
  603. if ((s->packet_number % s->pack_header_freq) == 0 || s->last_scr != scr) {
  604. /* output pack and systems header if needed */
  605. size = put_pack_header(ctx, buf_ptr, scr);
  606. buf_ptr += size;
  607. s->last_scr= scr;
  608. if (s->is_vcd) {
  609. /* there is exactly one system header for each stream in a VCD MPEG,
  610. One in the very first video packet and one in the very first
  611. audio packet (see VCD standard p. IV-7 and IV-8).*/
  612. if (stream->packet_number==0) {
  613. size = put_system_header(ctx, buf_ptr, id);
  614. buf_ptr += size;
  615. }
  616. } else if (s->is_dvd) {
  617. if (stream->align_iframe || s->packet_number == 0){
  618. int PES_bytes_to_fill = s->packet_size - size - 10;
  619. if (pts != AV_NOPTS_VALUE) {
  620. if (dts != pts)
  621. PES_bytes_to_fill -= 5 + 5;
  622. else
  623. PES_bytes_to_fill -= 5;
  624. }
  625. if (stream->bytes_to_iframe == 0 || s->packet_number == 0) {
  626. size = put_system_header(ctx, buf_ptr, 0);
  627. buf_ptr += size;
  628. size = buf_ptr - buffer;
  629. put_buffer(&ctx->pb, buffer, size);
  630. put_be32(&ctx->pb, PRIVATE_STREAM_2);
  631. put_be16(&ctx->pb, 0x03d4); // length
  632. put_byte(&ctx->pb, 0x00); // substream ID, 00=PCI
  633. for (i = 0; i < 979; i++)
  634. put_byte(&ctx->pb, 0x00);
  635. put_be32(&ctx->pb, PRIVATE_STREAM_2);
  636. put_be16(&ctx->pb, 0x03fa); // length
  637. put_byte(&ctx->pb, 0x01); // substream ID, 01=DSI
  638. for (i = 0; i < 1017; i++)
  639. put_byte(&ctx->pb, 0x00);
  640. memset(buffer, 0, 128);
  641. buf_ptr = buffer;
  642. s->packet_number++;
  643. stream->align_iframe = 0;
  644. scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
  645. size = put_pack_header(ctx, buf_ptr, scr);
  646. s->last_scr= scr;
  647. buf_ptr += size;
  648. /* GOP Start */
  649. } else if (stream->bytes_to_iframe < PES_bytes_to_fill) {
  650. pad_packet_bytes = PES_bytes_to_fill - stream->bytes_to_iframe;
  651. }
  652. }
  653. } else {
  654. if ((s->packet_number % s->system_header_freq) == 0) {
  655. size = put_system_header(ctx, buf_ptr, 0);
  656. buf_ptr += size;
  657. }
  658. }
  659. }
  660. size = buf_ptr - buffer;
  661. put_buffer(&ctx->pb, buffer, size);
  662. packet_size = s->packet_size - size;
  663. if (s->is_vcd && id == AUDIO_ID)
  664. /* The VCD standard demands that 20 zero bytes follow
  665. each audio pack (see standard p. IV-8).*/
  666. zero_trail_bytes += 20;
  667. if ((s->is_vcd && stream->packet_number==0)
  668. || (s->is_svcd && s->packet_number==0)) {
  669. /* for VCD the first pack of each stream contains only the pack header,
  670. the system header and lots of padding (see VCD standard p. IV-6).
  671. In the case of an audio pack, 20 zero bytes are also added at
  672. the end.*/
  673. /* For SVCD we fill the very first pack to increase compatibility with
  674. some DVD players. Not mandated by the standard.*/
  675. if (s->is_svcd)
  676. general_pack = 1; /* the system header refers to both streams and no stream data*/
  677. pad_packet_bytes = packet_size - zero_trail_bytes;
  678. }
  679. packet_size -= pad_packet_bytes + zero_trail_bytes;
  680. if (packet_size > 0) {
  681. /* packet header size */
  682. packet_size -= 6;
  683. /* packet header */
  684. if (s->is_mpeg2) {
  685. header_len = 3;
  686. if (stream->packet_number==0)
  687. header_len += 3; /* PES extension */
  688. header_len += 1; /* obligatory stuffing byte */
  689. } else {
  690. header_len = 0;
  691. }
  692. if (pts != AV_NOPTS_VALUE) {
  693. if (dts != pts)
  694. header_len += 5 + 5;
  695. else
  696. header_len += 5;
  697. } else {
  698. if (!s->is_mpeg2)
  699. header_len++;
  700. }
  701. payload_size = packet_size - header_len;
  702. if (id < 0xc0) {
  703. startcode = PRIVATE_STREAM_1;
  704. payload_size -= 1;
  705. if (id >= 0x40) {
  706. payload_size -= 3;
  707. if (id >= 0xa0)
  708. payload_size -= 3;
  709. }
  710. } else {
  711. startcode = 0x100 + id;
  712. }
  713. stuffing_size = payload_size - av_fifo_size(&stream->fifo);
  714. // first byte does not fit -> reset pts/dts + stuffing
  715. if(payload_size <= trailer_size && pts != AV_NOPTS_VALUE){
  716. int timestamp_len=0;
  717. if(dts != pts)
  718. timestamp_len += 5;
  719. if(pts != AV_NOPTS_VALUE)
  720. timestamp_len += s->is_mpeg2 ? 5 : 4;
  721. pts=dts= AV_NOPTS_VALUE;
  722. header_len -= timestamp_len;
  723. if (s->is_dvd && stream->align_iframe) {
  724. pad_packet_bytes += timestamp_len;
  725. packet_size -= timestamp_len;
  726. } else {
  727. payload_size += timestamp_len;
  728. }
  729. stuffing_size += timestamp_len;
  730. if(payload_size > trailer_size)
  731. stuffing_size += payload_size - trailer_size;
  732. }
  733. if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) { // can't use padding, so use stuffing
  734. packet_size += pad_packet_bytes;
  735. payload_size += pad_packet_bytes; // undo the previous adjustment
  736. if (stuffing_size < 0) {
  737. stuffing_size = pad_packet_bytes;
  738. } else {
  739. stuffing_size += pad_packet_bytes;
  740. }
  741. pad_packet_bytes = 0;
  742. }
  743. if (stuffing_size < 0)
  744. stuffing_size = 0;
  745. if (stuffing_size > 16) { /*<=16 for MPEG-1, <=32 for MPEG-2*/
  746. pad_packet_bytes += stuffing_size;
  747. packet_size -= stuffing_size;
  748. payload_size -= stuffing_size;
  749. stuffing_size = 0;
  750. }
  751. nb_frames= get_nb_frames(ctx, stream, payload_size - stuffing_size);
  752. put_be32(&ctx->pb, startcode);
  753. put_be16(&ctx->pb, packet_size);
  754. if (!s->is_mpeg2)
  755. for(i=0;i<stuffing_size;i++)
  756. put_byte(&ctx->pb, 0xff);
  757. if (s->is_mpeg2) {
  758. put_byte(&ctx->pb, 0x80); /* mpeg2 id */
  759. pes_flags=0;
  760. if (pts != AV_NOPTS_VALUE) {
  761. pes_flags |= 0x80;
  762. if (dts != pts)
  763. pes_flags |= 0x40;
  764. }
  765. /* Both the MPEG-2 and the SVCD standards demand that the
  766. P-STD_buffer_size field be included in the first packet of
  767. every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
  768. and MPEG-2 standard 2.7.7) */
  769. if (stream->packet_number == 0)
  770. pes_flags |= 0x01;
  771. put_byte(&ctx->pb, pes_flags); /* flags */
  772. put_byte(&ctx->pb, header_len - 3 + stuffing_size);
  773. if (pes_flags & 0x80) /*write pts*/
  774. put_timestamp(&ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
  775. if (pes_flags & 0x40) /*write dts*/
  776. put_timestamp(&ctx->pb, 0x01, dts);
  777. if (pes_flags & 0x01) { /*write pes extension*/
  778. put_byte(&ctx->pb, 0x10); /* flags */
  779. /* P-STD buffer info */
  780. if (id == AUDIO_ID)
  781. put_be16(&ctx->pb, 0x4000 | stream->max_buffer_size/128);
  782. else
  783. put_be16(&ctx->pb, 0x6000 | stream->max_buffer_size/1024);
  784. }
  785. } else {
  786. if (pts != AV_NOPTS_VALUE) {
  787. if (dts != pts) {
  788. put_timestamp(&ctx->pb, 0x03, pts);
  789. put_timestamp(&ctx->pb, 0x01, dts);
  790. } else {
  791. put_timestamp(&ctx->pb, 0x02, pts);
  792. }
  793. } else {
  794. put_byte(&ctx->pb, 0x0f);
  795. }
  796. }
  797. if (s->is_mpeg2) {
  798. /* special stuffing byte that is always written
  799. to prevent accidental generation of start codes. */
  800. put_byte(&ctx->pb, 0xff);
  801. for(i=0;i<stuffing_size;i++)
  802. put_byte(&ctx->pb, 0xff);
  803. }
  804. if (startcode == PRIVATE_STREAM_1) {
  805. put_byte(&ctx->pb, id);
  806. if (id >= 0xa0) {
  807. /* LPCM (XXX: check nb_frames) */
  808. put_byte(&ctx->pb, 7);
  809. put_be16(&ctx->pb, 4); /* skip 3 header bytes */
  810. put_byte(&ctx->pb, stream->lpcm_header[0]);
  811. put_byte(&ctx->pb, stream->lpcm_header[1]);
  812. put_byte(&ctx->pb, stream->lpcm_header[2]);
  813. } else if (id >= 0x40) {
  814. /* AC3 */
  815. put_byte(&ctx->pb, nb_frames);
  816. put_be16(&ctx->pb, trailer_size+1);
  817. }
  818. }
  819. /* output data */
  820. if(av_fifo_generic_read(&stream->fifo, payload_size - stuffing_size, &put_buffer, &ctx->pb) < 0)
  821. return -1;
  822. stream->bytes_to_iframe -= payload_size - stuffing_size;
  823. }else{
  824. payload_size=
  825. stuffing_size= 0;
  826. }
  827. if (pad_packet_bytes > 0)
  828. put_padding_packet(ctx,&ctx->pb, pad_packet_bytes);
  829. for(i=0;i<zero_trail_bytes;i++)
  830. put_byte(&ctx->pb, 0x00);
  831. put_flush_packet(&ctx->pb);
  832. s->packet_number++;
  833. /* only increase the stream packet number if this pack actually contains
  834. something that is specific to this stream! I.e. a dedicated header
  835. or some data.*/
  836. if (!general_pack)
  837. stream->packet_number++;
  838. return payload_size - stuffing_size;
  839. }
  840. static void put_vcd_padding_sector(AVFormatContext *ctx)
  841. {
  842. /* There are two ways to do this padding: writing a sector/pack
  843. of 0 values, or writing an MPEG padding pack. Both seem to
  844. work with most decoders, BUT the VCD standard only allows a 0-sector
  845. (see standard p. IV-4, IV-5).
  846. So a 0-sector it is...*/
  847. MpegMuxContext *s = ctx->priv_data;
  848. int i;
  849. for(i=0;i<s->packet_size;i++)
  850. put_byte(&ctx->pb, 0);
  851. s->vcd_padding_bytes_written += s->packet_size;
  852. put_flush_packet(&ctx->pb);
  853. /* increasing the packet number is correct. The SCR of the following packs
  854. is calculated from the packet_number and it has to include the padding
  855. sector (it represents the sector index, not the MPEG pack index)
  856. (see VCD standard p. IV-6)*/
  857. s->packet_number++;
  858. }
  859. #if 0 /* unused, remove? */
  860. static int64_t get_vcd_scr(AVFormatContext *ctx,int stream_index,int64_t pts)
  861. {
  862. MpegMuxContext *s = ctx->priv_data;
  863. int64_t scr;
  864. /* Since the data delivery rate is constant, SCR is computed
  865. using the formula C + i * 1200 where C is the start constant
  866. and i is the pack index.
  867. It is recommended that SCR 0 is at the beginning of the VCD front
  868. margin (a sequence of empty Form 2 sectors on the CD).
  869. It is recommended that the front margin is 30 sectors long, so
  870. we use C = 30*1200 = 36000
  871. (Note that even if the front margin is not 30 sectors the file
  872. will still be correct according to the standard. It just won't have
  873. the "recommended" value).*/
  874. scr = 36000 + s->packet_number * 1200;
  875. return scr;
  876. }
  877. #endif
  878. static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr){
  879. // MpegMuxContext *s = ctx->priv_data;
  880. int i;
  881. for(i=0; i<ctx->nb_streams; i++){
  882. AVStream *st = ctx->streams[i];
  883. StreamInfo *stream = st->priv_data;
  884. PacketDesc *pkt_desc;
  885. while((pkt_desc= stream->predecode_packet)
  886. && scr > pkt_desc->dts){ //FIXME > vs >=
  887. if(stream->buffer_index < pkt_desc->size ||
  888. stream->predecode_packet == stream->premux_packet){
  889. av_log(ctx, AV_LOG_ERROR,
  890. "buffer underflow i=%d bufi=%d size=%d\n",
  891. i, stream->buffer_index, pkt_desc->size);
  892. break;
  893. }
  894. stream->buffer_index -= pkt_desc->size;
  895. stream->predecode_packet= pkt_desc->next;
  896. av_freep(&pkt_desc);
  897. }
  898. }
  899. return 0;
  900. }
  901. static int output_packet(AVFormatContext *ctx, int flush){
  902. MpegMuxContext *s = ctx->priv_data;
  903. AVStream *st;
  904. StreamInfo *stream;
  905. int i, avail_space, es_size, trailer_size;
  906. int best_i= -1;
  907. int best_score= INT_MIN;
  908. int ignore_constraints=0;
  909. int64_t scr= s->last_scr;
  910. PacketDesc *timestamp_packet;
  911. const int64_t max_delay= av_rescale(ctx->max_delay, 90000, AV_TIME_BASE);
  912. retry:
  913. for(i=0; i<ctx->nb_streams; i++){
  914. AVStream *st = ctx->streams[i];
  915. StreamInfo *stream = st->priv_data;
  916. const int avail_data= av_fifo_size(&stream->fifo);
  917. const int space= stream->max_buffer_size - stream->buffer_index;
  918. int rel_space= 1024*space / stream->max_buffer_size;
  919. PacketDesc *next_pkt= stream->premux_packet;
  920. /* for subtitle, a single PES packet must be generated,
  921. so we flush after every single subtitle packet */
  922. if(s->packet_size > avail_data && !flush
  923. && st->codec->codec_type != CODEC_TYPE_SUBTITLE)
  924. return 0;
  925. if(avail_data==0)
  926. continue;
  927. assert(avail_data>0);
  928. if(space < s->packet_size && !ignore_constraints)
  929. continue;
  930. if(next_pkt && next_pkt->dts - scr > max_delay)
  931. continue;
  932. if(rel_space > best_score){
  933. best_score= rel_space;
  934. best_i = i;
  935. avail_space= space;
  936. }
  937. }
  938. if(best_i < 0){
  939. int64_t best_dts= INT64_MAX;
  940. for(i=0; i<ctx->nb_streams; i++){
  941. AVStream *st = ctx->streams[i];
  942. StreamInfo *stream = st->priv_data;
  943. PacketDesc *pkt_desc= stream->predecode_packet;
  944. if(pkt_desc && pkt_desc->dts < best_dts)
  945. best_dts= pkt_desc->dts;
  946. }
  947. #if 0
  948. av_log(ctx, AV_LOG_DEBUG, "bumping scr, scr:%f, dts:%f\n",
  949. scr/90000.0, best_dts/90000.0);
  950. #endif
  951. if(best_dts == INT64_MAX)
  952. return 0;
  953. if(scr >= best_dts+1 && !ignore_constraints){
  954. av_log(ctx, AV_LOG_ERROR, "packet too large, ignoring buffer limits to mux it\n");
  955. ignore_constraints= 1;
  956. }
  957. scr= FFMAX(best_dts+1, scr);
  958. if(remove_decoded_packets(ctx, scr) < 0)
  959. return -1;
  960. goto retry;
  961. }
  962. assert(best_i >= 0);
  963. st = ctx->streams[best_i];
  964. stream = st->priv_data;
  965. assert(av_fifo_size(&stream->fifo) > 0);
  966. assert(avail_space >= s->packet_size || ignore_constraints);
  967. timestamp_packet= stream->premux_packet;
  968. if(timestamp_packet->unwritten_size == timestamp_packet->size){
  969. trailer_size= 0;
  970. }else{
  971. trailer_size= timestamp_packet->unwritten_size;
  972. timestamp_packet= timestamp_packet->next;
  973. }
  974. if(timestamp_packet){
  975. //av_log(ctx, AV_LOG_DEBUG, "dts:%f pts:%f scr:%f stream:%d\n", timestamp_packet->dts/90000.0, timestamp_packet->pts/90000.0, scr/90000.0, best_i);
  976. es_size= flush_packet(ctx, best_i, timestamp_packet->pts, timestamp_packet->dts, scr, trailer_size);
  977. }else{
  978. assert(av_fifo_size(&stream->fifo) == trailer_size);
  979. es_size= flush_packet(ctx, best_i, AV_NOPTS_VALUE, AV_NOPTS_VALUE, scr, trailer_size);
  980. }
  981. if (s->is_vcd) {
  982. /* Write one or more padding sectors, if necessary, to reach
  983. the constant overall bitrate.*/
  984. int vcd_pad_bytes;
  985. while((vcd_pad_bytes = get_vcd_padding_size(ctx,stream->premux_packet->pts) ) >= s->packet_size){ //FIXME pts cannot be correct here
  986. put_vcd_padding_sector(ctx);
  987. s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
  988. }
  989. }
  990. stream->buffer_index += es_size;
  991. s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
  992. while(stream->premux_packet && stream->premux_packet->unwritten_size <= es_size){
  993. es_size -= stream->premux_packet->unwritten_size;
  994. stream->premux_packet= stream->premux_packet->next;
  995. }
  996. if(es_size)
  997. stream->premux_packet->unwritten_size -= es_size;
  998. if(remove_decoded_packets(ctx, s->last_scr) < 0)
  999. return -1;
  1000. return 1;
  1001. }
  1002. static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
  1003. {
  1004. MpegMuxContext *s = ctx->priv_data;
  1005. int stream_index= pkt->stream_index;
  1006. int size= pkt->size;
  1007. uint8_t *buf= pkt->data;
  1008. AVStream *st = ctx->streams[stream_index];
  1009. StreamInfo *stream = st->priv_data;
  1010. int64_t pts, dts;
  1011. PacketDesc *pkt_desc;
  1012. const int preload= av_rescale(ctx->preload, 90000, AV_TIME_BASE);
  1013. const int is_iframe = st->codec->codec_type == CODEC_TYPE_VIDEO && (pkt->flags & PKT_FLAG_KEY);
  1014. pts= pkt->pts;
  1015. dts= pkt->dts;
  1016. if(pts != AV_NOPTS_VALUE) pts += preload;
  1017. if(dts != AV_NOPTS_VALUE) dts += preload;
  1018. //av_log(ctx, AV_LOG_DEBUG, "dts:%f pts:%f flags:%d stream:%d nopts:%d\n", dts/90000.0, pts/90000.0, pkt->flags, pkt->stream_index, pts != AV_NOPTS_VALUE);
  1019. if (!stream->premux_packet)
  1020. stream->next_packet = &stream->premux_packet;
  1021. *stream->next_packet=
  1022. pkt_desc= av_mallocz(sizeof(PacketDesc));
  1023. pkt_desc->pts= pts;
  1024. pkt_desc->dts= dts;
  1025. pkt_desc->unwritten_size=
  1026. pkt_desc->size= size;
  1027. if(!stream->predecode_packet)
  1028. stream->predecode_packet= pkt_desc;
  1029. stream->next_packet= &pkt_desc->next;
  1030. av_fifo_realloc(&stream->fifo, av_fifo_size(&stream->fifo) + size + 1);
  1031. if (s->is_dvd){
  1032. if (is_iframe && (s->packet_number == 0 || (pts - stream->vobu_start_pts >= 36000))) { // min VOBU length 0.4 seconds (mpucoder)
  1033. stream->bytes_to_iframe = av_fifo_size(&stream->fifo);
  1034. stream->align_iframe = 1;
  1035. stream->vobu_start_pts = pts;
  1036. } else {
  1037. stream->align_iframe = 0;
  1038. }
  1039. }
  1040. av_fifo_write(&stream->fifo, buf, size);
  1041. for(;;){
  1042. int ret= output_packet(ctx, 0);
  1043. if(ret<=0)
  1044. return ret;
  1045. }
  1046. }
  1047. static int mpeg_mux_end(AVFormatContext *ctx)
  1048. {
  1049. // MpegMuxContext *s = ctx->priv_data;
  1050. StreamInfo *stream;
  1051. int i;
  1052. for(;;){
  1053. int ret= output_packet(ctx, 1);
  1054. if(ret<0)
  1055. return ret;
  1056. else if(ret==0)
  1057. break;
  1058. }
  1059. /* End header according to MPEG1 systems standard. We do not write
  1060. it as it is usually not needed by decoders and because it
  1061. complicates MPEG stream concatenation. */
  1062. //put_be32(&ctx->pb, ISO_11172_END_CODE);
  1063. //put_flush_packet(&ctx->pb);
  1064. for(i=0;i<ctx->nb_streams;i++) {
  1065. stream = ctx->streams[i]->priv_data;
  1066. assert(av_fifo_size(&stream->fifo) == 0);
  1067. av_fifo_free(&stream->fifo);
  1068. }
  1069. return 0;
  1070. }
  1071. #endif //CONFIG_MUXERS
  1072. /*********************************************/
  1073. /* demux code */
  1074. #define MAX_SYNC_SIZE 100000
  1075. static int cdxa_probe(AVProbeData *p)
  1076. {
  1077. /* check file header */
  1078. if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
  1079. p->buf[2] == 'F' && p->buf[3] == 'F' &&
  1080. p->buf[8] == 'C' && p->buf[9] == 'D' &&
  1081. p->buf[10] == 'X' && p->buf[11] == 'A')
  1082. return AVPROBE_SCORE_MAX;
  1083. else
  1084. return 0;
  1085. }
  1086. static int mpegps_probe(AVProbeData *p)
  1087. {
  1088. uint32_t code= -1;
  1089. int sys=0, pspack=0, priv1=0, vid=0, audio=0;
  1090. int i;
  1091. int score=0;
  1092. score = cdxa_probe(p);
  1093. if (score > 0) return score;
  1094. /* Search for MPEG stream */
  1095. for(i=0; i<p->buf_size; i++){
  1096. code = (code<<8) + p->buf[i];
  1097. if ((code & 0xffffff00) == 0x100) {
  1098. if(code == SYSTEM_HEADER_START_CODE) sys++;
  1099. else if(code == PRIVATE_STREAM_1) priv1++;
  1100. else if(code == PACK_START_CODE) pspack++;
  1101. else if((code & 0xf0) == VIDEO_ID) vid++;
  1102. else if((code & 0xe0) == AUDIO_ID) audio++;
  1103. }
  1104. }
  1105. if(vid || audio) /* invalid VDR files nd short PES streams */
  1106. score= AVPROBE_SCORE_MAX/4;
  1107. //av_log(NULL, AV_LOG_ERROR, "%d %d %d %d %d\n", sys, priv1, pspack,vid, audio);
  1108. if(sys && sys*9 <= pspack*10)
  1109. return AVPROBE_SCORE_MAX/2+2; // +1 for .mpg
  1110. if((priv1 || vid || audio) && (priv1+vid+audio)*9 <= pspack*10)
  1111. return AVPROBE_SCORE_MAX/2+2; // +1 for .mpg
  1112. if((!!vid ^ !!audio) && (audio+vid > 1) && !sys && !pspack) /* PES stream */
  1113. return AVPROBE_SCORE_MAX/2+2;
  1114. //02-Penguin.flac has sys:0 priv1:0 pspack:0 vid:0 audio:1
  1115. return score;
  1116. }
  1117. typedef struct MpegDemuxContext {
  1118. int32_t header_state;
  1119. unsigned char psm_es_type[256];
  1120. } MpegDemuxContext;
  1121. static int mpegps_read_header(AVFormatContext *s,
  1122. AVFormatParameters *ap)
  1123. {
  1124. MpegDemuxContext *m = s->priv_data;
  1125. m->header_state = 0xff;
  1126. s->ctx_flags |= AVFMTCTX_NOHEADER;
  1127. /* no need to do more */
  1128. return 0;
  1129. }
  1130. static int64_t get_pts(ByteIOContext *pb, int c)
  1131. {
  1132. int64_t pts;
  1133. int val;
  1134. if (c < 0)
  1135. c = get_byte(pb);
  1136. pts = (int64_t)((c >> 1) & 0x07) << 30;
  1137. val = get_be16(pb);
  1138. pts |= (int64_t)(val >> 1) << 15;
  1139. val = get_be16(pb);
  1140. pts |= (int64_t)(val >> 1);
  1141. return pts;
  1142. }
  1143. static int find_next_start_code(ByteIOContext *pb, int *size_ptr,
  1144. int32_t *header_state)
  1145. {
  1146. unsigned int state, v;
  1147. int val, n;
  1148. state = *header_state;
  1149. n = *size_ptr;
  1150. while (n > 0) {
  1151. if (url_feof(pb))
  1152. break;
  1153. v = get_byte(pb);
  1154. n--;
  1155. if (state == 0x000001) {
  1156. state = ((state << 8) | v) & 0xffffff;
  1157. val = state;
  1158. goto found;
  1159. }
  1160. state = ((state << 8) | v) & 0xffffff;
  1161. }
  1162. val = -1;
  1163. found:
  1164. *header_state = state;
  1165. *size_ptr = n;
  1166. return val;
  1167. }
  1168. #if 0 /* unused, remove? */
  1169. /* XXX: optimize */
  1170. static int find_prev_start_code(ByteIOContext *pb, int *size_ptr)
  1171. {
  1172. int64_t pos, pos_start;
  1173. int max_size, start_code;
  1174. max_size = *size_ptr;
  1175. pos_start = url_ftell(pb);
  1176. /* in order to go faster, we fill the buffer */
  1177. pos = pos_start - 16386;
  1178. if (pos < 0)
  1179. pos = 0;
  1180. url_fseek(pb, pos, SEEK_SET);
  1181. get_byte(pb);
  1182. pos = pos_start;
  1183. for(;;) {
  1184. pos--;
  1185. if (pos < 0 || (pos_start - pos) >= max_size) {
  1186. start_code = -1;
  1187. goto the_end;
  1188. }
  1189. url_fseek(pb, pos, SEEK_SET);
  1190. start_code = get_be32(pb);
  1191. if ((start_code & 0xffffff00) == 0x100)
  1192. break;
  1193. }
  1194. the_end:
  1195. *size_ptr = pos_start - pos;
  1196. return start_code;
  1197. }
  1198. #endif
  1199. /**
  1200. * Extracts stream types from a program stream map
  1201. * According to ISO/IEC 13818-1 ('MPEG-2 Systems') table 2-35
  1202. *
  1203. * @return number of bytes occupied by PSM in the bitstream
  1204. */
  1205. static long mpegps_psm_parse(MpegDemuxContext *m, ByteIOContext *pb)
  1206. {
  1207. int psm_length, ps_info_length, es_map_length;
  1208. psm_length = get_be16(pb);
  1209. get_byte(pb);
  1210. get_byte(pb);
  1211. ps_info_length = get_be16(pb);
  1212. /* skip program_stream_info */
  1213. url_fskip(pb, ps_info_length);
  1214. es_map_length = get_be16(pb);
  1215. /* at least one es available? */
  1216. while (es_map_length >= 4){
  1217. unsigned char type = get_byte(pb);
  1218. unsigned char es_id = get_byte(pb);
  1219. uint16_t es_info_length = get_be16(pb);
  1220. /* remember mapping from stream id to stream type */
  1221. m->psm_es_type[es_id] = type;
  1222. /* skip program_stream_info */
  1223. url_fskip(pb, es_info_length);
  1224. es_map_length -= 4 + es_info_length;
  1225. }
  1226. get_be32(pb); /* crc32 */
  1227. return 2 + psm_length;
  1228. }
  1229. /* read the next PES header. Return its position in ppos
  1230. (if not NULL), and its start code, pts and dts.
  1231. */
  1232. static int mpegps_read_pes_header(AVFormatContext *s,
  1233. int64_t *ppos, int *pstart_code,
  1234. int64_t *ppts, int64_t *pdts)
  1235. {
  1236. MpegDemuxContext *m = s->priv_data;
  1237. int len, size, startcode, c, flags, header_len;
  1238. int pes_ext, ext2_len, id_ext, skip;
  1239. int64_t pts, dts;
  1240. int64_t last_sync= url_ftell(&s->pb);
  1241. error_redo:
  1242. url_fseek(&s->pb, last_sync, SEEK_SET);
  1243. redo:
  1244. /* next start code (should be immediately after) */
  1245. m->header_state = 0xff;
  1246. size = MAX_SYNC_SIZE;
  1247. startcode = find_next_start_code(&s->pb, &size, &m->header_state);
  1248. last_sync = url_ftell(&s->pb);
  1249. //printf("startcode=%x pos=0x%"PRIx64"\n", startcode, url_ftell(&s->pb));
  1250. if (startcode < 0)
  1251. return AVERROR_IO;
  1252. if (startcode == PACK_START_CODE)
  1253. goto redo;
  1254. if (startcode == SYSTEM_HEADER_START_CODE)
  1255. goto redo;
  1256. if (startcode == PADDING_STREAM ||
  1257. startcode == PRIVATE_STREAM_2) {
  1258. /* skip them */
  1259. len = get_be16(&s->pb);
  1260. url_fskip(&s->pb, len);
  1261. goto redo;
  1262. }
  1263. if (startcode == PROGRAM_STREAM_MAP) {
  1264. mpegps_psm_parse(m, &s->pb);
  1265. goto redo;
  1266. }
  1267. /* find matching stream */
  1268. if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
  1269. (startcode >= 0x1e0 && startcode <= 0x1ef) ||
  1270. (startcode == 0x1bd) || (startcode == 0x1fd)))
  1271. goto redo;
  1272. if (ppos) {
  1273. *ppos = url_ftell(&s->pb) - 4;
  1274. }
  1275. len = get_be16(&s->pb);
  1276. pts =
  1277. dts = AV_NOPTS_VALUE;
  1278. /* stuffing */
  1279. for(;;) {
  1280. if (len < 1)
  1281. goto error_redo;
  1282. c = get_byte(&s->pb);
  1283. len--;
  1284. /* XXX: for mpeg1, should test only bit 7 */
  1285. if (c != 0xff)
  1286. break;
  1287. }
  1288. if ((c & 0xc0) == 0x40) {
  1289. /* buffer scale & size */
  1290. get_byte(&s->pb);
  1291. c = get_byte(&s->pb);
  1292. len -= 2;
  1293. }
  1294. if ((c & 0xe0) == 0x20) {
  1295. dts = pts = get_pts(&s->pb, c);
  1296. len -= 4;
  1297. if (c & 0x10){
  1298. dts = get_pts(&s->pb, -1);
  1299. len -= 5;
  1300. }
  1301. } else if ((c & 0xc0) == 0x80) {
  1302. /* mpeg 2 PES */
  1303. #if 0 /* some streams have this field set for no apparent reason */
  1304. if ((c & 0x30) != 0) {
  1305. /* Encrypted multiplex not handled */
  1306. goto redo;
  1307. }
  1308. #endif
  1309. flags = get_byte(&s->pb);
  1310. header_len = get_byte(&s->pb);
  1311. len -= 2;
  1312. if (header_len > len)
  1313. goto error_redo;
  1314. len -= header_len;
  1315. if (flags & 0x80) {
  1316. dts = pts = get_pts(&s->pb, -1);
  1317. header_len -= 5;
  1318. if (flags & 0x40) {
  1319. dts = get_pts(&s->pb, -1);
  1320. header_len -= 5;
  1321. }
  1322. }
  1323. if (flags & 0x01) { /* PES extension */
  1324. pes_ext = get_byte(&s->pb);
  1325. header_len--;
  1326. if (pes_ext & 0x40) { /* pack header - should be zero in PS */
  1327. goto error_redo;
  1328. }
  1329. /* Skip PES private data, program packet sequence counter and P-STD buffer */
  1330. skip = (pes_ext >> 4) & 0xb;
  1331. skip += skip & 0x9;
  1332. url_fskip(&s->pb, skip);
  1333. header_len -= skip;
  1334. if (pes_ext & 0x01) { /* PES extension 2 */
  1335. ext2_len = get_byte(&s->pb);
  1336. header_len--;
  1337. if ((ext2_len & 0x7f) > 0) {
  1338. id_ext = get_byte(&s->pb);
  1339. if ((id_ext & 0x80) == 0)
  1340. startcode = ((startcode & 0xff) << 8) | id_ext;
  1341. header_len--;
  1342. }
  1343. }
  1344. }
  1345. if(header_len < 0)
  1346. goto error_redo;
  1347. url_fskip(&s->pb, header_len);
  1348. }
  1349. else if( c!= 0xf )
  1350. goto redo;
  1351. if (startcode == PRIVATE_STREAM_1 && !m->psm_es_type[startcode & 0xff]) {
  1352. startcode = get_byte(&s->pb);
  1353. len--;
  1354. if (startcode >= 0x80 && startcode <= 0xcf) {
  1355. /* audio: skip header */
  1356. get_byte(&s->pb);
  1357. get_byte(&s->pb);
  1358. get_byte(&s->pb);
  1359. len -= 3;
  1360. if (startcode >= 0xb0 && startcode <= 0xbf) {
  1361. /* MLP/TrueHD audio has a 4-byte header */
  1362. get_byte(&s->pb);
  1363. len--;
  1364. }
  1365. }
  1366. }
  1367. if(len<0)
  1368. goto error_redo;
  1369. if(dts != AV_NOPTS_VALUE && ppos){
  1370. int i;
  1371. for(i=0; i<s->nb_streams; i++){
  1372. if(startcode == s->streams[i]->id) {
  1373. av_add_index_entry(s->streams[i], *ppos, dts, 0, 0, AVINDEX_KEYFRAME /* FIXME keyframe? */);
  1374. }
  1375. }
  1376. }
  1377. *pstart_code = startcode;
  1378. *ppts = pts;
  1379. *pdts = dts;
  1380. return len;
  1381. }
  1382. static int mpegps_read_packet(AVFormatContext *s,
  1383. AVPacket *pkt)
  1384. {
  1385. MpegDemuxContext *m = s->priv_data;
  1386. AVStream *st;
  1387. int len, startcode, i, type, codec_id = 0, es_type;
  1388. int64_t pts, dts, dummy_pos; //dummy_pos is needed for the index building to work
  1389. redo:
  1390. len = mpegps_read_pes_header(s, &dummy_pos, &startcode, &pts, &dts);
  1391. if (len < 0)
  1392. return len;
  1393. /* now find stream */
  1394. for(i=0;i<s->nb_streams;i++) {
  1395. st = s->streams[i];
  1396. if (st->id == startcode)
  1397. goto found;
  1398. }
  1399. es_type = m->psm_es_type[startcode & 0xff];
  1400. if(es_type > 0){
  1401. if(es_type == STREAM_TYPE_VIDEO_MPEG1){
  1402. codec_id = CODEC_ID_MPEG2VIDEO;
  1403. type = CODEC_TYPE_VIDEO;
  1404. } else if(es_type == STREAM_TYPE_VIDEO_MPEG2){
  1405. codec_id = CODEC_ID_MPEG2VIDEO;
  1406. type = CODEC_TYPE_VIDEO;
  1407. } else if(es_type == STREAM_TYPE_AUDIO_MPEG1 ||
  1408. es_type == STREAM_TYPE_AUDIO_MPEG2){
  1409. codec_id = CODEC_ID_MP3;
  1410. type = CODEC_TYPE_AUDIO;
  1411. } else if(es_type == STREAM_TYPE_AUDIO_AAC){
  1412. codec_id = CODEC_ID_AAC;
  1413. type = CODEC_TYPE_AUDIO;
  1414. } else if(es_type == STREAM_TYPE_VIDEO_MPEG4){
  1415. codec_id = CODEC_ID_MPEG4;
  1416. type = CODEC_TYPE_VIDEO;
  1417. } else if(es_type == STREAM_TYPE_VIDEO_H264){
  1418. codec_id = CODEC_ID_H264;
  1419. type = CODEC_TYPE_VIDEO;
  1420. } else if(es_type == STREAM_TYPE_AUDIO_AC3){
  1421. codec_id = CODEC_ID_AC3;
  1422. type = CODEC_TYPE_AUDIO;
  1423. } else {
  1424. goto skip;
  1425. }
  1426. } else if (startcode >= 0x1e0 && startcode <= 0x1ef) {
  1427. static const unsigned char avs_seqh[4] = { 0, 0, 1, 0xb0 };
  1428. unsigned char buf[8];
  1429. get_buffer(&s->pb, buf, 8);
  1430. url_fseek(&s->pb, -8, SEEK_CUR);
  1431. if(!memcmp(buf, avs_seqh, 4) && (buf[6] != 0 || buf[7] != 1))
  1432. codec_id = CODEC_ID_CAVS;
  1433. else
  1434. codec_id = CODEC_ID_MPEG2VIDEO;
  1435. type = CODEC_TYPE_VIDEO;
  1436. } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
  1437. type = CODEC_TYPE_AUDIO;
  1438. codec_id = CODEC_ID_MP2;
  1439. } else if (startcode >= 0x80 && startcode <= 0x87) {
  1440. type = CODEC_TYPE_AUDIO;
  1441. codec_id = CODEC_ID_AC3;
  1442. } else if ((startcode >= 0x88 && startcode <= 0x8f)
  1443. ||( startcode >= 0x98 && startcode <= 0x9f)) {
  1444. /* 0x90 - 0x97 is reserved for SDDS in DVD specs */
  1445. type = CODEC_TYPE_AUDIO;
  1446. codec_id = CODEC_ID_DTS;
  1447. } else if (startcode >= 0xa0 && startcode <= 0xaf) {
  1448. type = CODEC_TYPE_AUDIO;
  1449. codec_id = CODEC_ID_PCM_S16BE;
  1450. } else if (startcode >= 0xb0 && startcode <= 0xbf) {
  1451. type = CODEC_TYPE_AUDIO;
  1452. codec_id = CODEC_ID_MLP;
  1453. } else if (startcode >= 0xc0 && startcode <= 0xcf) {
  1454. /* Used for both AC-3 and E-AC-3 in EVOB files */
  1455. type = CODEC_TYPE_AUDIO;
  1456. codec_id = CODEC_ID_AC3;
  1457. } else if (startcode >= 0x20 && startcode <= 0x3f) {
  1458. type = CODEC_TYPE_SUBTITLE;
  1459. codec_id = CODEC_ID_DVD_SUBTITLE;
  1460. } else if (startcode >= 0xfd55 && startcode <= 0xfd5f) {
  1461. type = CODEC_TYPE_VIDEO;
  1462. codec_id = CODEC_ID_VC1;
  1463. } else {
  1464. skip:
  1465. /* skip packet */
  1466. url_fskip(&s->pb, len);
  1467. goto redo;
  1468. }
  1469. /* no stream found: add a new stream */
  1470. st = av_new_stream(s, startcode);
  1471. if (!st)
  1472. goto skip;
  1473. st->codec->codec_type = type;
  1474. st->codec->codec_id = codec_id;
  1475. if (codec_id != CODEC_ID_PCM_S16BE)
  1476. st->need_parsing = AVSTREAM_PARSE_FULL;
  1477. found:
  1478. if(st->discard >= AVDISCARD_ALL)
  1479. goto skip;
  1480. if (startcode >= 0xa0 && startcode <= 0xaf) {
  1481. int b1, freq;
  1482. /* for LPCM, we just skip the header and consider it is raw
  1483. audio data */
  1484. if (len <= 3)
  1485. goto skip;
  1486. get_byte(&s->pb); /* emphasis (1), muse(1), reserved(1), frame number(5) */
  1487. b1 = get_byte(&s->pb); /* quant (2), freq(2), reserved(1), channels(3) */
  1488. get_byte(&s->pb); /* dynamic range control (0x80 = off) */
  1489. len -= 3;
  1490. freq = (b1 >> 4) & 3;
  1491. st->codec->sample_rate = lpcm_freq_tab[freq];
  1492. st->codec->channels = 1 + (b1 & 7);
  1493. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * 2;
  1494. }
  1495. av_new_packet(pkt, len);
  1496. get_buffer(&s->pb, pkt->data, pkt->size);
  1497. pkt->pts = pts;
  1498. pkt->dts = dts;
  1499. pkt->stream_index = st->index;
  1500. #if 0
  1501. av_log(s, AV_LOG_DEBUG, "%d: pts=%0.3f dts=%0.3f size=%d\n",
  1502. pkt->stream_index, pkt->pts / 90000.0, pkt->dts / 90000.0, pkt->size);
  1503. #endif
  1504. return 0;
  1505. }
  1506. static int mpegps_read_close(AVFormatContext *s)
  1507. {
  1508. return 0;
  1509. }
  1510. static int64_t mpegps_read_dts(AVFormatContext *s, int stream_index,
  1511. int64_t *ppos, int64_t pos_limit)
  1512. {
  1513. int len, startcode;
  1514. int64_t pos, pts, dts;
  1515. pos = *ppos;
  1516. #ifdef DEBUG_SEEK
  1517. printf("read_dts: pos=0x%"PRIx64" next=%d -> ", pos, find_next);
  1518. #endif
  1519. url_fseek(&s->pb, pos, SEEK_SET);
  1520. for(;;) {
  1521. len = mpegps_read_pes_header(s, &pos, &startcode, &pts, &dts);
  1522. if (len < 0) {
  1523. #ifdef DEBUG_SEEK
  1524. printf("none (ret=%d)\n", len);
  1525. #endif
  1526. return AV_NOPTS_VALUE;
  1527. }
  1528. if (startcode == s->streams[stream_index]->id &&
  1529. dts != AV_NOPTS_VALUE) {
  1530. break;
  1531. }
  1532. url_fskip(&s->pb, len);
  1533. }
  1534. #ifdef DEBUG_SEEK
  1535. printf("pos=0x%"PRIx64" dts=0x%"PRIx64" %0.3f\n", pos, dts, dts / 90000.0);
  1536. #endif
  1537. *ppos = pos;
  1538. return dts;
  1539. }
  1540. #ifdef CONFIG_MPEG1SYSTEM_MUXER
  1541. AVOutputFormat mpeg1system_muxer = {
  1542. "mpeg",
  1543. "MPEG1 System format",
  1544. "video/mpeg",
  1545. "mpg,mpeg",
  1546. sizeof(MpegMuxContext),
  1547. CODEC_ID_MP2,
  1548. CODEC_ID_MPEG1VIDEO,
  1549. mpeg_mux_init,
  1550. mpeg_mux_write_packet,
  1551. mpeg_mux_end,
  1552. };
  1553. #endif
  1554. #ifdef CONFIG_MPEG1VCD_MUXER
  1555. AVOutputFormat mpeg1vcd_muxer = {
  1556. "vcd",
  1557. "MPEG1 System format (VCD)",
  1558. "video/mpeg",
  1559. NULL,
  1560. sizeof(MpegMuxContext),
  1561. CODEC_ID_MP2,
  1562. CODEC_ID_MPEG1VIDEO,
  1563. mpeg_mux_init,
  1564. mpeg_mux_write_packet,
  1565. mpeg_mux_end,
  1566. };
  1567. #endif
  1568. #ifdef CONFIG_MPEG2VOB_MUXER
  1569. AVOutputFormat mpeg2vob_muxer = {
  1570. "vob",
  1571. "MPEG2 PS format (VOB)",
  1572. "video/mpeg",
  1573. "vob",
  1574. sizeof(MpegMuxContext),
  1575. CODEC_ID_MP2,
  1576. CODEC_ID_MPEG2VIDEO,
  1577. mpeg_mux_init,
  1578. mpeg_mux_write_packet,
  1579. mpeg_mux_end,
  1580. };
  1581. #endif
  1582. /* Same as mpeg2vob_mux except that the pack size is 2324 */
  1583. #ifdef CONFIG_MPEG2SVCD_MUXER
  1584. AVOutputFormat mpeg2svcd_muxer = {
  1585. "svcd",
  1586. "MPEG2 PS format (VOB)",
  1587. "video/mpeg",
  1588. "vob",
  1589. sizeof(MpegMuxContext),
  1590. CODEC_ID_MP2,
  1591. CODEC_ID_MPEG2VIDEO,
  1592. mpeg_mux_init,
  1593. mpeg_mux_write_packet,
  1594. mpeg_mux_end,
  1595. };
  1596. #endif
  1597. /* Same as mpeg2vob_mux except the 'is_dvd' flag is set to produce NAV pkts */
  1598. #ifdef CONFIG_MPEG2DVD_MUXER
  1599. AVOutputFormat mpeg2dvd_muxer = {
  1600. "dvd",
  1601. "MPEG2 PS format (DVD VOB)",
  1602. "video/mpeg",
  1603. "dvd",
  1604. sizeof(MpegMuxContext),
  1605. CODEC_ID_MP2,
  1606. CODEC_ID_MPEG2VIDEO,
  1607. mpeg_mux_init,
  1608. mpeg_mux_write_packet,
  1609. mpeg_mux_end,
  1610. };
  1611. #endif
  1612. #ifdef CONFIG_MPEGPS_DEMUXER
  1613. AVInputFormat mpegps_demuxer = {
  1614. "mpeg",
  1615. "MPEG PS format",
  1616. sizeof(MpegDemuxContext),
  1617. mpegps_probe,
  1618. mpegps_read_header,
  1619. mpegps_read_packet,
  1620. mpegps_read_close,
  1621. NULL, //mpegps_read_seek,
  1622. mpegps_read_dts,
  1623. .flags = AVFMT_SHOW_IDS,
  1624. };
  1625. #endif