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.

406 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
  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,
  32. * FU-B packet types)
  33. */
  34. #include "libavutil/attributes.h"
  35. #include "libavutil/base64.h"
  36. #include "libavutil/avstring.h"
  37. #include "libavcodec/get_bits.h"
  38. #include "avformat.h"
  39. #include "network.h"
  40. #include <assert.h>
  41. #include "rtpdec.h"
  42. #include "rtpdec_formats.h"
  43. struct PayloadContext {
  44. // sdp setup parameters
  45. uint8_t profile_idc;
  46. uint8_t profile_iop;
  47. uint8_t level_idc;
  48. int packetization_mode;
  49. #ifdef DEBUG
  50. int packet_types_received[32];
  51. #endif
  52. };
  53. #ifdef DEBUG
  54. #define COUNT_NAL_TYPE(data, nal) data->packet_types_received[(nal) & 0x1f]++
  55. #else
  56. #define COUNT_NAL_TYPE(data, nal) do { } while (0)
  57. #endif
  58. static const uint8_t start_sequence[] = { 0, 0, 0, 1 };
  59. static int sdp_parse_fmtp_config_h264(AVStream *stream,
  60. PayloadContext *h264_data,
  61. char *attr, char *value)
  62. {
  63. AVCodecContext *codec = stream->codec;
  64. assert(codec->codec_id == AV_CODEC_ID_H264);
  65. assert(h264_data != NULL);
  66. if (!strcmp(attr, "packetization-mode")) {
  67. av_log(codec, AV_LOG_DEBUG, "RTP Packetization Mode: %d\n", atoi(value));
  68. h264_data->packetization_mode = atoi(value);
  69. /*
  70. * Packetization Mode:
  71. * 0 or not present: Single NAL mode (Only nals from 1-23 are allowed)
  72. * 1: Non-interleaved Mode: 1-23, 24 (STAP-A), 28 (FU-A) are allowed.
  73. * 2: Interleaved Mode: 25 (STAP-B), 26 (MTAP16), 27 (MTAP24), 28 (FU-A),
  74. * and 29 (FU-B) are allowed.
  75. */
  76. if (h264_data->packetization_mode > 1)
  77. av_log(codec, AV_LOG_ERROR,
  78. "Interleaved RTP mode is not supported yet.\n");
  79. } else if (!strcmp(attr, "profile-level-id")) {
  80. if (strlen(value) == 6) {
  81. char buffer[3];
  82. // 6 characters=3 bytes, in hex.
  83. uint8_t profile_idc;
  84. uint8_t profile_iop;
  85. uint8_t level_idc;
  86. buffer[0] = value[0];
  87. buffer[1] = value[1];
  88. buffer[2] = '\0';
  89. profile_idc = strtol(buffer, NULL, 16);
  90. buffer[0] = value[2];
  91. buffer[1] = value[3];
  92. profile_iop = strtol(buffer, NULL, 16);
  93. buffer[0] = value[4];
  94. buffer[1] = value[5];
  95. level_idc = strtol(buffer, NULL, 16);
  96. av_log(codec, AV_LOG_DEBUG,
  97. "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. codec->extradata_size = 0;
  105. av_freep(&codec->extradata);
  106. while (*value) {
  107. char base64packet[1024];
  108. uint8_t decoded_packet[1024];
  109. int packet_size;
  110. char *dst = base64packet;
  111. while (*value && *value != ','
  112. && (dst - base64packet) < sizeof(base64packet) - 1) {
  113. *dst++ = *value++;
  114. }
  115. *dst++ = '\0';
  116. if (*value == ',')
  117. value++;
  118. packet_size = av_base64_decode(decoded_packet, base64packet,
  119. sizeof(decoded_packet));
  120. if (packet_size > 0) {
  121. uint8_t *dest = av_malloc(packet_size + sizeof(start_sequence) +
  122. codec->extradata_size +
  123. FF_INPUT_BUFFER_PADDING_SIZE);
  124. if (!dest) {
  125. av_log(codec, AV_LOG_ERROR,
  126. "Unable to allocate memory for extradata!\n");
  127. return AVERROR(ENOMEM);
  128. }
  129. if (codec->extradata_size) {
  130. memcpy(dest, codec->extradata, codec->extradata_size);
  131. av_free(codec->extradata);
  132. }
  133. memcpy(dest + codec->extradata_size, start_sequence,
  134. sizeof(start_sequence));
  135. memcpy(dest + codec->extradata_size + sizeof(start_sequence),
  136. decoded_packet, packet_size);
  137. memset(dest + codec->extradata_size + sizeof(start_sequence) +
  138. packet_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  139. codec->extradata = dest;
  140. codec->extradata_size += sizeof(start_sequence) + packet_size;
  141. }
  142. }
  143. av_log(codec, AV_LOG_DEBUG, "Extradata set to %p (size: %d)!\n",
  144. codec->extradata, codec->extradata_size);
  145. }
  146. return 0;
  147. }
  148. // return 0 on packet, no more left, 1 on packet, 1 on partial packet
  149. static int h264_handle_packet(AVFormatContext *ctx, PayloadContext *data,
  150. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  151. const uint8_t *buf, int len, uint16_t seq,
  152. int flags)
  153. {
  154. uint8_t nal;
  155. uint8_t type;
  156. int result = 0;
  157. if (!len) {
  158. av_log(ctx, AV_LOG_ERROR, "Empty H264 RTP packet\n");
  159. return AVERROR_INVALIDDATA;
  160. }
  161. nal = buf[0];
  162. type = nal & 0x1f;
  163. assert(data);
  164. assert(buf);
  165. /* Simplify the case (these are all the nal types used internally by
  166. * the h264 codec). */
  167. if (type >= 1 && type <= 23)
  168. type = 1;
  169. switch (type) {
  170. case 0: // undefined, but pass them through
  171. case 1:
  172. if ((result = av_new_packet(pkt, len + sizeof(start_sequence))) < 0)
  173. return result;
  174. memcpy(pkt->data, start_sequence, sizeof(start_sequence));
  175. memcpy(pkt->data + sizeof(start_sequence), buf, len);
  176. COUNT_NAL_TYPE(data, nal);
  177. break;
  178. case 24: // STAP-A (one packet, multiple nals)
  179. // consume the STAP-A NAL
  180. buf++;
  181. len--;
  182. // first we are going to figure out the total size
  183. {
  184. int pass = 0;
  185. int total_length = 0;
  186. uint8_t *dst = NULL;
  187. for (pass = 0; pass < 2; pass++) {
  188. const uint8_t *src = buf;
  189. int src_len = len;
  190. while (src_len > 2) {
  191. uint16_t nal_size = AV_RB16(src);
  192. // consume the length of the aggregate
  193. src += 2;
  194. src_len -= 2;
  195. if (nal_size <= src_len) {
  196. if (pass == 0) {
  197. // counting
  198. total_length += sizeof(start_sequence) + nal_size;
  199. } else {
  200. // copying
  201. assert(dst);
  202. memcpy(dst, start_sequence, sizeof(start_sequence));
  203. dst += sizeof(start_sequence);
  204. memcpy(dst, src, nal_size);
  205. COUNT_NAL_TYPE(data, *src);
  206. dst += nal_size;
  207. }
  208. } else {
  209. av_log(ctx, AV_LOG_ERROR,
  210. "nal size exceeds length: %d %d\n", nal_size, src_len);
  211. }
  212. // eat what we handled
  213. src += nal_size;
  214. src_len -= nal_size;
  215. if (src_len < 0)
  216. av_log(ctx, AV_LOG_ERROR,
  217. "Consumed more bytes than we got! (%d)\n", src_len);
  218. }
  219. if (pass == 0) {
  220. /* now we know the total size of the packet (with the
  221. * start sequences added) */
  222. if ((result = av_new_packet(pkt, total_length)) < 0)
  223. return result;
  224. dst = pkt->data;
  225. } else {
  226. assert(dst - pkt->data == total_length);
  227. }
  228. }
  229. }
  230. break;
  231. case 25: // STAP-B
  232. case 26: // MTAP-16
  233. case 27: // MTAP-24
  234. case 29: // FU-B
  235. av_log(ctx, AV_LOG_ERROR,
  236. "Unhandled type (%d) (See RFC for implementation details\n",
  237. type);
  238. result = AVERROR(ENOSYS);
  239. break;
  240. case 28: // FU-A (fragmented nal)
  241. buf++;
  242. len--; // skip the fu_indicator
  243. if (len > 1) {
  244. // these are the same as above, we just redo them here for clarity
  245. uint8_t fu_indicator = nal;
  246. uint8_t fu_header = *buf;
  247. uint8_t start_bit = fu_header >> 7;
  248. uint8_t av_unused end_bit = (fu_header & 0x40) >> 6;
  249. uint8_t nal_type = fu_header & 0x1f;
  250. uint8_t reconstructed_nal;
  251. // Reconstruct this packet's true nal; only the data follows.
  252. /* The original nal forbidden bit and NRI are stored in this
  253. * packet's nal. */
  254. reconstructed_nal = fu_indicator & 0xe0;
  255. reconstructed_nal |= nal_type;
  256. // skip the fu_header
  257. buf++;
  258. len--;
  259. if (start_bit)
  260. COUNT_NAL_TYPE(data, nal_type);
  261. if (start_bit) {
  262. /* copy in the start sequence, and the reconstructed nal */
  263. if ((result = av_new_packet(pkt, sizeof(start_sequence) + sizeof(nal) + len)) < 0)
  264. return result;
  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. if ((result = av_new_packet(pkt, len)) < 0)
  270. return result;
  271. memcpy(pkt->data, buf, len);
  272. }
  273. } else {
  274. av_log(ctx, AV_LOG_ERROR, "Too short data for FU-A H264 RTP packet\n");
  275. result = AVERROR_INVALIDDATA;
  276. }
  277. break;
  278. case 30: // undefined
  279. case 31: // undefined
  280. default:
  281. av_log(ctx, AV_LOG_ERROR, "Undefined type (%d)\n", type);
  282. result = AVERROR_INVALIDDATA;
  283. break;
  284. }
  285. pkt->stream_index = st->index;
  286. return result;
  287. }
  288. static PayloadContext *h264_new_context(void)
  289. {
  290. return av_mallocz(sizeof(PayloadContext) + FF_INPUT_BUFFER_PADDING_SIZE);
  291. }
  292. static void h264_free_context(PayloadContext *data)
  293. {
  294. #ifdef DEBUG
  295. int ii;
  296. for (ii = 0; ii < 32; ii++) {
  297. if (data->packet_types_received[ii])
  298. av_log(NULL, AV_LOG_DEBUG, "Received %d packets of type %d\n",
  299. data->packet_types_received[ii], ii);
  300. }
  301. #endif
  302. av_free(data);
  303. }
  304. static av_cold int h264_init(AVFormatContext *s, int st_index,
  305. PayloadContext *data)
  306. {
  307. if (st_index < 0)
  308. return 0;
  309. s->streams[st_index]->need_parsing = AVSTREAM_PARSE_FULL;
  310. return 0;
  311. }
  312. static int parse_h264_sdp_line(AVFormatContext *s, int st_index,
  313. PayloadContext *h264_data, const char *line)
  314. {
  315. AVStream *stream;
  316. AVCodecContext *codec;
  317. const char *p = line;
  318. if (st_index < 0)
  319. return 0;
  320. stream = s->streams[st_index];
  321. codec = stream->codec;
  322. if (av_strstart(p, "framesize:", &p)) {
  323. char buf1[50];
  324. char *dst = buf1;
  325. // remove the protocol identifier
  326. while (*p && *p == ' ')
  327. p++; // strip spaces.
  328. while (*p && *p != ' ')
  329. p++; // eat protocol identifier
  330. while (*p && *p == ' ')
  331. p++; // strip trailing spaces.
  332. while (*p && *p != '-' && (dst - buf1) < sizeof(buf1) - 1)
  333. *dst++ = *p++;
  334. *dst = '\0';
  335. // a='framesize:96 320-240'
  336. // set our parameters
  337. codec->width = atoi(buf1);
  338. codec->height = atoi(p + 1); // skip the -
  339. } else if (av_strstart(p, "fmtp:", &p)) {
  340. return ff_parse_fmtp(stream, h264_data, p, sdp_parse_fmtp_config_h264);
  341. } else if (av_strstart(p, "cliprect:", &p)) {
  342. // could use this if we wanted.
  343. }
  344. return 0;
  345. }
  346. RTPDynamicProtocolHandler ff_h264_dynamic_handler = {
  347. .enc_name = "H264",
  348. .codec_type = AVMEDIA_TYPE_VIDEO,
  349. .codec_id = AV_CODEC_ID_H264,
  350. .init = h264_init,
  351. .parse_sdp_a_line = parse_h264_sdp_line,
  352. .alloc = h264_new_context,
  353. .free = h264_free_context,
  354. .parse_packet = h264_handle_packet
  355. };