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.

mpeg-ps-enc.c 8.0KB

10 月之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // ITU-T H.222.0(06/2012)
  2. // Information technology - Generic coding of moving pictures and associated audio information: Systems
  3. // 2.5.3.1 Program stream(p74)
  4. #include "mpeg-pes-internal.h"
  5. #include "mpeg-ps-internal.h"
  6. #include "mpeg-util.h"
  7. #include "mpeg-ps.h"
  8. #include <errno.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <assert.h>
  13. #include <time.h>
  14. #define MAX_PES_HEADER 1024 // pack_header + system_header + psm
  15. #define MAX_PES_PACKET 0xFFFF // 64k pes data
  16. struct ps_muxer_t
  17. {
  18. struct psm_t psm;
  19. struct ps_pack_header_t pack;
  20. struct ps_system_header_t system;
  21. int h264_h265_with_aud;
  22. unsigned int psm_period;
  23. unsigned int scr_period;
  24. struct ps_muxer_func_t func;
  25. void* param;
  26. // uint8_t packet[MAX_PACKET_SIZE];
  27. };
  28. static struct pes_t* ps_stream_find(struct ps_muxer_t *ps, int streamid)
  29. {
  30. size_t i;
  31. for (i = 0; i < ps->psm.stream_count; i++)
  32. {
  33. if (streamid == ps->psm.streams[i].sid)
  34. return &ps->psm.streams[i];
  35. }
  36. return NULL;
  37. }
  38. int ps_muxer_input(struct ps_muxer_t* ps, int streamid, int flags, int64_t pts, int64_t dts, const void* data, size_t bytes)
  39. {
  40. int r, first;
  41. size_t i, n, sz;
  42. uint8_t *packet;
  43. struct pes_t* stream;
  44. const uint8_t* payload;
  45. i = 0;
  46. first = 1;
  47. payload = (const uint8_t*)data;
  48. stream = ps_stream_find(ps, streamid);
  49. if (NULL == stream) return -1; // not found
  50. stream->data_alignment_indicator = (flags & MPEG_FLAG_IDR_FRAME) ? 1 : 0; // idr frame
  51. stream->pts = pts;
  52. stream->dts = dts;
  53. // Add PSM for IDR frame
  54. ps->psm_period = ((flags & MPEG_FLAG_IDR_FRAME) && mpeg_stream_type_video(stream->codecid)) ? 0 : ps->psm_period;
  55. ps->h264_h265_with_aud = (flags & MPEG_FLAG_H264_H265_WITH_AUD) ? 1 : 0;
  56. // TODO:
  57. // 1. update packet header program_mux_rate
  58. // 2. update system header rate_bound
  59. // alloc once (include Multi-PES packet)
  60. sz = bytes + MAX_PES_HEADER + (bytes/MAX_PES_PACKET+1) * 64; // 64 = 0x000001 + stream_id + PES_packet_length + other
  61. packet = ps->func.alloc(ps->param, sz);
  62. if(!packet) return -ENOMEM;
  63. // write pack_header(p74)
  64. // 2.7.1 Frequency of coding the system clock reference
  65. // http://www.bretl.com/mpeghtml/SCR.HTM
  66. //the maximum allowed interval between SCRs is 700ms
  67. //ps->pack.system_clock_reference_base = (dts-3600) % (((int64_t)1)<<33);
  68. ps->pack.system_clock_reference_base = dts >= 3600 ? (dts - 3600) : 0;
  69. ps->pack.system_clock_reference_extension = 0;
  70. ps->pack.program_mux_rate = 6106;
  71. i += pack_header_write(&ps->pack, packet + i);
  72. #if !defined(MPEG_FIX_VLC_3_X_PS_SYSTEM_HEADER)
  73. // https://github.com/videolan/vlc/blob/3.0.x/modules/demux/mpeg/ps.h#L488
  74. // fix ps_pkt_parse_system -> ps_track_fill with default mp1/2 audio codec(without psm)
  75. // write system_header(p76)
  76. if(0 == (ps->psm_period % 30))
  77. i += system_header_write(&ps->system, packet + i);
  78. #endif
  79. // write program_stream_map(p79)
  80. if (0 == (ps->psm_period % 30))
  81. {
  82. #if defined(MPEG_CLOCK_EXTENSION_DESCRIPTOR)
  83. ps->psm.clock = time() * 1000; // todo: gettimeofday
  84. #endif
  85. i += psm_write(&ps->psm, packet + i);
  86. }
  87. // check packet size
  88. assert(i < MAX_PES_HEADER);
  89. // write data
  90. while(bytes > 0)
  91. {
  92. uint8_t *p;
  93. uint8_t *pes = packet + i;
  94. p = pes + pes_write_header(stream, pes, sz - i);
  95. assert(p - pes < 64);
  96. if(first)
  97. {
  98. if (PSI_STREAM_H264 == stream->codecid && !ps->h264_h265_with_aud)
  99. {
  100. // 2.14 Carriage of Rec. ITU-T H.264 | ISO/IEC 14496-10 video
  101. // Each AVC access unit shall contain an access unit delimiter NAL Unit
  102. nbo_w32(p, 0x00000001);
  103. p[4] = 0x09; // AUD
  104. p[5] = 0xE0; // any slice type (0xe) + rbsp stop one bit
  105. p += 6;
  106. }
  107. else if (PSI_STREAM_H265 == stream->codecid && !ps->h264_h265_with_aud)
  108. {
  109. // 2.17 Carriage of HEVC
  110. // Each HEVC access unit shall contain an access unit delimiter NAL unit.
  111. nbo_w32(p, 0x00000001);
  112. p[4] = 0x46; // 35-AUD_NUT
  113. p[5] = 01;
  114. p[6] = 0x50; // B&P&I (0x2) + rbsp stop one bit
  115. p += 7;
  116. }
  117. }
  118. // PES_packet_length = PES-Header + Payload-Size
  119. // A value of 0 indicates that the PES packet length is neither specified nor bounded
  120. // and is allowed only in PES packets whose payload consists of bytes from a
  121. // video elementary stream contained in transport stream packets
  122. if((p - pes - 6) + bytes > MAX_PES_PACKET)
  123. {
  124. nbo_w16(pes + 4, MAX_PES_PACKET);
  125. n = MAX_PES_PACKET - (p - pes - 6);
  126. }
  127. else
  128. {
  129. nbo_w16(pes + 4, (uint16_t)((p - pes - 6) + bytes));
  130. n = bytes;
  131. }
  132. memcpy(p, payload, n);
  133. payload += n;
  134. bytes -= n;
  135. // notify packet already
  136. i += n + (p - pes);
  137. // i = 0; // clear value, the next pes packet don't need pack_header
  138. first = 0; // clear first packet flag
  139. pts = dts = 0; // only first packet write PTS/DTS
  140. }
  141. assert(i < sz);
  142. r = ps->func.write(ps->param, stream->sid, packet, i);
  143. ps->func.free(ps->param, packet);
  144. ++ps->psm_period;
  145. return r;
  146. }
  147. struct ps_muxer_t* ps_muxer_create(const struct ps_muxer_func_t *func, void* param)
  148. {
  149. struct ps_muxer_t *ps = NULL;
  150. assert(func);
  151. ps = (struct ps_muxer_t *)calloc(1, sizeof(struct ps_muxer_t));
  152. if(!ps)
  153. return NULL;
  154. memcpy(&ps->func, func, sizeof(ps->func));
  155. ps->param = param;
  156. ps->system.rate_bound = 26234; //10493600~10mbps(50BPS * 8 = 400bps)
  157. // ps->system.audio_bound = 1; // [0,32] max active audio streams
  158. // ps->system.video_bound = 1; // [0,16] max active video streams
  159. ps->system.fixed_flag = 0; // 1-fixed bitrate, 0-variable bitrate
  160. ps->system.CSPS_flag = 0; // meets the constraints defined in 2.7.9.
  161. ps->system.packet_rate_restriction_flag = 0; // dependence CSPS_flag
  162. ps->system.system_audio_lock_flag = 0; // all audio stream sampling rate is constant
  163. ps->system.system_video_lock_flag = 0; // all video stream frequency is constant
  164. //ps->psm.ver = 1;
  165. //ps->psm.stream_count = 2;
  166. //ps->psm.streams[0].element_stream_id = PES_SID_VIDEO;
  167. //ps->psm.streams[0].stream_type = PSI_STREAM_H264;
  168. //ps->psm.streams[1].element_stream_id = PES_SID_AUDIO;
  169. //ps->psm.streams[1].stream_type = PSI_STREAM_AAC;
  170. return ps;
  171. }
  172. int ps_muxer_destroy(struct ps_muxer_t* ps)
  173. {
  174. size_t i;
  175. for (i = 0; i < ps->psm.stream_count; i++)
  176. {
  177. if (ps->psm.streams[i].esinfo)
  178. {
  179. free(ps->psm.streams[i].esinfo);
  180. ps->psm.streams[i].esinfo = NULL;
  181. }
  182. }
  183. free(ps);
  184. return 0;
  185. }
  186. int ps_muxer_add_stream(struct ps_muxer_t* ps, int codecid, const void* extradata, size_t bytes)
  187. {
  188. struct psm_t* psm;
  189. struct pes_t* pes;
  190. assert(bytes < 512);
  191. if (!ps || ps->psm.stream_count >= sizeof(ps->psm.streams) / sizeof(ps->psm.streams[0]))
  192. {
  193. assert(0);
  194. return -1;
  195. }
  196. psm = &ps->psm;
  197. pes = &psm->streams[psm->stream_count];
  198. if (mpeg_stream_type_video(codecid))
  199. {
  200. pes->sid = (uint8_t)(PES_SID_VIDEO + ps->system.video_bound);
  201. assert(ps->system.video_bound + 1 < 16);
  202. ++ps->system.video_bound; // [0,16] max active video streams
  203. ps->system.streams[ps->system.stream_count].buffer_bound_scale = 1;
  204. /* FIXME -- VCD uses 46, SVCD uses 230, ffmpeg has 230 with a note that it is small */
  205. ps->system.streams[ps->system.stream_count].buffer_size_bound = 400 /* 8191-13 bits max value */;
  206. }
  207. else if (mpeg_stream_type_audio(codecid))
  208. {
  209. pes->sid = (uint8_t)(PES_SID_AUDIO + ps->system.audio_bound);
  210. assert(ps->system.audio_bound + 1 < 32);
  211. ++ps->system.audio_bound; // [0,32] max active audio streams
  212. ps->system.streams[ps->system.stream_count].buffer_bound_scale = 0;
  213. /* This value HAS to be used for VCD (see VCD standard, p. IV-7).
  214. * Right now it is also used for everything else. */
  215. ps->system.streams[ps->system.stream_count].buffer_size_bound = 32 /* 4 * 1024 / 128 */;
  216. }
  217. else
  218. {
  219. assert(0);
  220. return -1;
  221. }
  222. if (bytes > 0)
  223. {
  224. pes->esinfo = (uint8_t*)malloc(bytes);
  225. if (!pes->esinfo)
  226. return -1;
  227. memcpy(pes->esinfo, extradata, bytes);
  228. pes->esinfo_len = (uint16_t)bytes;
  229. }
  230. assert(psm->stream_count == ps->system.stream_count);
  231. ps->system.streams[ps->system.stream_count].stream_id = pes->sid;
  232. ++ps->system.stream_count;
  233. pes->codecid = (uint8_t)codecid;
  234. ++psm->stream_count;
  235. ++psm->ver;
  236. ps->psm_period = 0; // immediate update psm
  237. return pes->sid;
  238. }