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.

412 lines
14KB

  1. /*
  2. * RTP H264 Protocol (RFC3984)
  3. * Copyright (c) 2006 Ryan Martell.
  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. /**
  22. * @file rtp_h264.c
  23. * @brief H.264 / RTP Code (RFC3984)
  24. * @author Ryan Martell <rdm4@martellventures.com>
  25. *
  26. * @note Notes:
  27. * Notes:
  28. * This currently supports packetization mode:
  29. * Single Nal Unit Mode (0), or
  30. * Non-Interleaved Mode (1). It currently does not support
  31. * Interleaved Mode (2). (This requires implementing STAP-B, MTAP16, MTAP24, FU-B packet types)
  32. *
  33. * @note TODO:
  34. * 1) RTCP sender reports for udp streams are required..
  35. *
  36. */
  37. #include "avformat.h"
  38. #include "mpegts.h"
  39. #include "bitstream.h"
  40. #include <unistd.h>
  41. #include "network.h"
  42. #include <assert.h>
  43. #include "rtp_internal.h"
  44. #include "rtp_h264.h"
  45. #include "base64.h"
  46. /**
  47. RTP/H264 specific private data.
  48. */
  49. typedef struct h264_rtp_extra_data {
  50. unsigned long cookie; ///< sanity check, to make sure we get the pointer we're expecting.
  51. //sdp setup parameters
  52. uint8_t profile_idc; ///< from the sdp setup parameters.
  53. uint8_t profile_iop; ///< from the sdp setup parameters.
  54. uint8_t level_idc; ///< from the sdp setup parameters.
  55. int packetization_mode; ///< from the sdp setup parameters.
  56. #ifdef DEBUG
  57. int packet_types_received[32];
  58. #endif
  59. } h264_rtp_extra_data;
  60. #define MAGIC_COOKIE (0xdeadbeef) ///< Cookie for the extradata; to verify we are what we think we are, and that we haven't been freed.
  61. #define DEAD_COOKIE (0xdeaddead) ///< Cookie for the extradata; once it is freed.
  62. /* ---------------- private code */
  63. static void sdp_parse_fmtp_config_h264(AVStream * stream,
  64. h264_rtp_extra_data * h264_data,
  65. char *attr, char *value)
  66. {
  67. AVCodecContext *codec = stream->codec;
  68. assert(codec->codec_id == CODEC_ID_H264);
  69. assert(h264_data != NULL);
  70. if (!strcmp(attr, "packetization-mode")) {
  71. av_log(NULL, AV_LOG_DEBUG, "H.264/RTP Packetization Mode: %d\n", atoi(attr));
  72. h264_data->packetization_mode = atoi(attr);
  73. /*
  74. Packetization Mode:
  75. 0 or not present: Single NAL mode (Only nals from 1-23 are allowed)
  76. 1: Non-interleaved Mode: 1-23, 24 (STAP-A), 28 (FU-A) are allowed.
  77. 2: Interleaved Mode: 25 (STAP-B), 26 (MTAP16), 27 (MTAP24), 28 (FU-A), and 29 (FU-B) are allowed.
  78. */
  79. if (h264_data->packetization_mode > 1)
  80. av_log(stream, AV_LOG_ERROR,
  81. "H.264/RTP Interleaved RTP mode is not supported yet.");
  82. } else if (!strcmp(attr, "profile-level-id")) {
  83. if (strlen(value) == 6) {
  84. char buffer[3];
  85. // 6 characters=3 bytes, in hex.
  86. uint8_t profile_idc;
  87. uint8_t profile_iop;
  88. uint8_t level_idc;
  89. buffer[0] = value[0]; buffer[1] = value[1]; buffer[2] = '\0';
  90. profile_idc = strtol(buffer, NULL, 16);
  91. buffer[0] = value[2]; buffer[1] = value[3];
  92. profile_iop = strtol(buffer, NULL, 16);
  93. buffer[0] = value[4]; buffer[1] = value[5];
  94. level_idc = strtol(buffer, NULL, 16);
  95. // set the parameters...
  96. av_log(NULL, AV_LOG_DEBUG,
  97. "H.264/RTP Profile IDC: %x Profile IOP: %x Level: %x\n",
  98. profile_idc, profile_iop, level_idc);
  99. h264_data->profile_idc = profile_idc;
  100. h264_data->profile_iop = profile_iop;
  101. h264_data->level_idc = level_idc;
  102. }
  103. } else if (!strcmp(attr, "sprop-parameter-sets")) {
  104. uint8_t start_sequence[]= { 0, 0, 1 };
  105. codec->extradata_size= 0;
  106. codec->extradata= NULL;
  107. while (*value) {
  108. char base64packet[1024];
  109. uint8_t decoded_packet[1024];
  110. uint32_t packet_size;
  111. char *dst = base64packet;
  112. while (*value && *value != ','
  113. && (dst - base64packet) < sizeof(base64packet) - 1) {
  114. *dst++ = *value++;
  115. }
  116. *dst++ = '\0';
  117. if (*value == ',')
  118. value++;
  119. packet_size= av_base64_decode(decoded_packet, base64packet, sizeof(decoded_packet));
  120. if (packet_size) {
  121. uint8_t *dest= av_malloc(packet_size+sizeof(start_sequence)+codec->extradata_size);
  122. if(dest)
  123. {
  124. if(codec->extradata_size)
  125. {
  126. // av_realloc?
  127. memcpy(dest, codec->extradata, codec->extradata_size);
  128. av_free(codec->extradata);
  129. }
  130. memcpy(dest+codec->extradata_size, start_sequence, sizeof(start_sequence));
  131. memcpy(dest+codec->extradata_size+sizeof(start_sequence), decoded_packet, packet_size);
  132. codec->extradata= dest;
  133. codec->extradata_size+= sizeof(start_sequence)+packet_size;
  134. } else {
  135. av_log(NULL, AV_LOG_ERROR, "H.264/RTP Unable to allocate memory for extradata!");
  136. }
  137. }
  138. }
  139. av_log(NULL, AV_LOG_DEBUG, "H.264/RTP Extradata set to %p (size: %d)!", codec->extradata, codec->extradata_size);
  140. }
  141. }
  142. // return 0 on packet, no more left, 1 on packet, 1 on partial packet...
  143. static int h264_handle_packet(RTPDemuxContext * s,
  144. AVPacket * pkt,
  145. uint32_t * timestamp,
  146. const uint8_t * buf,
  147. int len)
  148. {
  149. // h264_rtp_extra_data *data = s->dynamic_protocol_context;
  150. uint8_t nal = buf[0];
  151. uint8_t type = (nal & 0x1f);
  152. int result= 0;
  153. uint8_t start_sequence[]= {0, 0, 1};
  154. assert(data);
  155. assert(data->cookie == MAGIC_COOKIE);
  156. assert(buf);
  157. if (type >= 1 && type <= 23)
  158. type = 1; // simplify the case. (these are all the nal types used internally by the h264 codec)
  159. switch (type) {
  160. case 0: // undefined;
  161. result= -1;
  162. break;
  163. case 1:
  164. av_new_packet(pkt, len+sizeof(start_sequence));
  165. memcpy(pkt->data, start_sequence, sizeof(start_sequence));
  166. memcpy(pkt->data+sizeof(start_sequence), buf, len);
  167. #ifdef DEBUG
  168. data->packet_types_received[nal & 0x1f]++;
  169. #endif
  170. break;
  171. case 24: // STAP-A (one packet, multiple nals)
  172. // consume the STAP-A NAL
  173. buf++;
  174. len--;
  175. // first we are going to figure out the total size....
  176. {
  177. int pass= 0;
  178. int total_length= 0;
  179. uint8_t *dst= NULL;
  180. for(pass= 0; pass<2; pass++) {
  181. const uint8_t *src= buf;
  182. int src_len= len;
  183. do {
  184. uint16_t nal_size = AV_RB16(src); // this going to be a problem if unaligned (can it be?)
  185. // consume the length of the aggregate...
  186. src += 2;
  187. src_len -= 2;
  188. if (nal_size <= src_len) {
  189. if(pass==0) {
  190. // counting...
  191. total_length+= sizeof(start_sequence)+nal_size;
  192. } else {
  193. // copying
  194. assert(dst);
  195. memcpy(dst, start_sequence, sizeof(start_sequence));
  196. dst+= sizeof(start_sequence);
  197. memcpy(dst, src, nal_size);
  198. #ifdef DEBUG
  199. data->packet_types_received[*src & 0x1f]++;
  200. #endif
  201. dst+= nal_size;
  202. }
  203. } else {
  204. av_log(NULL, AV_LOG_ERROR,
  205. "nal size exceeds length: %d %d\n", nal_size, src_len);
  206. }
  207. // eat what we handled...
  208. src += nal_size;
  209. src_len -= nal_size;
  210. if (src_len < 0)
  211. av_log(NULL, AV_LOG_ERROR,
  212. "Consumed more bytes than we got! (%d)\n", src_len);
  213. } while (src_len > 2); // because there could be rtp padding..
  214. if(pass==0) {
  215. // now we know the total size of the packet (with the start sequences added)
  216. av_new_packet(pkt, total_length);
  217. dst= pkt->data;
  218. } else {
  219. assert(dst-pkt->data==total_length);
  220. }
  221. }
  222. }
  223. break;
  224. case 25: // STAP-B
  225. case 26: // MTAP-16
  226. case 27: // MTAP-24
  227. case 29: // FU-B
  228. av_log(NULL, AV_LOG_ERROR,
  229. "Unhandled type (%d) (See RFC for implementation details\n",
  230. type);
  231. result= -1;
  232. break;
  233. case 28: // FU-A (fragmented nal)
  234. buf++;
  235. len--; // skip the fu_indicator
  236. {
  237. // these are the same as above, we just redo them here for clarity...
  238. uint8_t fu_indicator = nal;
  239. uint8_t fu_header = *buf; // read the fu_header.
  240. uint8_t start_bit = (fu_header & 0x80) >> 7;
  241. // uint8_t end_bit = (fu_header & 0x40) >> 6;
  242. uint8_t nal_type = (fu_header & 0x1f);
  243. uint8_t reconstructed_nal;
  244. // reconstruct this packet's true nal; only the data follows..
  245. reconstructed_nal = fu_indicator & (0xe0); // the original nal forbidden bit and NRI are stored in this packet's nal;
  246. reconstructed_nal |= (nal_type & 0x1f);
  247. // skip the fu_header...
  248. buf++;
  249. len--;
  250. #ifdef DEBUG
  251. if (start_bit)
  252. data->packet_types_received[nal_type & 0x1f]++;
  253. #endif
  254. if(start_bit) {
  255. // copy in the start sequence, and the reconstructed nal....
  256. av_new_packet(pkt, sizeof(start_sequence)+sizeof(nal)+len);
  257. memcpy(pkt->data, start_sequence, sizeof(start_sequence));
  258. pkt->data[sizeof(start_sequence)]= reconstructed_nal;
  259. memcpy(pkt->data+sizeof(start_sequence)+sizeof(nal), buf, len);
  260. } else {
  261. av_new_packet(pkt, len);
  262. memcpy(pkt->data, buf, len);
  263. }
  264. }
  265. break;
  266. case 30: // undefined
  267. case 31: // undefined
  268. default:
  269. av_log(NULL, AV_LOG_ERROR, "Undefined type (%d)", type);
  270. result= -1;
  271. break;
  272. }
  273. return result;
  274. }
  275. /* ---------------- public code */
  276. static void *h264_new_extradata()
  277. {
  278. h264_rtp_extra_data *data =
  279. av_mallocz(sizeof(h264_rtp_extra_data) +
  280. FF_INPUT_BUFFER_PADDING_SIZE);
  281. if (data) {
  282. data->cookie = MAGIC_COOKIE;
  283. }
  284. return data;
  285. }
  286. static void h264_free_extradata(void *d)
  287. {
  288. h264_rtp_extra_data *data = (h264_rtp_extra_data *) d;
  289. #ifdef DEBUG
  290. int ii;
  291. for (ii = 0; ii < 32; ii++) {
  292. if (data->packet_types_received[ii])
  293. av_log(NULL, AV_LOG_DEBUG, "Received %d packets of type %d\n",
  294. data->packet_types_received[ii], ii);
  295. }
  296. #endif
  297. assert(data);
  298. assert(data->cookie == MAGIC_COOKIE);
  299. // avoid stale pointers (assert)
  300. data->cookie = DEAD_COOKIE;
  301. // and clear out this...
  302. av_free(data);
  303. }
  304. static int parse_h264_sdp_line(AVStream * stream, void *data,
  305. const char *line)
  306. {
  307. AVCodecContext *codec = stream->codec;
  308. h264_rtp_extra_data *h264_data = (h264_rtp_extra_data *) data;
  309. const char *p = line;
  310. assert(h264_data->cookie == MAGIC_COOKIE);
  311. if (strstart(p, "framesize:", &p)) {
  312. char buf1[50];
  313. char *dst = buf1;
  314. // remove the protocol identifier..
  315. while (*p && *p == ' ') p++; // strip spaces.
  316. while (*p && *p != ' ') p++; // eat protocol identifier
  317. while (*p && *p == ' ') p++; // strip trailing spaces.
  318. while (*p && *p != '-' && (buf1 - dst) < sizeof(buf1) - 1) {
  319. *dst++ = *p++;
  320. }
  321. *dst = '\0';
  322. // a='framesize:96 320-240'
  323. // set our parameters..
  324. codec->width = atoi(buf1);
  325. codec->height = atoi(p + 1); // skip the -
  326. codec->pix_fmt = PIX_FMT_YUV420P;
  327. } else if (strstart(p, "fmtp:", &p)) {
  328. char attr[256];
  329. char value[4096];
  330. // remove the protocol identifier..
  331. while (*p && *p == ' ') p++; // strip spaces.
  332. while (*p && *p != ' ') p++; // eat protocol identifier
  333. while (*p && *p == ' ') p++; // strip trailing spaces.
  334. /* loop on each attribute */
  335. while (rtsp_next_attr_and_value
  336. (&p, attr, sizeof(attr), value, sizeof(value))) {
  337. /* grab the codec extra_data from the config parameter of the fmtp line */
  338. sdp_parse_fmtp_config_h264(stream, h264_data, attr, value);
  339. }
  340. } else if (strstart(p, "cliprect:", &p)) {
  341. // could use this if we wanted.
  342. }
  343. av_set_pts_info(stream, 33, 1, 90000); // 33 should be right, because the pts is 64 bit? (done elsewhere; this is a one time thing)
  344. return 0; // keep processing it the normal way...
  345. }
  346. /**
  347. This is the structure for expanding on the dynamic rtp protocols (makes everything static. yay!)
  348. */
  349. RTPDynamicProtocolHandler ff_h264_dynamic_handler = {
  350. "H264",
  351. CODEC_TYPE_VIDEO,
  352. CODEC_ID_H264,
  353. parse_h264_sdp_line,
  354. h264_new_extradata,
  355. h264_free_extradata,
  356. h264_handle_packet
  357. };