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.

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