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.

144 lines
4.6KB

  1. /*
  2. * RTP parser for DV payload format (RFC 6469)
  3. * Copyright (c) 2015 Thomas Volkert <thomas@homer-conferencing.com>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/avstring.h"
  22. #include "libavcodec/bytestream.h"
  23. #include "avio_internal.h"
  24. #include "rtpdec_formats.h"
  25. struct PayloadContext {
  26. AVIOContext *buf;
  27. uint32_t timestamp;
  28. int bundled_audio;
  29. };
  30. static av_cold void dv_close_context(PayloadContext *data)
  31. {
  32. ffio_free_dyn_buf(&data->buf);
  33. }
  34. static av_cold int dv_sdp_parse_fmtp_config(AVFormatContext *s,
  35. AVStream *stream,
  36. PayloadContext *dv_data,
  37. const char *attr, const char *value)
  38. {
  39. /* does the DV stream include audio? */
  40. if (!strcmp(attr, "audio") && !strcmp(value, "bundled"))
  41. dv_data->bundled_audio = 1;
  42. /* extract the DV profile */
  43. if (!strcmp(attr, "encode")) {
  44. /* SD-VCR/525-60 */
  45. /* SD-VCR/625-50 */
  46. /* HD-VCR/1125-60 */
  47. /* HD-VCR/1250-50 */
  48. /* SDL-VCR/525-60 */
  49. /* SDL-VCR/625-50 */
  50. /* 314M-25/525-60 */
  51. /* 314M-25/625-50 */
  52. /* 314M-50/525-60 */
  53. /* 314M-50/625-50 */
  54. /* 370M/1080-60i */
  55. /* 370M/1080-50i */
  56. /* 370M/720-60p */
  57. /* 370M/720-50p */
  58. /* 306M/525-60 (for backward compatibility) */
  59. /* 306M/625-50 (for backward compatibility) */
  60. }
  61. return 0;
  62. }
  63. static av_cold int dv_parse_sdp_line(AVFormatContext *ctx, int st_index,
  64. PayloadContext *dv_data, const char *line)
  65. {
  66. AVStream *current_stream;
  67. const char *sdp_line_ptr = line;
  68. if (st_index < 0)
  69. return 0;
  70. current_stream = ctx->streams[st_index];
  71. if (av_strstart(sdp_line_ptr, "fmtp:", &sdp_line_ptr)) {
  72. return ff_parse_fmtp(ctx, current_stream, dv_data, sdp_line_ptr,
  73. dv_sdp_parse_fmtp_config);
  74. }
  75. return 0;
  76. }
  77. static int dv_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_dv_ctx,
  78. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  79. const uint8_t *buf, int len, uint16_t seq,
  80. int flags)
  81. {
  82. int res = 0;
  83. /* drop data of previous packets in case of non-continuous (lossy) packet stream */
  84. if (rtp_dv_ctx->buf && rtp_dv_ctx->timestamp != *timestamp) {
  85. ffio_free_dyn_buf(&rtp_dv_ctx->buf);
  86. }
  87. /* sanity check for size of input packet: 1 byte payload at least */
  88. if (len < 1) {
  89. av_log(ctx, AV_LOG_ERROR, "Too short RTP/DV packet, got %d bytes\n", len);
  90. return AVERROR_INVALIDDATA;
  91. }
  92. /* start frame buffering with new dynamic buffer */
  93. if (!rtp_dv_ctx->buf) {
  94. res = avio_open_dyn_buf(&rtp_dv_ctx->buf);
  95. if (res < 0)
  96. return res;
  97. /* update the timestamp in the frame packet with the one from the RTP packet */
  98. rtp_dv_ctx->timestamp = *timestamp;
  99. }
  100. /* write the fragment to the dyn. buffer */
  101. avio_write(rtp_dv_ctx->buf, buf, len);
  102. /* RTP marker bit means: last fragment of current frame was received;
  103. otherwise, an additional fragment is needed for the current frame */
  104. if (!(flags & RTP_FLAG_MARKER))
  105. return AVERROR(EAGAIN);
  106. /* close frame buffering and create resulting A/V packet */
  107. res = ff_rtp_finalize_packet(pkt, &rtp_dv_ctx->buf, st->index);
  108. if (res < 0)
  109. return res;
  110. return 0;
  111. }
  112. RTPDynamicProtocolHandler ff_dv_dynamic_handler = {
  113. .enc_name = "DV",
  114. .codec_type = AVMEDIA_TYPE_VIDEO,
  115. .codec_id = AV_CODEC_ID_DVVIDEO,
  116. .need_parsing = AVSTREAM_PARSE_FULL,
  117. .parse_sdp_a_line = dv_parse_sdp_line,
  118. .priv_data_size = sizeof(PayloadContext),
  119. .close = dv_close_context,
  120. .parse_packet = dv_handle_packet,
  121. };