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.

424 lines
13KB

  1. /*
  2. * Copyright (c) 2012 Andrew D'Addesio
  3. * Copyright (c) 2013-2014 Mozilla Corporation
  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. /**
  22. * @file
  23. * Opus decoder/parser shared code
  24. */
  25. #include <stdint.h>
  26. #include "libavutil/error.h"
  27. #include "opus.h"
  28. #include "vorbis.h"
  29. static const uint16_t opus_frame_duration[32] = {
  30. 480, 960, 1920, 2880,
  31. 480, 960, 1920, 2880,
  32. 480, 960, 1920, 2880,
  33. 480, 960,
  34. 480, 960,
  35. 120, 240, 480, 960,
  36. 120, 240, 480, 960,
  37. 120, 240, 480, 960,
  38. 120, 240, 480, 960,
  39. };
  40. /**
  41. * Read a 1- or 2-byte frame length
  42. */
  43. static inline int xiph_lacing_16bit(const uint8_t **ptr, const uint8_t *end)
  44. {
  45. int val;
  46. if (*ptr >= end)
  47. return AVERROR_INVALIDDATA;
  48. val = *(*ptr)++;
  49. if (val >= 252) {
  50. if (*ptr >= end)
  51. return AVERROR_INVALIDDATA;
  52. val += 4 * *(*ptr)++;
  53. }
  54. return val;
  55. }
  56. /**
  57. * Read a multi-byte length (used for code 3 packet padding size)
  58. */
  59. static inline int xiph_lacing_full(const uint8_t **ptr, const uint8_t *end)
  60. {
  61. int val = 0;
  62. int next;
  63. while (1) {
  64. if (*ptr >= end || val > INT_MAX - 254)
  65. return AVERROR_INVALIDDATA;
  66. next = *(*ptr)++;
  67. val += next;
  68. if (next < 255)
  69. break;
  70. else
  71. val--;
  72. }
  73. return val;
  74. }
  75. /**
  76. * Parse Opus packet info from raw packet data
  77. */
  78. int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size,
  79. int self_delimiting)
  80. {
  81. const uint8_t *ptr = buf;
  82. const uint8_t *end = buf + buf_size;
  83. int padding = 0;
  84. int frame_bytes, i;
  85. if (buf_size < 1)
  86. goto fail;
  87. /* TOC byte */
  88. i = *ptr++;
  89. pkt->code = (i ) & 0x3;
  90. pkt->stereo = (i >> 2) & 0x1;
  91. pkt->config = (i >> 3) & 0x1F;
  92. /* code 2 and code 3 packets have at least 1 byte after the TOC */
  93. if (pkt->code >= 2 && buf_size < 2)
  94. goto fail;
  95. switch (pkt->code) {
  96. case 0:
  97. /* 1 frame */
  98. pkt->frame_count = 1;
  99. pkt->vbr = 0;
  100. if (self_delimiting) {
  101. int len = xiph_lacing_16bit(&ptr, end);
  102. if (len < 0 || len > end - ptr)
  103. goto fail;
  104. end = ptr + len;
  105. buf_size = end - buf;
  106. }
  107. frame_bytes = end - ptr;
  108. if (frame_bytes > MAX_FRAME_SIZE)
  109. goto fail;
  110. pkt->frame_offset[0] = ptr - buf;
  111. pkt->frame_size[0] = frame_bytes;
  112. break;
  113. case 1:
  114. /* 2 frames, equal size */
  115. pkt->frame_count = 2;
  116. pkt->vbr = 0;
  117. if (self_delimiting) {
  118. int len = xiph_lacing_16bit(&ptr, end);
  119. if (len < 0 || 2 * len > end - ptr)
  120. goto fail;
  121. end = ptr + 2 * len;
  122. buf_size = end - buf;
  123. }
  124. frame_bytes = end - ptr;
  125. if (frame_bytes & 1 || frame_bytes >> 1 > MAX_FRAME_SIZE)
  126. goto fail;
  127. pkt->frame_offset[0] = ptr - buf;
  128. pkt->frame_size[0] = frame_bytes >> 1;
  129. pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0];
  130. pkt->frame_size[1] = frame_bytes >> 1;
  131. break;
  132. case 2:
  133. /* 2 frames, different sizes */
  134. pkt->frame_count = 2;
  135. pkt->vbr = 1;
  136. /* read 1st frame size */
  137. frame_bytes = xiph_lacing_16bit(&ptr, end);
  138. if (frame_bytes < 0)
  139. goto fail;
  140. if (self_delimiting) {
  141. int len = xiph_lacing_16bit(&ptr, end);
  142. if (len < 0 || len + frame_bytes > end - ptr)
  143. goto fail;
  144. end = ptr + frame_bytes + len;
  145. buf_size = end - buf;
  146. }
  147. pkt->frame_offset[0] = ptr - buf;
  148. pkt->frame_size[0] = frame_bytes;
  149. /* calculate 2nd frame size */
  150. frame_bytes = end - ptr - pkt->frame_size[0];
  151. if (frame_bytes < 0 || frame_bytes > MAX_FRAME_SIZE)
  152. goto fail;
  153. pkt->frame_offset[1] = pkt->frame_offset[0] + pkt->frame_size[0];
  154. pkt->frame_size[1] = frame_bytes;
  155. break;
  156. case 3:
  157. /* 1 to 48 frames, can be different sizes */
  158. i = *ptr++;
  159. pkt->frame_count = (i ) & 0x3F;
  160. padding = (i >> 6) & 0x01;
  161. pkt->vbr = (i >> 7) & 0x01;
  162. if (pkt->frame_count == 0 || pkt->frame_count > MAX_FRAMES)
  163. goto fail;
  164. /* read padding size */
  165. if (padding) {
  166. padding = xiph_lacing_full(&ptr, end);
  167. if (padding < 0)
  168. goto fail;
  169. }
  170. /* read frame sizes */
  171. if (pkt->vbr) {
  172. /* for VBR, all frames except the final one have their size coded
  173. in the bitstream. the last frame size is implicit. */
  174. int total_bytes = 0;
  175. for (i = 0; i < pkt->frame_count - 1; i++) {
  176. frame_bytes = xiph_lacing_16bit(&ptr, end);
  177. if (frame_bytes < 0)
  178. goto fail;
  179. pkt->frame_size[i] = frame_bytes;
  180. total_bytes += frame_bytes;
  181. }
  182. if (self_delimiting) {
  183. int len = xiph_lacing_16bit(&ptr, end);
  184. if (len < 0 || len + total_bytes + padding > end - ptr)
  185. goto fail;
  186. end = ptr + total_bytes + len + padding;
  187. buf_size = end - buf;
  188. }
  189. frame_bytes = end - ptr - padding;
  190. if (total_bytes > frame_bytes)
  191. goto fail;
  192. pkt->frame_offset[0] = ptr - buf;
  193. for (i = 1; i < pkt->frame_count; i++)
  194. pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1];
  195. pkt->frame_size[pkt->frame_count-1] = frame_bytes - total_bytes;
  196. } else {
  197. /* for CBR, the remaining packet bytes are divided evenly between
  198. the frames */
  199. if (self_delimiting) {
  200. frame_bytes = xiph_lacing_16bit(&ptr, end);
  201. if (frame_bytes < 0 || pkt->frame_count * frame_bytes + padding > end - ptr)
  202. goto fail;
  203. end = ptr + pkt->frame_count * frame_bytes + padding;
  204. buf_size = end - buf;
  205. } else {
  206. frame_bytes = end - ptr - padding;
  207. if (frame_bytes % pkt->frame_count ||
  208. frame_bytes / pkt->frame_count > MAX_FRAME_SIZE)
  209. goto fail;
  210. frame_bytes /= pkt->frame_count;
  211. }
  212. pkt->frame_offset[0] = ptr - buf;
  213. pkt->frame_size[0] = frame_bytes;
  214. for (i = 1; i < pkt->frame_count; i++) {
  215. pkt->frame_offset[i] = pkt->frame_offset[i-1] + pkt->frame_size[i-1];
  216. pkt->frame_size[i] = frame_bytes;
  217. }
  218. }
  219. }
  220. pkt->packet_size = buf_size;
  221. pkt->data_size = pkt->packet_size - padding;
  222. /* total packet duration cannot be larger than 120ms */
  223. pkt->frame_duration = opus_frame_duration[pkt->config];
  224. if (pkt->frame_duration * pkt->frame_count > MAX_PACKET_DUR)
  225. goto fail;
  226. /* set mode and bandwidth */
  227. if (pkt->config < 12) {
  228. pkt->mode = OPUS_MODE_SILK;
  229. pkt->bandwidth = pkt->config >> 2;
  230. } else if (pkt->config < 16) {
  231. pkt->mode = OPUS_MODE_HYBRID;
  232. pkt->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND + (pkt->config >= 14);
  233. } else {
  234. pkt->mode = OPUS_MODE_CELT;
  235. pkt->bandwidth = (pkt->config - 16) >> 2;
  236. /* skip medium band */
  237. if (pkt->bandwidth)
  238. pkt->bandwidth++;
  239. }
  240. return 0;
  241. fail:
  242. memset(pkt, 0, sizeof(*pkt));
  243. return AVERROR_INVALIDDATA;
  244. }
  245. static int channel_reorder_vorbis(int nb_channels, int channel_idx)
  246. {
  247. return ff_vorbis_channel_layout_offsets[nb_channels - 1][channel_idx];
  248. }
  249. static int channel_reorder_unknown(int nb_channels, int channel_idx)
  250. {
  251. return channel_idx;
  252. }
  253. av_cold int ff_opus_parse_extradata(AVCodecContext *avctx,
  254. OpusContext *s)
  255. {
  256. static const uint8_t default_channel_map[2] = { 0, 1 };
  257. int (*channel_reorder)(int, int) = channel_reorder_unknown;
  258. const uint8_t *extradata, *channel_map;
  259. int extradata_size;
  260. int version, channels, map_type, streams, stereo_streams, i, j;
  261. uint64_t layout;
  262. if (!avctx->extradata) {
  263. if (avctx->channels > 2) {
  264. av_log(avctx, AV_LOG_ERROR,
  265. "Multichannel configuration without extradata.\n");
  266. return AVERROR(EINVAL);
  267. }
  268. extradata = opus_default_extradata;
  269. extradata_size = sizeof(opus_default_extradata);
  270. } else {
  271. extradata = avctx->extradata;
  272. extradata_size = avctx->extradata_size;
  273. }
  274. if (extradata_size < 19) {
  275. av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
  276. extradata_size);
  277. return AVERROR_INVALIDDATA;
  278. }
  279. version = extradata[8];
  280. if (version > 15) {
  281. avpriv_request_sample(avctx, "Extradata version %d", version);
  282. return AVERROR_PATCHWELCOME;
  283. }
  284. avctx->delay = AV_RL16(extradata + 10);
  285. channels = avctx->extradata ? extradata[9] : (avctx->channels == 1) ? 1 : 2;
  286. if (!channels) {
  287. av_log(avctx, AV_LOG_ERROR, "Zero channel count specified in the extadata\n");
  288. return AVERROR_INVALIDDATA;
  289. }
  290. s->gain_i = AV_RL16(extradata + 16);
  291. if (s->gain_i)
  292. s->gain = pow(10, s->gain_i / (20.0 * 256));
  293. map_type = extradata[18];
  294. if (!map_type) {
  295. if (channels > 2) {
  296. av_log(avctx, AV_LOG_ERROR,
  297. "Channel mapping 0 is only specified for up to 2 channels\n");
  298. return AVERROR_INVALIDDATA;
  299. }
  300. layout = (channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
  301. streams = 1;
  302. stereo_streams = channels - 1;
  303. channel_map = default_channel_map;
  304. } else if (map_type == 1 || map_type == 255) {
  305. if (extradata_size < 21 + channels) {
  306. av_log(avctx, AV_LOG_ERROR, "Invalid extradata size: %d\n",
  307. extradata_size);
  308. return AVERROR_INVALIDDATA;
  309. }
  310. streams = extradata[19];
  311. stereo_streams = extradata[20];
  312. if (!streams || stereo_streams > streams ||
  313. streams + stereo_streams > 255) {
  314. av_log(avctx, AV_LOG_ERROR,
  315. "Invalid stream/stereo stream count: %d/%d\n", streams, stereo_streams);
  316. return AVERROR_INVALIDDATA;
  317. }
  318. if (map_type == 1) {
  319. if (channels > 8) {
  320. av_log(avctx, AV_LOG_ERROR,
  321. "Channel mapping 1 is only specified for up to 8 channels\n");
  322. return AVERROR_INVALIDDATA;
  323. }
  324. layout = ff_vorbis_channel_layouts[channels - 1];
  325. channel_reorder = channel_reorder_vorbis;
  326. } else
  327. layout = 0;
  328. channel_map = extradata + 21;
  329. } else {
  330. avpriv_request_sample(avctx, "Mapping type %d", map_type);
  331. return AVERROR_PATCHWELCOME;
  332. }
  333. s->channel_maps = av_mallocz_array(channels, sizeof(*s->channel_maps));
  334. if (!s->channel_maps)
  335. return AVERROR(ENOMEM);
  336. for (i = 0; i < channels; i++) {
  337. ChannelMap *map = &s->channel_maps[i];
  338. uint8_t idx = channel_map[channel_reorder(channels, i)];
  339. if (idx == 255) {
  340. map->silence = 1;
  341. continue;
  342. } else if (idx >= streams + stereo_streams) {
  343. av_log(avctx, AV_LOG_ERROR,
  344. "Invalid channel map for output channel %d: %d\n", i, idx);
  345. return AVERROR_INVALIDDATA;
  346. }
  347. /* check that we did not see this index yet */
  348. map->copy = 0;
  349. for (j = 0; j < i; j++)
  350. if (channel_map[channel_reorder(channels, j)] == idx) {
  351. map->copy = 1;
  352. map->copy_idx = j;
  353. break;
  354. }
  355. if (idx < 2 * stereo_streams) {
  356. map->stream_idx = idx / 2;
  357. map->channel_idx = idx & 1;
  358. } else {
  359. map->stream_idx = idx - stereo_streams;
  360. map->channel_idx = 0;
  361. }
  362. }
  363. avctx->channels = channels;
  364. avctx->channel_layout = layout;
  365. s->nb_streams = streams;
  366. s->nb_stereo_streams = stereo_streams;
  367. return 0;
  368. }