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.

340 line
11KB

  1. /// RFC2250 3. Encapsulation of MPEG Elementary Streams (p4)
  2. /// 3.1 MPEG Video elementary streams
  3. /// 1. The MPEG Video_Sequence_Header, when present, will always be at the beginning of an RTP payload
  4. /// 2. An MPEG GOP_header, when present, will always be at the beginning of the RTP payload, or will follow a Video_Sequence_Header
  5. /// 3. An MPEG Picture_Header, when present, will always be at the beginning of a RTP payload, or will follow a GOP_header
  6. /// 4. An implementation based on this encapsulation assumes that the Video_Sequence_Header is repeated periodically in the MPEG bitstream.
  7. /// 5. The beginning of a slice must either be the first data in a packet(after any MPEG ES headers) or must follow after some integral number of slices in a packet.
  8. ///
  9. /// minimum RTP payload size of 261 bytes must be supported to contain the largest single header
  10. ///
  11. /// 3.2 MPEG Audio elementary streams
  12. /// 1. Multiple audio frames may be encapsulated within one RTP packet.
  13. ///
  14. /// 3.3 RTP Fixed Header for MPEG ES encapsulation (p7)
  15. /// 1. M bit: For video, set to 1 on packet containing MPEG frame end code, 0 otherwise.
  16. /// For audio, set to 1 on first packet of a "talk-spurt," 0 otherwise.
  17. /// 2. timestamp: 32 bit 90K Hz timestamp representing the target transmission time for the first byte of the packet
  18. #include "rtp-packet.h"
  19. #include "rtp-profile.h"
  20. #include "rtp-payload-internal.h"
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <assert.h>
  24. #include <errno.h>
  25. // ISO/IEC 13818-2: 1995 (E) Table 6-1 ¡ª Start code values (p41)
  26. #define MPEG2VIDEO_PICTURE 0x00
  27. #define MPEG2VIDEO_SLICE 0x01 // ~0xAF
  28. #define MPEG2VIDEO_SEQUENCE 0xB3
  29. #define MPEG2VIDEO_GROUP 0xB8
  30. #define N_MPEG12_HEADER 4
  31. #define KHz 90 // 90000Hz
  32. struct rtp_encode_mpeg2es_t
  33. {
  34. struct rtp_packet_t pkt;
  35. struct rtp_payload_t handler;
  36. void* cbparam;
  37. int size;
  38. };
  39. struct mpeg2_video_header_t
  40. {
  41. unsigned int begin_of_sequence : 1;
  42. //unsigned int begin_of_slice : 1;
  43. //unsigned int end_of_slice : 1;
  44. unsigned int frame_type : 3; // This value is constant for each RTP packet of a given picture.
  45. unsigned int temporal_reference : 10;// This value is constant for all RTP packets of a given picture.
  46. // Obtained from the most recent picture header, and are
  47. // constant for each RTP packet of a given picture.
  48. unsigned int FBV : 1;
  49. unsigned int BFC : 3;
  50. unsigned int FFV : 1;
  51. unsigned int FFC : 3;
  52. };
  53. static void* rtp_mpeg2es_pack_create(int size, uint8_t pt, uint16_t seq, uint32_t ssrc, struct rtp_payload_t *handler, void* cbparam)
  54. {
  55. struct rtp_encode_mpeg2es_t *packer;
  56. packer = (struct rtp_encode_mpeg2es_t *)calloc(1, sizeof(*packer));
  57. if (!packer) return NULL;
  58. assert(RTP_PAYLOAD_MP3 == pt || RTP_PAYLOAD_MPV == pt);
  59. memcpy(&packer->handler, handler, sizeof(packer->handler));
  60. packer->cbparam = cbparam;
  61. packer->size = size;
  62. packer->pkt.rtp.v = RTP_VERSION;
  63. packer->pkt.rtp.pt = pt;
  64. packer->pkt.rtp.seq = seq;
  65. packer->pkt.rtp.ssrc = ssrc;
  66. packer->pkt.rtp.m = (RTP_PAYLOAD_MP3 == pt) ? 1 : 0; // set to 1 on first packet of a "talk-spurt," 0 otherwise.
  67. return packer;
  68. }
  69. static void rtp_mpeg2es_pack_destroy(void* pack)
  70. {
  71. struct rtp_encode_mpeg2es_t *packer;
  72. packer = (struct rtp_encode_mpeg2es_t *)pack;
  73. #if defined(_DEBUG) || defined(DEBUG)
  74. memset(packer, 0xCC, sizeof(*packer));
  75. #endif
  76. free(packer);
  77. }
  78. static void rtp_mpeg2es_pack_get_info(void* pack, uint16_t* seq, uint32_t* timestamp)
  79. {
  80. struct rtp_encode_mpeg2es_t *packer;
  81. packer = (struct rtp_encode_mpeg2es_t *)pack;
  82. *seq = (uint16_t)packer->pkt.rtp.seq;
  83. *timestamp = packer->pkt.rtp.timestamp;
  84. }
  85. // 3.5 MPEG Audio-specific header
  86. /*
  87. 0 1 2 3
  88. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  89. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  90. | MBZ | Frag_offset |
  91. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  92. */
  93. static int rtp_mpeg2es_pack_audio(struct rtp_encode_mpeg2es_t *packer, const uint8_t* audio, int bytes)
  94. {
  95. int r, n;
  96. int offset;
  97. uint8_t *rtp;
  98. for (r = offset = 0; 0 == r && bytes > 0; ++packer->pkt.rtp.seq)
  99. {
  100. packer->pkt.payload = audio;
  101. packer->pkt.payloadlen = (bytes + N_MPEG12_HEADER + RTP_FIXED_HEADER) <= packer->size ? bytes : (packer->size - N_MPEG12_HEADER - RTP_FIXED_HEADER);
  102. audio += packer->pkt.payloadlen;
  103. bytes -= packer->pkt.payloadlen;
  104. n = RTP_FIXED_HEADER + N_MPEG12_HEADER + packer->pkt.payloadlen;
  105. rtp = (uint8_t*)packer->handler.alloc(packer->cbparam, n);
  106. if (!rtp) return -ENOMEM;
  107. n = rtp_packet_serialize_header(&packer->pkt, rtp, n);
  108. if (n != RTP_FIXED_HEADER)
  109. {
  110. assert(0);
  111. return -1;
  112. }
  113. packer->pkt.rtp.m = 0; // set to 1 on first packet of a "talk-spurt," 0 otherwise.
  114. /* build fragmented packet */
  115. rtp[n + 0] = 0;
  116. rtp[n + 1] = 0;
  117. rtp[n + 2] = (uint8_t)(offset >> 8);
  118. rtp[n + 3] = (uint8_t)offset;
  119. memcpy(rtp + n + N_MPEG12_HEADER, packer->pkt.payload, packer->pkt.payloadlen);
  120. offset += packer->pkt.payloadlen;
  121. r = packer->handler.packet(packer->cbparam, rtp, n + N_MPEG12_HEADER + packer->pkt.payloadlen, packer->pkt.rtp.timestamp, 0);
  122. packer->handler.free(packer->cbparam, rtp);
  123. }
  124. return r;
  125. }
  126. static const uint8_t* mpeg2_start_code_prefix_find(const uint8_t* p, const uint8_t* end)
  127. {
  128. int i;
  129. for (i = 0; p + i + 4 < end; i++)
  130. {
  131. if (0x00 == p[i] && 0x00 == p[i + 1] && 0x01 == p[i + 2])
  132. return p + i;
  133. }
  134. return end;
  135. }
  136. // 3.4 MPEG Video-specific header
  137. /*
  138. 0 1 2 3
  139. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  140. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  141. | MBZ |T| TR | |N|S|B|E| P | | BFC | | FFC |
  142. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  143. AN FBV FFV
  144. */
  145. // 3.4.1 MPEG-2 Video-specific header extension
  146. /*
  147. 0 1 2 3
  148. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  149. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  150. |X|E|f_[0,0]|f_[0,1]|f_[1,0]|f_[1,1]| DC| PS|T|P|C|Q|V|A|R|H|G|D|
  151. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  152. */
  153. static int rtp_mpeg2es_pack_slice(struct rtp_encode_mpeg2es_t *packer, const uint8_t* video, int bytes, struct mpeg2_video_header_t* h, int marker)
  154. {
  155. int r, n;
  156. uint8_t *rtp;
  157. uint8_t begin_of_slice;
  158. uint8_t end_of_slice;
  159. uint8_t begin_of_sequence;
  160. r = 0;
  161. for (begin_of_slice = 1; 0 == r && bytes > 0; ++packer->pkt.rtp.seq)
  162. {
  163. packer->pkt.payload = video;
  164. packer->pkt.payloadlen = (bytes + N_MPEG12_HEADER + RTP_FIXED_HEADER) <= packer->size ? bytes : (packer->size - N_MPEG12_HEADER - RTP_FIXED_HEADER);
  165. video += packer->pkt.payloadlen;
  166. bytes -= packer->pkt.payloadlen;
  167. n = RTP_FIXED_HEADER + N_MPEG12_HEADER + packer->pkt.payloadlen;
  168. rtp = (uint8_t*)packer->handler.alloc(packer->cbparam, n);
  169. if (!rtp) return -ENOMEM;
  170. packer->pkt.rtp.m = (marker && 0==bytes) ? 1 : 0; // set to 1 on packet containing MPEG frame end code
  171. n = rtp_packet_serialize_header(&packer->pkt, rtp, n);
  172. if (n != RTP_FIXED_HEADER)
  173. {
  174. assert(0);
  175. return -1;
  176. }
  177. /* build fragmented packet */
  178. end_of_slice = bytes ? 0 : 1;
  179. begin_of_sequence = (h->begin_of_sequence && begin_of_slice) ? 1 : 0;
  180. rtp[n + 0] = (uint8_t)(h->temporal_reference >> 8) & 0x03;
  181. rtp[n + 1] = (uint8_t)h->temporal_reference;
  182. rtp[n + 2] = (uint8_t)((begin_of_sequence << 5) | (begin_of_slice << 4) | (end_of_slice << 3) | h->frame_type);
  183. rtp[n + 3] = (uint8_t)((h->FBV << 7) | (h->BFC << 4) | (h->FFV << 3) | h->FFC);
  184. memcpy(rtp + n + N_MPEG12_HEADER, packer->pkt.payload, packer->pkt.payloadlen);
  185. begin_of_slice = 0;
  186. r = packer->handler.packet(packer->cbparam, rtp, n + N_MPEG12_HEADER + packer->pkt.payloadlen, packer->pkt.rtp.timestamp, 0);
  187. packer->handler.free(packer->cbparam, rtp);
  188. }
  189. return r;
  190. }
  191. static int mpeg2_video_header_parse(struct mpeg2_video_header_t* mpeg2vh, const uint8_t* data, int bytes)
  192. {
  193. if (bytes < 4)
  194. return -1;
  195. if (MPEG2VIDEO_PICTURE == data[3])
  196. {
  197. if (bytes < 9)
  198. return -1;
  199. // ISO/IEC 13818-2: 1995 (E) 6.2.3 Picture header (p47)
  200. /*
  201. picture_header() {
  202. picture_start_code 32 bslbf
  203. temporal_reference 10 uimsbf
  204. picture_coding_type 3 uimsbf
  205. vbv_delay 16 uimsbf
  206. if ( picture_coding_type == 2 || picture_coding_type == 3) {
  207. full_pel_forward_vector 1 bslbf
  208. forward_f_code 3 bslbf
  209. }
  210. if ( picture_coding_type == 3 ) {
  211. full_pel_backward_vector 1 bslbf
  212. backward_f_code 3 bslbf
  213. }
  214. }
  215. */
  216. mpeg2vh->frame_type = (data[5] >> 3) & 0x07;
  217. mpeg2vh->temporal_reference = (data[4] << 2) | (data[5] >> 6);
  218. if (2 == mpeg2vh->frame_type)
  219. {
  220. mpeg2vh->FFV = (uint8_t)(data[7] >> 2) & 0x01;
  221. mpeg2vh->FFC = (uint8_t)((data[7] & 0x03) << 1) | ((data[8] >> 7) & 0x01);
  222. }
  223. else if (3 == mpeg2vh->frame_type)
  224. {
  225. mpeg2vh->FFV = (uint8_t)(data[7] >> 2) & 0x01;
  226. mpeg2vh->FFC = (uint8_t)((data[7] & 0x03) << 1) | ((data[8] >> 7) & 0x01);
  227. mpeg2vh->FBV = (uint8_t)(data[8] >> 6) & 0x01;
  228. mpeg2vh->BFC = (uint8_t)(data[8] >> 3) & 0x07;
  229. }
  230. }
  231. else if (MPEG2VIDEO_SEQUENCE == data[3] || MPEG2VIDEO_GROUP == data[3])
  232. {
  233. mpeg2vh->begin_of_sequence = 1;
  234. }
  235. return 0;
  236. }
  237. static int rtp_mpeg2es_pack_video(struct rtp_encode_mpeg2es_t *packer, const uint8_t* video, int bytes)
  238. {
  239. int r;
  240. const uint8_t *p, *pnext, *pend;
  241. struct mpeg2_video_header_t mpeg2vh;
  242. memset(&mpeg2vh, 0, sizeof(mpeg2vh));
  243. pend = video + bytes;
  244. p = mpeg2_start_code_prefix_find(video, pend);
  245. for (r = 0; p < pend && 0 == r; p = pnext)
  246. {
  247. //size_t nalu_size;
  248. mpeg2vh.begin_of_sequence = 0;
  249. mpeg2_video_header_parse(&mpeg2vh, p, (int)(pend - p));
  250. if (pend - p + N_MPEG12_HEADER + RTP_FIXED_HEADER <= packer->size)
  251. {
  252. //nalu_size = pend - p;
  253. pnext = pend;
  254. }
  255. else
  256. {
  257. // current frame end position
  258. pnext = mpeg2_start_code_prefix_find(p + 4, pend);
  259. // try to put multi-slice into together
  260. while(pnext - p + N_MPEG12_HEADER + RTP_FIXED_HEADER < packer->size)
  261. {
  262. const uint8_t* pnextnext;
  263. pnextnext = mpeg2_start_code_prefix_find(pnext + 4, pend);
  264. if (pnextnext - p + N_MPEG12_HEADER + RTP_FIXED_HEADER > packer->size)
  265. break;
  266. // merge and get information
  267. mpeg2_video_header_parse(&mpeg2vh, pnext, (int)(pend - pnext));
  268. pnext = pnextnext;
  269. }
  270. }
  271. r = rtp_mpeg2es_pack_slice(packer, p, (int)(pnext - p), &mpeg2vh, (pnext == pend) ? 1 : 0);
  272. }
  273. return r;
  274. }
  275. static int rtp_mpeg2es_pack_input(void* pack, const void* data, int bytes, uint32_t timestamp)
  276. {
  277. struct rtp_encode_mpeg2es_t *packer;
  278. packer = (struct rtp_encode_mpeg2es_t*)pack;
  279. assert(packer->pkt.rtp.timestamp != timestamp || !packer->pkt.payload /*first packet*/);
  280. packer->pkt.rtp.timestamp = timestamp; //(uint32_t)(time * KHz); // ms -> 90KHZ (RFC2250 p7)
  281. return RTP_PAYLOAD_MP3 == packer->pkt.rtp.pt ?
  282. rtp_mpeg2es_pack_audio(packer, (const uint8_t*)data, bytes) :
  283. rtp_mpeg2es_pack_video(packer, (const uint8_t*)data, bytes);
  284. }
  285. // MPV/MPA (MPEG-1/MPEG-2 Audio/Video Elementary Stream)
  286. struct rtp_payload_encode_t *rtp_mpeg1or2es_encode()
  287. {
  288. static struct rtp_payload_encode_t encode = {
  289. rtp_mpeg2es_pack_create,
  290. rtp_mpeg2es_pack_destroy,
  291. rtp_mpeg2es_pack_get_info,
  292. rtp_mpeg2es_pack_input,
  293. };
  294. return &encode;
  295. }