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.

425 lines
13KB

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