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.

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