rtpdec_qcelp.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * RTP Depacketization of QCELP/PureVoice, RFC 2658
  3. * Copyright (c) 2010 Martin Storsjo
  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. #include "rtpdec_formats.h"
  22. static const uint8_t frame_sizes[] = {
  23. 1, 4, 8, 17, 35
  24. };
  25. typedef struct {
  26. int pos;
  27. int size;
  28. /* The largest frame is 35 bytes, only 10 frames are allowed per
  29. * packet, and we return the first one immediately, so allocate
  30. * space for 9 frames */
  31. uint8_t data[35*9];
  32. } InterleavePacket;
  33. struct PayloadContext {
  34. int interleave_size;
  35. int interleave_index;
  36. InterleavePacket group[6];
  37. int group_finished;
  38. /* The maximum packet size, 10 frames of 35 bytes each, and one
  39. * packet header byte. */
  40. uint8_t next_data[1 + 35*10];
  41. int next_size;
  42. uint32_t next_timestamp;
  43. };
  44. static PayloadContext *qcelp_new_context(void)
  45. {
  46. return av_mallocz(sizeof(PayloadContext));
  47. }
  48. static void qcelp_free_context(PayloadContext *data)
  49. {
  50. av_free(data);
  51. }
  52. static int return_stored_frame(AVFormatContext *ctx, PayloadContext *data,
  53. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  54. const uint8_t *buf, int len);
  55. static int store_packet(AVFormatContext *ctx, PayloadContext *data,
  56. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  57. const uint8_t *buf, int len)
  58. {
  59. int interleave_size, interleave_index;
  60. int frame_size, ret;
  61. InterleavePacket* ip;
  62. if (len < 2)
  63. return AVERROR_INVALIDDATA;
  64. interleave_size = buf[0] >> 3 & 7;
  65. interleave_index = buf[0] & 7;
  66. if (interleave_size > 5) {
  67. av_log(ctx, AV_LOG_ERROR, "Invalid interleave size %d\n",
  68. interleave_size);
  69. return AVERROR_INVALIDDATA;
  70. }
  71. if (interleave_index > interleave_size) {
  72. av_log(ctx, AV_LOG_ERROR, "Invalid interleave index %d/%d\n",
  73. interleave_index, interleave_size);
  74. return AVERROR_INVALIDDATA;
  75. }
  76. if (interleave_size != data->interleave_size) {
  77. int i;
  78. /* First packet, or changed interleave size */
  79. data->interleave_size = interleave_size;
  80. data->interleave_index = 0;
  81. for (i = 0; i < 6; i++)
  82. data->group[i].size = 0;
  83. }
  84. if (interleave_index < data->interleave_index) {
  85. /* Wrapped around - missed the last packet of the previous group. */
  86. if (data->group_finished) {
  87. /* No more data in the packets in this interleaving group, just
  88. * start processing the next one */
  89. data->interleave_index = 0;
  90. } else {
  91. /* Stash away the current packet, emit everything we have of the
  92. * previous group. */
  93. for (; data->interleave_index <= interleave_size;
  94. data->interleave_index++)
  95. data->group[data->interleave_index].size = 0;
  96. if (len > sizeof(data->next_data))
  97. return AVERROR_INVALIDDATA;
  98. memcpy(data->next_data, buf, len);
  99. data->next_size = len;
  100. data->next_timestamp = *timestamp;
  101. *timestamp = RTP_NOTS_VALUE;
  102. data->interleave_index = 0;
  103. return return_stored_frame(ctx, data, st, pkt, timestamp, buf, len);
  104. }
  105. }
  106. if (interleave_index > data->interleave_index) {
  107. /* We missed a packet */
  108. for (; data->interleave_index < interleave_index;
  109. data->interleave_index++)
  110. data->group[data->interleave_index].size = 0;
  111. }
  112. data->interleave_index = interleave_index;
  113. if (buf[1] >= FF_ARRAY_ELEMS(frame_sizes))
  114. return AVERROR_INVALIDDATA;
  115. frame_size = frame_sizes[buf[1]];
  116. if (1 + frame_size > len)
  117. return AVERROR_INVALIDDATA;
  118. if (len - 1 - frame_size > sizeof(data->group[0].data))
  119. return AVERROR_INVALIDDATA;
  120. if ((ret = av_new_packet(pkt, frame_size)) < 0)
  121. return ret;
  122. memcpy(pkt->data, &buf[1], frame_size);
  123. pkt->stream_index = st->index;
  124. ip = &data->group[data->interleave_index];
  125. ip->size = len - 1 - frame_size;
  126. ip->pos = 0;
  127. memcpy(ip->data, &buf[1 + frame_size], ip->size);
  128. /* Each packet must contain the same number of frames according to the
  129. * RFC. If there's no data left in this packet, there shouldn't be any
  130. * in any of the other frames in the interleaving group either. */
  131. data->group_finished = ip->size == 0;
  132. if (interleave_index == interleave_size) {
  133. data->interleave_index = 0;
  134. return !data->group_finished;
  135. } else {
  136. data->interleave_index++;
  137. return 0;
  138. }
  139. }
  140. static int return_stored_frame(AVFormatContext *ctx, PayloadContext *data,
  141. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  142. const uint8_t *buf, int len)
  143. {
  144. InterleavePacket* ip = &data->group[data->interleave_index];
  145. int frame_size, ret;
  146. if (data->group_finished && data->interleave_index == 0) {
  147. *timestamp = data->next_timestamp;
  148. ret = store_packet(ctx, data, st, pkt, timestamp, data->next_data,
  149. data->next_size);
  150. data->next_size = 0;
  151. return ret;
  152. }
  153. if (ip->size == 0) {
  154. /* No stored data for this interleave block, output an empty packet */
  155. if ((ret = av_new_packet(pkt, 1)) < 0)
  156. return ret;
  157. pkt->data[0] = 0; // Blank - could also be 14, Erasure
  158. } else {
  159. if (ip->pos >= ip->size)
  160. return AVERROR_INVALIDDATA;
  161. if (ip->data[ip->pos] >= FF_ARRAY_ELEMS(frame_sizes))
  162. return AVERROR_INVALIDDATA;
  163. frame_size = frame_sizes[ip->data[ip->pos]];
  164. if (ip->pos + frame_size > ip->size)
  165. return AVERROR_INVALIDDATA;
  166. if ((ret = av_new_packet(pkt, frame_size)) < 0)
  167. return ret;
  168. memcpy(pkt->data, &ip->data[ip->pos], frame_size);
  169. ip->pos += frame_size;
  170. data->group_finished = ip->pos >= ip->size;
  171. }
  172. pkt->stream_index = st->index;
  173. if (data->interleave_index == data->interleave_size) {
  174. data->interleave_index = 0;
  175. if (!data->group_finished)
  176. return 1;
  177. else
  178. return data->next_size > 0;
  179. } else {
  180. data->interleave_index++;
  181. return 1;
  182. }
  183. }
  184. static int qcelp_parse_packet(AVFormatContext *ctx, PayloadContext *data,
  185. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  186. const uint8_t *buf, int len, int flags)
  187. {
  188. if (buf)
  189. return store_packet(ctx, data, st, pkt, timestamp, buf, len);
  190. else
  191. return return_stored_frame(ctx, data, st, pkt, timestamp, buf, len);
  192. }
  193. RTPDynamicProtocolHandler ff_qcelp_dynamic_handler = {
  194. .enc_name = "x-Purevoice",
  195. .codec_type = AVMEDIA_TYPE_AUDIO,
  196. .codec_id = AV_CODEC_ID_QCELP,
  197. .static_payload_id = 12,
  198. .alloc = qcelp_new_context,
  199. .free = qcelp_free_context,
  200. .parse_packet = qcelp_parse_packet
  201. };