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.

415 lines
13KB

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