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.

307 lines
9.4KB

  1. /*
  2. * RTP parser for VP9 payload format (draft version 0) - experimental
  3. * Copyright (c) 2015 Thomas Volkert <thomas@homer-conferencing.com>
  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. #include "libavcodec/bytestream.h"
  23. #include "rtpdec_formats.h"
  24. #define RTP_VP9_DESC_REQUIRED_SIZE 1
  25. struct PayloadContext {
  26. AVIOContext *buf;
  27. uint32_t timestamp;
  28. };
  29. static void vp9_free_dyn_buffer(AVIOContext **dyn_buf)
  30. {
  31. uint8_t *ptr_dyn_buffer;
  32. avio_close_dyn_buf(*dyn_buf, &ptr_dyn_buffer);
  33. av_free(ptr_dyn_buffer);
  34. *dyn_buf = NULL;
  35. }
  36. static av_cold int vp9_init(AVFormatContext *ctx, int st_index,
  37. PayloadContext *data)
  38. {
  39. av_dlog(ctx, "vp9_init() for stream %d\n", st_index);
  40. av_log(ctx, AV_LOG_WARNING,
  41. "RTP/VP9 support is still experimental\n");
  42. if (st_index < 0)
  43. return 0;
  44. ctx->streams[st_index]->need_parsing = AVSTREAM_PARSE_FULL;
  45. return 0;
  46. }
  47. static int vp9_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_vp9_ctx,
  48. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  49. const uint8_t *buf, int len, uint16_t seq,
  50. int flags)
  51. {
  52. int has_pic_id, has_layer_idc, has_ref_idc, has_ss_data, has_su_data;
  53. av_unused int pic_id = 0, non_key_frame = 0;
  54. av_unused int layer_temporal = -1, layer_spatial = -1, layer_quality = -1;
  55. int ref_fields = 0, has_ref_field_ext_pic_id = 0;
  56. int first_fragment, last_fragment;
  57. int res = 0;
  58. /* drop data of previous packets in case of non-continuous (lossy) packet stream */
  59. if (rtp_vp9_ctx->buf && rtp_vp9_ctx->timestamp != *timestamp) {
  60. vp9_free_dyn_buffer(&rtp_vp9_ctx->buf);
  61. }
  62. /* sanity check for size of input packet: 1 byte payload at least */
  63. if (len < RTP_VP9_DESC_REQUIRED_SIZE + 1) {
  64. av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet, got %d bytes\n", len);
  65. return AVERROR_INVALIDDATA;
  66. }
  67. /*
  68. decode the required VP9 payload descriptor according to section 4.2 of the spec.:
  69. 0 1 2 3 4 5 6 7
  70. +-+-+-+-+-+-+-+-+
  71. |I|L|F|B|E|V|U|-| (REQUIRED)
  72. +-+-+-+-+-+-+-+-+
  73. I: PictureID present
  74. L: Layer indices present
  75. F: Reference indices present
  76. B: Start of VP9 frame
  77. E: End of picture
  78. V: Scalability Structure (SS) present
  79. U: Scalability Structure Update (SU) present
  80. */
  81. has_pic_id = buf[0] & 0x80;
  82. has_layer_idc = buf[0] & 0x40;
  83. has_ref_idc = buf[0] & 0x20;
  84. first_fragment = buf[0] & 0x10;
  85. last_fragment = buf[0] & 0x08;
  86. has_ss_data = buf[0] & 0x04;
  87. has_su_data = buf[0] & 0x02;
  88. /* sanity check for markers: B should always be equal to the RTP M marker */
  89. if (last_fragment >> 2 != flags & RTP_FLAG_MARKER) {
  90. av_log(ctx, AV_LOG_ERROR, "Invalid combination of B and M marker\n");
  91. return AVERROR_INVALIDDATA;
  92. }
  93. /* pass the extensions field */
  94. buf += RTP_VP9_DESC_REQUIRED_SIZE;
  95. len -= RTP_VP9_DESC_REQUIRED_SIZE;
  96. /*
  97. decode the 1-byte/2-byte picture ID:
  98. 0 1 2 3 4 5 6 7
  99. +-+-+-+-+-+-+-+-+
  100. I: |M|PICTURE ID | (RECOMMENDED)
  101. +-+-+-+-+-+-+-+-+
  102. M: | EXTENDED PID | (RECOMMENDED)
  103. +-+-+-+-+-+-+-+-+
  104. M: The most significant bit of the first octet is an extension flag.
  105. PictureID: 8 or 16 bits including the M bit.
  106. */
  107. if (has_pic_id) {
  108. if (len < 1) {
  109. av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
  110. return AVERROR_INVALIDDATA;
  111. }
  112. /* check for 1-byte or 2-byte picture index */
  113. if (buf[0] & 0x80) {
  114. if (len < 2) {
  115. av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
  116. return AVERROR_INVALIDDATA;
  117. }
  118. pic_id = AV_RB16(buf) & 0x7fff;
  119. buf += 2;
  120. len -= 2;
  121. } else {
  122. pic_id = buf[0] & 0x7f;
  123. buf++;
  124. len--;
  125. }
  126. }
  127. /*
  128. decode layer indices
  129. 0 1 2 3 4 5 6 7
  130. +-+-+-+-+-+-+-+-+
  131. L: | T | S | Q | R | (CONDITIONALLY RECOMMENDED)
  132. +-+-+-+-+-+-+-+-+
  133. T, S and Q are 2-bit indices for temporal, spatial, and quality layers.
  134. If "F" is set in the initial octet, R is 2 bits representing the number
  135. of reference fields this frame refers to.
  136. */
  137. if (has_layer_idc) {
  138. if (len < 1) {
  139. av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet");
  140. return AVERROR_INVALIDDATA;
  141. }
  142. layer_temporal = buf[0] & 0xC0;
  143. layer_spatial = buf[0] & 0x30;
  144. layer_quality = buf[0] & 0x0C;
  145. if (has_ref_idc) {
  146. ref_fields = buf[0] & 0x03;
  147. if (ref_fields)
  148. non_key_frame = 1;
  149. }
  150. buf++;
  151. len--;
  152. }
  153. /*
  154. decode the reference fields
  155. 0 1 2 3 4 5 6 7
  156. +-+-+-+-+-+-+-+-+ -\
  157. F: | PID |X| RS| RQ| (OPTIONAL) .
  158. +-+-+-+-+-+-+-+-+ . - R times
  159. X: | EXTENDED PID | (OPTIONAL) .
  160. +-+-+-+-+-+-+-+-+ -/
  161. PID: The relative Picture ID referred to by this frame.
  162. RS and RQ: The spatial and quality layer IDs.
  163. X: 1 if this layer index has an extended relative Picture ID.
  164. */
  165. if (has_ref_idc) {
  166. while (ref_fields) {
  167. if (len < 1) {
  168. av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
  169. return AVERROR_INVALIDDATA;
  170. }
  171. has_ref_field_ext_pic_id = buf[0] & 0x10;
  172. /* pass ref. field */
  173. if (has_ref_field_ext_pic_id) {
  174. if (len < 2) {
  175. av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
  176. return AVERROR_INVALIDDATA;
  177. }
  178. /* ignore ref. data */
  179. buf += 2;
  180. len -= 2;
  181. } else {
  182. /* ignore ref. data */
  183. buf++;
  184. len--;
  185. }
  186. ref_fields--;
  187. }
  188. }
  189. /*
  190. decode the scalability structure (SS)
  191. 0 1 2 3 4 5 6 7
  192. +-+-+-+-+-+-+-+-+
  193. V: | PATTERN LENGTH|
  194. +-+-+-+-+-+-+-+-+ -\
  195. | T | S | Q | R | (OPTIONAL) .
  196. +-+-+-+-+-+-+-+-+ -\ .
  197. | PID |X| RS| RQ| (OPTIONAL) . . - PAT. LEN. times
  198. +-+-+-+-+-+-+-+-+ . - R times .
  199. X: | EXTENDED PID | (OPTIONAL) . .
  200. +-+-+-+-+-+-+-+-+ -/ -/
  201. PID: The relative Picture ID referred to by this frame.
  202. RS and RQ: The spatial and quality layer IDs.
  203. X: 1 if this layer index has an extended relative Picture ID.
  204. */
  205. if (has_ss_data) {
  206. avpriv_report_missing_feature(ctx, "VP9 scalability structure data\n");
  207. return AVERROR_PATCHWELCOME;
  208. }
  209. /*
  210. decode the scalability update structure (SU)
  211. spec. is tbd
  212. */
  213. if (has_su_data) {
  214. avpriv_report_missing_feature(ctx, "VP9 scalability update structure data\n");
  215. return AVERROR_PATCHWELCOME;
  216. }
  217. /*
  218. decode the VP9 payload header
  219. spec. is tbd
  220. */
  221. //XXX: implement when specified
  222. /* sanity check: 1 byte payload as minimum */
  223. if (len < 1) {
  224. av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
  225. return AVERROR_INVALIDDATA;
  226. }
  227. /* start frame buffering with new dynamic buffer */
  228. if (!rtp_vp9_ctx->buf) {
  229. /* sanity check: a new frame should have started */
  230. if (first_fragment) {
  231. res = avio_open_dyn_buf(&rtp_vp9_ctx->buf);
  232. if (res < 0)
  233. return res;
  234. /* update the timestamp in the frame packet with the one from the RTP packet */
  235. rtp_vp9_ctx->timestamp = *timestamp;
  236. } else {
  237. /* frame not started yet, need more packets */
  238. return AVERROR(EAGAIN);
  239. }
  240. }
  241. /* write the fragment to the dyn. buffer */
  242. avio_write(rtp_vp9_ctx->buf, buf, len);
  243. /* do we need more fragments? */
  244. if (!last_fragment)
  245. return AVERROR(EAGAIN);
  246. /* close frame buffering and create resulting A/V packet */
  247. res = ff_rtp_finalize_packet(pkt, &rtp_vp9_ctx->buf, st->index);
  248. if (res < 0)
  249. return res;
  250. return 0;
  251. }
  252. RTPDynamicProtocolHandler ff_vp9_dynamic_handler = {
  253. .enc_name = "VP9",
  254. .codec_type = AVMEDIA_TYPE_VIDEO,
  255. .codec_id = AV_CODEC_ID_VP9,
  256. .init = vp9_init,
  257. .priv_data_size = sizeof(PayloadContext),
  258. .parse_packet = vp9_handle_packet
  259. };