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.

854 lines
39KB

  1. /*
  2. * RIFF common functions and data
  3. * Copyright (c) 2000 Fabrice Bellard
  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 "libavutil/mathematics.h"
  22. #include "libavcodec/avcodec.h"
  23. #include "avformat.h"
  24. #include "avio_internal.h"
  25. #include "riff.h"
  26. #include "libavcodec/bytestream.h"
  27. #include "libavutil/avassert.h"
  28. /* Note: when encoding, the first matching tag is used, so order is
  29. important if multiple tags possible for a given codec.
  30. Note also that this list is used for more than just riff, other
  31. files use it as well.
  32. */
  33. const AVCodecTag ff_codec_bmp_tags[] = {
  34. { AV_CODEC_ID_H264, MKTAG('H', '2', '6', '4') },
  35. { AV_CODEC_ID_H264, MKTAG('h', '2', '6', '4') },
  36. { AV_CODEC_ID_H264, MKTAG('X', '2', '6', '4') },
  37. { AV_CODEC_ID_H264, MKTAG('x', '2', '6', '4') },
  38. { AV_CODEC_ID_H264, MKTAG('a', 'v', 'c', '1') },
  39. { AV_CODEC_ID_H264, MKTAG('D', 'A', 'V', 'C') },
  40. { AV_CODEC_ID_H264, MKTAG('S', 'M', 'V', '2') },
  41. { AV_CODEC_ID_H264, MKTAG('V', 'S', 'S', 'H') },
  42. { AV_CODEC_ID_H264, MKTAG('Q', '2', '6', '4') }, /* QNAP surveillance system */
  43. { AV_CODEC_ID_H263, MKTAG('H', '2', '6', '3') },
  44. { AV_CODEC_ID_H263, MKTAG('X', '2', '6', '3') },
  45. { AV_CODEC_ID_H263, MKTAG('T', '2', '6', '3') },
  46. { AV_CODEC_ID_H263, MKTAG('L', '2', '6', '3') },
  47. { AV_CODEC_ID_H263, MKTAG('V', 'X', '1', 'K') },
  48. { AV_CODEC_ID_H263, MKTAG('Z', 'y', 'G', 'o') },
  49. { AV_CODEC_ID_H263, MKTAG('M', '2', '6', '3') },
  50. { AV_CODEC_ID_H263, MKTAG('l', 's', 'v', 'm') },
  51. { AV_CODEC_ID_H263P, MKTAG('H', '2', '6', '3') },
  52. { AV_CODEC_ID_H263I, MKTAG('I', '2', '6', '3') }, /* intel h263 */
  53. { AV_CODEC_ID_H261, MKTAG('H', '2', '6', '1') },
  54. { AV_CODEC_ID_H263, MKTAG('U', '2', '6', '3') },
  55. { AV_CODEC_ID_MPEG4, MKTAG('F', 'M', 'P', '4') },
  56. { AV_CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X') },
  57. { AV_CODEC_ID_MPEG4, MKTAG('D', 'X', '5', '0') },
  58. { AV_CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'D') },
  59. { AV_CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
  60. { AV_CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
  61. { AV_CODEC_ID_MPEG4, MKTAG( 4 , 0 , 0 , 0 ) }, /* some broken avi use this */
  62. { AV_CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', '1') },
  63. { AV_CODEC_ID_MPEG4, MKTAG('B', 'L', 'Z', '0') },
  64. { AV_CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
  65. { AV_CODEC_ID_MPEG4, MKTAG('U', 'M', 'P', '4') },
  66. { AV_CODEC_ID_MPEG4, MKTAG('W', 'V', '1', 'F') },
  67. { AV_CODEC_ID_MPEG4, MKTAG('S', 'E', 'D', 'G') },
  68. { AV_CODEC_ID_MPEG4, MKTAG('R', 'M', 'P', '4') },
  69. { AV_CODEC_ID_MPEG4, MKTAG('3', 'I', 'V', '2') },
  70. { AV_CODEC_ID_MPEG4, MKTAG('W', 'A', 'W', 'V') }, /* WaWv MPEG-4 Video Codec */
  71. { AV_CODEC_ID_MPEG4, MKTAG('F', 'F', 'D', 'S') },
  72. { AV_CODEC_ID_MPEG4, MKTAG('F', 'V', 'F', 'W') },
  73. { AV_CODEC_ID_MPEG4, MKTAG('D', 'C', 'O', 'D') },
  74. { AV_CODEC_ID_MPEG4, MKTAG('M', 'V', 'X', 'M') },
  75. { AV_CODEC_ID_MPEG4, MKTAG('P', 'M', '4', 'V') },
  76. { AV_CODEC_ID_MPEG4, MKTAG('S', 'M', 'P', '4') },
  77. { AV_CODEC_ID_MPEG4, MKTAG('D', 'X', 'G', 'M') },
  78. { AV_CODEC_ID_MPEG4, MKTAG('V', 'I', 'D', 'M') },
  79. { AV_CODEC_ID_MPEG4, MKTAG('M', '4', 'T', '3') },
  80. { AV_CODEC_ID_MPEG4, MKTAG('G', 'E', 'O', 'X') },
  81. { AV_CODEC_ID_MPEG4, MKTAG('H', 'D', 'X', '4') }, /* flipped video */
  82. { AV_CODEC_ID_MPEG4, MKTAG('D', 'M', 'K', '2') },
  83. { AV_CODEC_ID_MPEG4, MKTAG('D', 'I', 'G', 'I') },
  84. { AV_CODEC_ID_MPEG4, MKTAG('I', 'N', 'M', 'C') },
  85. { AV_CODEC_ID_MPEG4, MKTAG('E', 'P', 'H', 'V') }, /* Ephv MPEG-4 */
  86. { AV_CODEC_ID_MPEG4, MKTAG('E', 'M', '4', 'A') },
  87. { AV_CODEC_ID_MPEG4, MKTAG('M', '4', 'C', 'C') }, /* Divio MPEG-4 */
  88. { AV_CODEC_ID_MPEG4, MKTAG('S', 'N', '4', '0') },
  89. { AV_CODEC_ID_MPEG4, MKTAG('V', 'S', 'P', 'X') },
  90. { AV_CODEC_ID_MPEG4, MKTAG('U', 'L', 'D', 'X') },
  91. { AV_CODEC_ID_MPEG4, MKTAG('G', 'E', 'O', 'V') },
  92. { AV_CODEC_ID_MPEG4, MKTAG('S', 'I', 'P', 'P') }, /* Samsung SHR-6040 */
  93. { AV_CODEC_ID_MPEG4, MKTAG('S', 'M', '4', 'V') },
  94. { AV_CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'X') },
  95. { AV_CODEC_ID_MPEG4, MKTAG('D', 'r', 'e', 'X') },
  96. { AV_CODEC_ID_MPEG4, MKTAG('Q', 'M', 'P', '4') }, /* QNAP Systems */
  97. { AV_CODEC_ID_MPEG4, MKTAG('P', 'L', 'V', '1') }, /* Pelco DVR MPEG-4 */
  98. { AV_CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
  99. { AV_CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '3') },
  100. { AV_CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', 'G', '3') },
  101. { AV_CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '5') },
  102. { AV_CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '6') },
  103. { AV_CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '4') },
  104. { AV_CODEC_ID_MSMPEG4V3, MKTAG('D', 'V', 'X', '3') },
  105. { AV_CODEC_ID_MSMPEG4V3, MKTAG('A', 'P', '4', '1') },
  106. { AV_CODEC_ID_MSMPEG4V3, MKTAG('C', 'O', 'L', '1') },
  107. { AV_CODEC_ID_MSMPEG4V3, MKTAG('C', 'O', 'L', '0') },
  108. { AV_CODEC_ID_MSMPEG4V2, MKTAG('M', 'P', '4', '2') },
  109. { AV_CODEC_ID_MSMPEG4V2, MKTAG('D', 'I', 'V', '2') },
  110. { AV_CODEC_ID_MSMPEG4V1, MKTAG('M', 'P', 'G', '4') },
  111. { AV_CODEC_ID_MSMPEG4V1, MKTAG('M', 'P', '4', '1') },
  112. { AV_CODEC_ID_WMV1, MKTAG('W', 'M', 'V', '1') },
  113. { AV_CODEC_ID_WMV2, MKTAG('W', 'M', 'V', '2') },
  114. { AV_CODEC_ID_WMV2, MKTAG('G', 'X', 'V', 'E') },
  115. { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'd') },
  116. { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'h', 'd') },
  117. { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'h', '1') },
  118. { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'l') },
  119. { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', '2', '5') },
  120. { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', '5', '0') },
  121. { AV_CODEC_ID_DVVIDEO, MKTAG('c', 'd', 'v', 'c') }, /* Canopus DV */
  122. { AV_CODEC_ID_DVVIDEO, MKTAG('C', 'D', 'V', 'H') }, /* Canopus DV */
  123. { AV_CODEC_ID_DVVIDEO, MKTAG('C', 'D', 'V', '5') }, /* Canopus DV */
  124. { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') },
  125. { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', 's') },
  126. { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'h', '1') },
  127. { AV_CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'i', 's') },
  128. { AV_CODEC_ID_DVVIDEO, MKTAG('p', 'd', 'v', 'c') },
  129. { AV_CODEC_ID_DVVIDEO, MKTAG('S', 'L', '2', '5') },
  130. { AV_CODEC_ID_DVVIDEO, MKTAG('S', 'L', 'D', 'V') },
  131. { AV_CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '1') },
  132. { AV_CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '2') },
  133. { AV_CODEC_ID_MPEG2VIDEO, MKTAG('m', 'p', 'g', '2') },
  134. { AV_CODEC_ID_MPEG2VIDEO, MKTAG('M', 'P', 'E', 'G') },
  135. { AV_CODEC_ID_MPEG1VIDEO, MKTAG('P', 'I', 'M', '1') },
  136. { AV_CODEC_ID_MPEG2VIDEO, MKTAG('P', 'I', 'M', '2') },
  137. { AV_CODEC_ID_MPEG1VIDEO, MKTAG('V', 'C', 'R', '2') },
  138. { AV_CODEC_ID_MPEG1VIDEO, MKTAG( 1 , 0 , 0 , 16) },
  139. { AV_CODEC_ID_MPEG2VIDEO, MKTAG( 2 , 0 , 0 , 16) },
  140. { AV_CODEC_ID_MPEG4, MKTAG( 4 , 0 , 0 , 16) },
  141. { AV_CODEC_ID_MPEG2VIDEO, MKTAG('D', 'V', 'R', ' ') },
  142. { AV_CODEC_ID_MPEG2VIDEO, MKTAG('M', 'M', 'E', 'S') },
  143. { AV_CODEC_ID_MPEG2VIDEO, MKTAG('L', 'M', 'P', '2') }, /* Lead MPEG2 in avi */
  144. { AV_CODEC_ID_MPEG2VIDEO, MKTAG('s', 'l', 'i', 'f') },
  145. { AV_CODEC_ID_MPEG2VIDEO, MKTAG('E', 'M', '2', 'V') },
  146. { AV_CODEC_ID_MPEG2VIDEO, MKTAG('M', '7', '0', '1') }, /* Matrox MPEG2 intra-only */
  147. { AV_CODEC_ID_MPEG2VIDEO, MKTAG('m', 'p', 'g', 'v') },
  148. { AV_CODEC_ID_MPEG1VIDEO, MKTAG('B', 'W', '1', '0') },
  149. { AV_CODEC_ID_MPEG1VIDEO, MKTAG('X', 'M', 'P', 'G') }, /* Xing MPEG intra only */
  150. { AV_CODEC_ID_MJPEG, MKTAG('M', 'J', 'P', 'G') },
  151. { AV_CODEC_ID_MJPEG, MKTAG('L', 'J', 'P', 'G') },
  152. { AV_CODEC_ID_MJPEG, MKTAG('d', 'm', 'b', '1') },
  153. { AV_CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'a') },
  154. { AV_CODEC_ID_LJPEG, MKTAG('L', 'J', 'P', 'G') },
  155. { AV_CODEC_ID_MJPEG, MKTAG('J', 'P', 'G', 'L') }, /* Pegasus lossless JPEG */
  156. { AV_CODEC_ID_JPEGLS, MKTAG('M', 'J', 'L', 'S') }, /* JPEG-LS custom FOURCC for avi - encoder */
  157. { AV_CODEC_ID_JPEGLS, MKTAG('M', 'J', 'P', 'G') },
  158. { AV_CODEC_ID_MJPEG, MKTAG('M', 'J', 'L', 'S') }, /* JPEG-LS custom FOURCC for avi - decoder */
  159. { AV_CODEC_ID_MJPEG, MKTAG('j', 'p', 'e', 'g') },
  160. { AV_CODEC_ID_MJPEG, MKTAG('I', 'J', 'P', 'G') },
  161. { AV_CODEC_ID_AVRN, MKTAG('A', 'V', 'R', 'n') },
  162. { AV_CODEC_ID_MJPEG, MKTAG('A', 'C', 'D', 'V') },
  163. { AV_CODEC_ID_MJPEG, MKTAG('Q', 'I', 'V', 'G') },
  164. { AV_CODEC_ID_MJPEG, MKTAG('S', 'L', 'M', 'J') }, /* SL M-JPEG */
  165. { AV_CODEC_ID_MJPEG, MKTAG('C', 'J', 'P', 'G') }, /* Creative Webcam JPEG */
  166. { AV_CODEC_ID_MJPEG, MKTAG('I', 'J', 'L', 'V') }, /* Intel JPEG Library Video Codec */
  167. { AV_CODEC_ID_MJPEG, MKTAG('M', 'V', 'J', 'P') }, /* Midvid JPEG Video Codec */
  168. { AV_CODEC_ID_MJPEG, MKTAG('A', 'V', 'I', '1') },
  169. { AV_CODEC_ID_MJPEG, MKTAG('A', 'V', 'I', '2') },
  170. { AV_CODEC_ID_MJPEG, MKTAG('M', 'T', 'S', 'J') },
  171. { AV_CODEC_ID_MJPEG, MKTAG('Z', 'J', 'P', 'G') }, /* Paradigm Matrix M-JPEG Codec */
  172. { AV_CODEC_ID_MJPEG, MKTAG('M', 'M', 'J', 'P') },
  173. { AV_CODEC_ID_HUFFYUV, MKTAG('H', 'F', 'Y', 'U') },
  174. { AV_CODEC_ID_FFVHUFF, MKTAG('F', 'F', 'V', 'H') },
  175. { AV_CODEC_ID_CYUV, MKTAG('C', 'Y', 'U', 'V') },
  176. { AV_CODEC_ID_RAWVIDEO, MKTAG( 0 , 0 , 0 , 0 ) },
  177. { AV_CODEC_ID_RAWVIDEO, MKTAG( 3 , 0 , 0 , 0 ) },
  178. { AV_CODEC_ID_RAWVIDEO, MKTAG('I', '4', '2', '0') },
  179. { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', 'U', 'Y', '2') },
  180. { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', '2', '2') },
  181. { AV_CODEC_ID_RAWVIDEO, MKTAG('V', '4', '2', '2') },
  182. { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', 'U', 'N', 'V') },
  183. { AV_CODEC_ID_RAWVIDEO, MKTAG('U', 'Y', 'N', 'V') },
  184. { AV_CODEC_ID_RAWVIDEO, MKTAG('U', 'Y', 'N', 'Y') },
  185. { AV_CODEC_ID_RAWVIDEO, MKTAG('u', 'y', 'v', '1') },
  186. { AV_CODEC_ID_RAWVIDEO, MKTAG('2', 'V', 'u', '1') },
  187. { AV_CODEC_ID_RAWVIDEO, MKTAG('2', 'v', 'u', 'y') },
  188. { AV_CODEC_ID_RAWVIDEO, MKTAG('y', 'u', 'v', 's') },
  189. { AV_CODEC_ID_RAWVIDEO, MKTAG('y', 'u', 'v', '2') },
  190. { AV_CODEC_ID_RAWVIDEO, MKTAG('P', '4', '2', '2') },
  191. { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', 'V', '1', '2') },
  192. { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', 'V', '1', '6') },
  193. { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', 'V', '2', '4') },
  194. { AV_CODEC_ID_RAWVIDEO, MKTAG('U', 'Y', 'V', 'Y') },
  195. { AV_CODEC_ID_RAWVIDEO, MKTAG('V', 'Y', 'U', 'Y') },
  196. { AV_CODEC_ID_RAWVIDEO, MKTAG('I', 'Y', 'U', 'V') },
  197. { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '8', '0', '0') },
  198. { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '8', ' ', ' ') },
  199. { AV_CODEC_ID_RAWVIDEO, MKTAG('H', 'D', 'Y', 'C') },
  200. { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', 'V', 'U', '9') },
  201. { AV_CODEC_ID_RAWVIDEO, MKTAG('V', 'D', 'T', 'Z') }, /* SoftLab-NSK VideoTizer */
  202. { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', '1', '1') },
  203. { AV_CODEC_ID_RAWVIDEO, MKTAG('N', 'V', '1', '2') },
  204. { AV_CODEC_ID_RAWVIDEO, MKTAG('N', 'V', '2', '1') },
  205. { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', '1', 'B') },
  206. { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', '4', '2', 'B') },
  207. { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', 'U', 'V', '9') },
  208. { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', 'V', 'U', '9') },
  209. { AV_CODEC_ID_RAWVIDEO, MKTAG('a', 'u', 'v', '2') },
  210. { AV_CODEC_ID_RAWVIDEO, MKTAG('Y', 'V', 'Y', 'U') },
  211. { AV_CODEC_ID_FRWU, MKTAG('F', 'R', 'W', 'U') },
  212. { AV_CODEC_ID_R10K, MKTAG('R', '1', '0', 'k') },
  213. { AV_CODEC_ID_R210, MKTAG('r', '2', '1', '0') },
  214. { AV_CODEC_ID_V210, MKTAG('v', '2', '1', '0') },
  215. { AV_CODEC_ID_V308, MKTAG('v', '3', '0', '8') },
  216. { AV_CODEC_ID_V408, MKTAG('v', '4', '0', '8') },
  217. { AV_CODEC_ID_AYUV, MKTAG('A', 'Y', 'U', 'V') },
  218. { AV_CODEC_ID_V410, MKTAG('v', '4', '1', '0') },
  219. { AV_CODEC_ID_YUV4, MKTAG('y', 'u', 'v', '4') },
  220. { AV_CODEC_ID_INDEO3, MKTAG('I', 'V', '3', '1') },
  221. { AV_CODEC_ID_INDEO3, MKTAG('I', 'V', '3', '2') },
  222. { AV_CODEC_ID_INDEO4, MKTAG('I', 'V', '4', '1') },
  223. { AV_CODEC_ID_INDEO5, MKTAG('I', 'V', '5', '0') },
  224. { AV_CODEC_ID_VP3, MKTAG('V', 'P', '3', '1') },
  225. { AV_CODEC_ID_VP3, MKTAG('V', 'P', '3', '0') },
  226. { AV_CODEC_ID_VP5, MKTAG('V', 'P', '5', '0') },
  227. { AV_CODEC_ID_VP6, MKTAG('V', 'P', '6', '0') },
  228. { AV_CODEC_ID_VP6, MKTAG('V', 'P', '6', '1') },
  229. { AV_CODEC_ID_VP6, MKTAG('V', 'P', '6', '2') },
  230. { AV_CODEC_ID_VP6F, MKTAG('V', 'P', '6', 'F') },
  231. { AV_CODEC_ID_VP6F, MKTAG('F', 'L', 'V', '4') },
  232. { AV_CODEC_ID_VP8, MKTAG('V', 'P', '8', '0') },
  233. { AV_CODEC_ID_ASV1, MKTAG('A', 'S', 'V', '1') },
  234. { AV_CODEC_ID_ASV2, MKTAG('A', 'S', 'V', '2') },
  235. { AV_CODEC_ID_VCR1, MKTAG('V', 'C', 'R', '1') },
  236. { AV_CODEC_ID_FFV1, MKTAG('F', 'F', 'V', '1') },
  237. { AV_CODEC_ID_XAN_WC4, MKTAG('X', 'x', 'a', 'n') },
  238. { AV_CODEC_ID_MIMIC, MKTAG('L', 'M', '2', '0') },
  239. { AV_CODEC_ID_MSRLE, MKTAG('m', 'r', 'l', 'e') },
  240. { AV_CODEC_ID_MSRLE, MKTAG( 1 , 0 , 0 , 0 ) },
  241. { AV_CODEC_ID_MSRLE, MKTAG( 2 , 0 , 0 , 0 ) },
  242. { AV_CODEC_ID_MSVIDEO1, MKTAG('M', 'S', 'V', 'C') },
  243. { AV_CODEC_ID_MSVIDEO1, MKTAG('m', 's', 'v', 'c') },
  244. { AV_CODEC_ID_MSVIDEO1, MKTAG('C', 'R', 'A', 'M') },
  245. { AV_CODEC_ID_MSVIDEO1, MKTAG('c', 'r', 'a', 'm') },
  246. { AV_CODEC_ID_MSVIDEO1, MKTAG('W', 'H', 'A', 'M') },
  247. { AV_CODEC_ID_MSVIDEO1, MKTAG('w', 'h', 'a', 'm') },
  248. { AV_CODEC_ID_CINEPAK, MKTAG('c', 'v', 'i', 'd') },
  249. { AV_CODEC_ID_TRUEMOTION1, MKTAG('D', 'U', 'C', 'K') },
  250. { AV_CODEC_ID_TRUEMOTION1, MKTAG('P', 'V', 'E', 'Z') },
  251. { AV_CODEC_ID_MSZH, MKTAG('M', 'S', 'Z', 'H') },
  252. { AV_CODEC_ID_ZLIB, MKTAG('Z', 'L', 'I', 'B') },
  253. { AV_CODEC_ID_SNOW, MKTAG('S', 'N', 'O', 'W') },
  254. { AV_CODEC_ID_4XM, MKTAG('4', 'X', 'M', 'V') },
  255. { AV_CODEC_ID_FLV1, MKTAG('F', 'L', 'V', '1') },
  256. { AV_CODEC_ID_FLV1, MKTAG('S', '2', '6', '3') },
  257. { AV_CODEC_ID_FLASHSV, MKTAG('F', 'S', 'V', '1') },
  258. { AV_CODEC_ID_SVQ1, MKTAG('s', 'v', 'q', '1') },
  259. { AV_CODEC_ID_TSCC, MKTAG('t', 's', 'c', 'c') },
  260. { AV_CODEC_ID_ULTI, MKTAG('U', 'L', 'T', 'I') },
  261. { AV_CODEC_ID_VIXL, MKTAG('V', 'I', 'X', 'L') },
  262. { AV_CODEC_ID_QPEG, MKTAG('Q', 'P', 'E', 'G') },
  263. { AV_CODEC_ID_QPEG, MKTAG('Q', '1', '.', '0') },
  264. { AV_CODEC_ID_QPEG, MKTAG('Q', '1', '.', '1') },
  265. { AV_CODEC_ID_WMV3, MKTAG('W', 'M', 'V', '3') },
  266. { AV_CODEC_ID_WMV3IMAGE, MKTAG('W', 'M', 'V', 'P') },
  267. { AV_CODEC_ID_VC1, MKTAG('W', 'V', 'C', '1') },
  268. { AV_CODEC_ID_VC1, MKTAG('W', 'M', 'V', 'A') },
  269. { AV_CODEC_ID_VC1IMAGE, MKTAG('W', 'V', 'P', '2') },
  270. { AV_CODEC_ID_LOCO, MKTAG('L', 'O', 'C', 'O') },
  271. { AV_CODEC_ID_WNV1, MKTAG('W', 'N', 'V', '1') },
  272. { AV_CODEC_ID_WNV1, MKTAG('Y', 'U', 'V', '8') },
  273. { AV_CODEC_ID_AASC, MKTAG('A', 'A', 'S', '4') },
  274. { AV_CODEC_ID_AASC, MKTAG('A', 'A', 'S', 'C') },
  275. { AV_CODEC_ID_INDEO2, MKTAG('R', 'T', '2', '1') },
  276. { AV_CODEC_ID_FRAPS, MKTAG('F', 'P', 'S', '1') },
  277. { AV_CODEC_ID_THEORA, MKTAG('t', 'h', 'e', 'o') },
  278. { AV_CODEC_ID_TRUEMOTION2, MKTAG('T', 'M', '2', '0') },
  279. { AV_CODEC_ID_CSCD, MKTAG('C', 'S', 'C', 'D') },
  280. { AV_CODEC_ID_ZMBV, MKTAG('Z', 'M', 'B', 'V') },
  281. { AV_CODEC_ID_KMVC, MKTAG('K', 'M', 'V', 'C') },
  282. { AV_CODEC_ID_CAVS, MKTAG('C', 'A', 'V', 'S') },
  283. { AV_CODEC_ID_JPEG2000, MKTAG('m', 'j', 'p', '2') },
  284. { AV_CODEC_ID_JPEG2000, MKTAG('M', 'J', '2', 'C') },
  285. { AV_CODEC_ID_JPEG2000, MKTAG('L', 'J', '2', 'C') },
  286. { AV_CODEC_ID_JPEG2000, MKTAG('L', 'J', '2', 'K') },
  287. { AV_CODEC_ID_JPEG2000, MKTAG('I', 'P', 'J', '2') },
  288. { AV_CODEC_ID_VMNC, MKTAG('V', 'M', 'n', 'c') },
  289. { AV_CODEC_ID_TARGA, MKTAG('t', 'g', 'a', ' ') },
  290. { AV_CODEC_ID_PNG, MKTAG('M', 'P', 'N', 'G') },
  291. { AV_CODEC_ID_PNG, MKTAG('P', 'N', 'G', '1') },
  292. { AV_CODEC_ID_CLJR, MKTAG('C', 'L', 'J', 'R') },
  293. { AV_CODEC_ID_DIRAC, MKTAG('d', 'r', 'a', 'c') },
  294. { AV_CODEC_ID_RPZA, MKTAG('a', 'z', 'p', 'r') },
  295. { AV_CODEC_ID_RPZA, MKTAG('R', 'P', 'Z', 'A') },
  296. { AV_CODEC_ID_RPZA, MKTAG('r', 'p', 'z', 'a') },
  297. { AV_CODEC_ID_SP5X, MKTAG('S', 'P', '5', '4') },
  298. { AV_CODEC_ID_AURA, MKTAG('A', 'U', 'R', 'A') },
  299. { AV_CODEC_ID_AURA2, MKTAG('A', 'U', 'R', '2') },
  300. { AV_CODEC_ID_DPX, MKTAG('d', 'p', 'x', ' ') },
  301. { AV_CODEC_ID_KGV1, MKTAG('K', 'G', 'V', '1') },
  302. { AV_CODEC_ID_LAGARITH, MKTAG('L', 'A', 'G', 'S') },
  303. { AV_CODEC_ID_G2M, MKTAG('G', '2', 'M', '2') },
  304. { AV_CODEC_ID_G2M, MKTAG('G', '2', 'M', '3') },
  305. { AV_CODEC_ID_G2M, MKTAG('G', '2', 'M', '4') },
  306. { AV_CODEC_ID_AMV, MKTAG('A', 'M', 'V', 'F') },
  307. { AV_CODEC_ID_UTVIDEO, MKTAG('U', 'L', 'R', 'A') },
  308. { AV_CODEC_ID_UTVIDEO, MKTAG('U', 'L', 'R', 'G') },
  309. { AV_CODEC_ID_UTVIDEO, MKTAG('U', 'L', 'Y', '0') },
  310. { AV_CODEC_ID_UTVIDEO, MKTAG('U', 'L', 'Y', '2') },
  311. { AV_CODEC_ID_VBLE, MKTAG('V', 'B', 'L', 'E') },
  312. { AV_CODEC_ID_ESCAPE130, MKTAG('E', '1', '3', '0') },
  313. { AV_CODEC_ID_DXTORY, MKTAG('x', 't', 'o', 'r') },
  314. { AV_CODEC_ID_ZEROCODEC, MKTAG('Z', 'E', 'C', 'O') },
  315. { AV_CODEC_ID_Y41P, MKTAG('Y', '4', '1', 'P') },
  316. { AV_CODEC_ID_FLIC, MKTAG('A', 'F', 'L', 'C') },
  317. { AV_CODEC_ID_EXR, MKTAG('e', 'x', 'r', ' ') },
  318. { AV_CODEC_ID_MSS1, MKTAG('M', 'S', 'S', '1') },
  319. { AV_CODEC_ID_MSA1, MKTAG('M', 'S', 'A', '1') },
  320. { AV_CODEC_ID_TSCC2, MKTAG('T', 'S', 'C', '2') },
  321. { AV_CODEC_ID_MTS2, MKTAG('M', 'T', 'S', '2') },
  322. { AV_CODEC_ID_CLLC, MKTAG('C', 'L', 'L', 'C') },
  323. { AV_CODEC_ID_MSS2, MKTAG('M', 'S', 'S', '2') },
  324. { AV_CODEC_ID_SVQ3, MKTAG('S', 'V', 'Q', '3') },
  325. { AV_CODEC_ID_NONE, 0 }
  326. };
  327. const AVCodecTag ff_codec_wav_tags[] = {
  328. { AV_CODEC_ID_PCM_S16LE, 0x0001 },
  329. { AV_CODEC_ID_PCM_U8, 0x0001 }, /* must come after s16le in this list */
  330. { AV_CODEC_ID_PCM_S24LE, 0x0001 },
  331. { AV_CODEC_ID_PCM_S32LE, 0x0001 },
  332. { AV_CODEC_ID_ADPCM_MS, 0x0002 },
  333. { AV_CODEC_ID_PCM_F32LE, 0x0003 },
  334. { AV_CODEC_ID_PCM_F64LE, 0x0003 }, /* must come after f32le in this list */
  335. { AV_CODEC_ID_PCM_ALAW, 0x0006 },
  336. { AV_CODEC_ID_PCM_MULAW, 0x0007 },
  337. { AV_CODEC_ID_WMAVOICE, 0x000A },
  338. { AV_CODEC_ID_ADPCM_IMA_WAV, 0x0011 },
  339. { AV_CODEC_ID_PCM_ZORK, 0x0011 }, /* must come after adpcm_ima_wav in this list */
  340. { AV_CODEC_ID_ADPCM_YAMAHA, 0x0020 },
  341. { AV_CODEC_ID_TRUESPEECH, 0x0022 },
  342. { AV_CODEC_ID_GSM_MS, 0x0031 },
  343. { AV_CODEC_ID_AMR_NB, 0x0038 }, /* rogue format number */
  344. { AV_CODEC_ID_G723_1, 0x0042 },
  345. { AV_CODEC_ID_ADPCM_G726, 0x0045 },
  346. { AV_CODEC_ID_MP2, 0x0050 },
  347. { AV_CODEC_ID_MP3, 0x0055 },
  348. { AV_CODEC_ID_AMR_NB, 0x0057 },
  349. { AV_CODEC_ID_AMR_WB, 0x0058 },
  350. { AV_CODEC_ID_ADPCM_IMA_DK4, 0x0061 }, /* rogue format number */
  351. { AV_CODEC_ID_ADPCM_IMA_DK3, 0x0062 }, /* rogue format number */
  352. { AV_CODEC_ID_ADPCM_IMA_WAV, 0x0069 },
  353. { AV_CODEC_ID_VOXWARE, 0x0075 },
  354. { AV_CODEC_ID_AAC, 0x00ff },
  355. { AV_CODEC_ID_SIPR, 0x0130 },
  356. { AV_CODEC_ID_WMAV1, 0x0160 },
  357. { AV_CODEC_ID_WMAV2, 0x0161 },
  358. { AV_CODEC_ID_WMAPRO, 0x0162 },
  359. { AV_CODEC_ID_WMALOSSLESS, 0x0163 },
  360. { AV_CODEC_ID_ADPCM_CT, 0x0200 },
  361. { AV_CODEC_ID_ATRAC3, 0x0270 },
  362. { AV_CODEC_ID_ADPCM_G722, 0x028F },
  363. { AV_CODEC_ID_IMC, 0x0401 },
  364. { AV_CODEC_ID_IAC, 0x0402 },
  365. { AV_CODEC_ID_GSM_MS, 0x1500 },
  366. { AV_CODEC_ID_TRUESPEECH, 0x1501 },
  367. { AV_CODEC_ID_AAC, 0x1600 }, /* ADTS AAC */
  368. { AV_CODEC_ID_AAC_LATM, 0x1602 },
  369. { AV_CODEC_ID_AC3, 0x2000 },
  370. { AV_CODEC_ID_DTS, 0x2001 },
  371. { AV_CODEC_ID_SONIC, 0x2048 },
  372. { AV_CODEC_ID_SONIC_LS, 0x2048 },
  373. { AV_CODEC_ID_PCM_MULAW, 0x6c75 },
  374. { AV_CODEC_ID_AAC, 0x706d },
  375. { AV_CODEC_ID_AAC, 0x4143 },
  376. { AV_CODEC_ID_G723_1, 0xA100 },
  377. { AV_CODEC_ID_AAC, 0xA106 },
  378. { AV_CODEC_ID_SPEEX, 0xA109 },
  379. { AV_CODEC_ID_FLAC, 0xF1AC },
  380. { AV_CODEC_ID_ADPCM_SWF, ('S'<<8)+'F' },
  381. { AV_CODEC_ID_VORBIS, ('V'<<8)+'o' }, //HACK/FIXME, does vorbis in WAV/AVI have an (in)official id?
  382. /* FIXME: All of the IDs below are not 16 bit and thus illegal. */
  383. // for NuppelVideo (nuv.c)
  384. { AV_CODEC_ID_PCM_S16LE, MKTAG('R', 'A', 'W', 'A') },
  385. { AV_CODEC_ID_MP3, MKTAG('L', 'A', 'M', 'E') },
  386. { AV_CODEC_ID_MP3, MKTAG('M', 'P', '3', ' ') },
  387. { AV_CODEC_ID_NONE, 0 },
  388. };
  389. const AVCodecGuid ff_codec_wav_guids[] = {
  390. {AV_CODEC_ID_AC3, {0x2C,0x80,0x6D,0xE0,0x46,0xDB,0xCF,0x11,0xB4,0xD1,0x00,0x80,0x5F,0x6C,0xBB,0xEA}},
  391. {AV_CODEC_ID_ATRAC3P, {0xBF,0xAA,0x23,0xE9,0x58,0xCB,0x71,0x44,0xA1,0x19,0xFF,0xFA,0x01,0xE4,0xCE,0x62}},
  392. {AV_CODEC_ID_EAC3, {0xAF,0x87,0xFB,0xA7,0x02,0x2D,0xFB,0x42,0xA4,0xD4,0x05,0xCD,0x93,0x84,0x3B,0xDD}},
  393. {AV_CODEC_ID_MP2, {0x2B,0x80,0x6D,0xE0,0x46,0xDB,0xCF,0x11,0xB4,0xD1,0x00,0x80,0x5F,0x6C,0xBB,0xEA}},
  394. {AV_CODEC_ID_NONE}
  395. };
  396. const AVMetadataConv ff_riff_info_conv[] = {
  397. { "IART", "artist" },
  398. { "ICMT", "comment" },
  399. { "ICOP", "copyright" },
  400. { "ICRD", "date" },
  401. { "IGNR", "genre" },
  402. { "ILNG", "language" },
  403. { "INAM", "title" },
  404. { "IPRD", "album" },
  405. { "IPRT", "track" },
  406. { "ISFT", "encoder" },
  407. { "ISMP", "timecode" },
  408. { "ITCH", "encoded_by"},
  409. { 0 },
  410. };
  411. const char ff_riff_tags[][5] = {
  412. "IARL", "IART", "ICMS", "ICMT", "ICOP", "ICRD", "ICRP", "IDIM", "IDPI",
  413. "IENG", "IGNR", "IKEY", "ILGT", "ILNG", "IMED", "INAM", "IPLT", "IPRD",
  414. "IPRT", "ISBJ", "ISFT", "ISHP", "ISMP", "ISRC", "ISRF", "ITCH",
  415. {0}
  416. };
  417. #if CONFIG_MUXERS
  418. int64_t ff_start_tag(AVIOContext *pb, const char *tag)
  419. {
  420. ffio_wfourcc(pb, tag);
  421. avio_wl32(pb, 0);
  422. return avio_tell(pb);
  423. }
  424. void ff_end_tag(AVIOContext *pb, int64_t start)
  425. {
  426. int64_t pos;
  427. pos = avio_tell(pb);
  428. avio_seek(pb, start - 4, SEEK_SET);
  429. avio_wl32(pb, (uint32_t)(pos - start));
  430. avio_seek(pb, pos, SEEK_SET);
  431. }
  432. /* WAVEFORMATEX header */
  433. /* returns the size or -1 on error */
  434. int ff_put_wav_header(AVIOContext *pb, AVCodecContext *enc)
  435. {
  436. int bps, blkalign, bytespersec, frame_size;
  437. int hdrsize = 18;
  438. int waveformatextensible;
  439. uint8_t temp[256];
  440. uint8_t *riff_extradata= temp;
  441. uint8_t *riff_extradata_start= temp;
  442. if(!enc->codec_tag || enc->codec_tag > 0xffff)
  443. return -1;
  444. /* We use the known constant frame size for the codec if known, otherwise
  445. fallback to using AVCodecContext.frame_size, which is not as reliable
  446. for indicating packet duration */
  447. frame_size = av_get_audio_frame_duration(enc, 0);
  448. if (!frame_size)
  449. frame_size = enc->frame_size;
  450. waveformatextensible = (enc->channels > 2 && enc->channel_layout)
  451. || enc->sample_rate > 48000
  452. || av_get_bits_per_sample(enc->codec_id) > 16;
  453. if (waveformatextensible) {
  454. avio_wl16(pb, 0xfffe);
  455. } else {
  456. avio_wl16(pb, enc->codec_tag);
  457. }
  458. avio_wl16(pb, enc->channels);
  459. avio_wl32(pb, enc->sample_rate);
  460. if (enc->codec_id == CODEC_ID_ATRAC3 ||
  461. enc->codec_id == CODEC_ID_G723_1 ||
  462. enc->codec_id == CODEC_ID_GSM_MS ||
  463. enc->codec_id == CODEC_ID_MP2 ||
  464. enc->codec_id == CODEC_ID_MP3) {
  465. bps = 0;
  466. } else {
  467. if (!(bps = av_get_bits_per_sample(enc->codec_id))) {
  468. if (enc->bits_per_coded_sample)
  469. bps = enc->bits_per_coded_sample;
  470. else
  471. bps = 16; // default to 16
  472. }
  473. }
  474. if(bps != enc->bits_per_coded_sample && enc->bits_per_coded_sample){
  475. av_log(enc, AV_LOG_WARNING, "requested bits_per_coded_sample (%d) and actually stored (%d) differ\n", enc->bits_per_coded_sample, bps);
  476. }
  477. if (enc->codec_id == AV_CODEC_ID_MP2 || enc->codec_id == AV_CODEC_ID_MP3) {
  478. /* this is wrong, but it seems many demuxers do not work if this is set
  479. correctly */
  480. blkalign = frame_size;
  481. //blkalign = 144 * enc->bit_rate/enc->sample_rate;
  482. } else if (enc->codec_id == AV_CODEC_ID_AC3) {
  483. blkalign = 3840; //maximum bytes per frame
  484. } else if (enc->codec_id == AV_CODEC_ID_AAC) {
  485. blkalign = 768 * enc->channels; //maximum bytes per frame
  486. } else if (enc->codec_id == AV_CODEC_ID_G723_1) {
  487. blkalign = 24;
  488. } else if (enc->block_align != 0) { /* specified by the codec */
  489. blkalign = enc->block_align;
  490. } else
  491. blkalign = bps * enc->channels / av_gcd(8, bps);
  492. if (enc->codec_id == AV_CODEC_ID_PCM_U8 ||
  493. enc->codec_id == AV_CODEC_ID_PCM_S24LE ||
  494. enc->codec_id == AV_CODEC_ID_PCM_S32LE ||
  495. enc->codec_id == AV_CODEC_ID_PCM_F32LE ||
  496. enc->codec_id == AV_CODEC_ID_PCM_F64LE ||
  497. enc->codec_id == AV_CODEC_ID_PCM_S16LE) {
  498. bytespersec = enc->sample_rate * blkalign;
  499. } else if (enc->codec_id == AV_CODEC_ID_G723_1) {
  500. bytespersec = 800;
  501. } else {
  502. bytespersec = enc->bit_rate / 8;
  503. }
  504. avio_wl32(pb, bytespersec); /* bytes per second */
  505. avio_wl16(pb, blkalign); /* block align */
  506. avio_wl16(pb, bps); /* bits per sample */
  507. if (enc->codec_id == AV_CODEC_ID_MP3) {
  508. hdrsize += 12;
  509. bytestream_put_le16(&riff_extradata, 1); /* wID */
  510. bytestream_put_le32(&riff_extradata, 2); /* fdwFlags */
  511. bytestream_put_le16(&riff_extradata, 1152); /* nBlockSize */
  512. bytestream_put_le16(&riff_extradata, 1); /* nFramesPerBlock */
  513. bytestream_put_le16(&riff_extradata, 1393); /* nCodecDelay */
  514. } else if (enc->codec_id == AV_CODEC_ID_MP2) {
  515. hdrsize += 22;
  516. bytestream_put_le16(&riff_extradata, 2); /* fwHeadLayer */
  517. bytestream_put_le32(&riff_extradata, enc->bit_rate); /* dwHeadBitrate */
  518. bytestream_put_le16(&riff_extradata, enc->channels == 2 ? 1 : 8); /* fwHeadMode */
  519. bytestream_put_le16(&riff_extradata, 0); /* fwHeadModeExt */
  520. bytestream_put_le16(&riff_extradata, 1); /* wHeadEmphasis */
  521. bytestream_put_le16(&riff_extradata, 16); /* fwHeadFlags */
  522. bytestream_put_le32(&riff_extradata, 0); /* dwPTSLow */
  523. bytestream_put_le32(&riff_extradata, 0); /* dwPTSHigh */
  524. } else if (enc->codec_id == AV_CODEC_ID_G723_1) {
  525. hdrsize += 20;
  526. bytestream_put_le32(&riff_extradata, 0x9ace0002); /* extradata needed for msacm g723.1 codec */
  527. bytestream_put_le32(&riff_extradata, 0xaea2f732);
  528. bytestream_put_le16(&riff_extradata, 0xacde);
  529. } else if (enc->codec_id == AV_CODEC_ID_GSM_MS || enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
  530. hdrsize += 2;
  531. bytestream_put_le16(&riff_extradata, frame_size); /* wSamplesPerBlock */
  532. } else if(enc->extradata_size){
  533. riff_extradata_start= enc->extradata;
  534. riff_extradata= enc->extradata + enc->extradata_size;
  535. hdrsize += enc->extradata_size;
  536. }
  537. if(waveformatextensible) { /* write WAVEFORMATEXTENSIBLE extensions */
  538. hdrsize += 22;
  539. avio_wl16(pb, riff_extradata - riff_extradata_start + 22); /* 22 is WAVEFORMATEXTENSIBLE size */
  540. avio_wl16(pb, bps); /* ValidBitsPerSample || SamplesPerBlock || Reserved */
  541. avio_wl32(pb, enc->channel_layout); /* dwChannelMask */
  542. avio_wl32(pb, enc->codec_tag); /* GUID + next 3 */
  543. avio_wl32(pb, 0x00100000);
  544. avio_wl32(pb, 0xAA000080);
  545. avio_wl32(pb, 0x719B3800);
  546. } else {
  547. avio_wl16(pb, riff_extradata - riff_extradata_start); /* cbSize */
  548. }
  549. avio_write(pb, riff_extradata_start, riff_extradata - riff_extradata_start);
  550. if(hdrsize&1){
  551. hdrsize++;
  552. avio_w8(pb, 0);
  553. }
  554. return hdrsize;
  555. }
  556. /* BITMAPINFOHEADER header */
  557. void ff_put_bmp_header(AVIOContext *pb, AVCodecContext *enc, const AVCodecTag *tags, int for_asf)
  558. {
  559. avio_wl32(pb, 40 + enc->extradata_size); /* size */
  560. avio_wl32(pb, enc->width);
  561. //We always store RGB TopDown
  562. avio_wl32(pb, enc->codec_tag ? enc->height : -enc->height);
  563. avio_wl16(pb, 1); /* planes */
  564. avio_wl16(pb, enc->bits_per_coded_sample ? enc->bits_per_coded_sample : 24); /* depth */
  565. /* compression type */
  566. avio_wl32(pb, enc->codec_tag);
  567. avio_wl32(pb, (enc->width * enc->height * (enc->bits_per_coded_sample ? enc->bits_per_coded_sample : 24)+7) / 8);
  568. avio_wl32(pb, 0);
  569. avio_wl32(pb, 0);
  570. avio_wl32(pb, 0);
  571. avio_wl32(pb, 0);
  572. avio_write(pb, enc->extradata, enc->extradata_size);
  573. if (!for_asf && enc->extradata_size & 1)
  574. avio_w8(pb, 0);
  575. }
  576. void ff_parse_specific_params(AVCodecContext *stream, int *au_rate, int *au_ssize, int *au_scale)
  577. {
  578. int gcd;
  579. int audio_frame_size;
  580. /* We use the known constant frame size for the codec if known, otherwise
  581. fallback to using AVCodecContext.frame_size, which is not as reliable
  582. for indicating packet duration */
  583. audio_frame_size = av_get_audio_frame_duration(stream, 0);
  584. if (!audio_frame_size)
  585. audio_frame_size = stream->frame_size;
  586. *au_ssize= stream->block_align;
  587. if (audio_frame_size && stream->sample_rate) {
  588. *au_scale = audio_frame_size;
  589. *au_rate= stream->sample_rate;
  590. }else if(stream->codec_type == AVMEDIA_TYPE_VIDEO ||
  591. stream->codec_type == AVMEDIA_TYPE_DATA ||
  592. stream->codec_type == AVMEDIA_TYPE_SUBTITLE){
  593. *au_scale= stream->time_base.num;
  594. *au_rate = stream->time_base.den;
  595. }else{
  596. *au_scale= stream->block_align ? stream->block_align*8 : 8;
  597. *au_rate = stream->bit_rate ? stream->bit_rate : 8*stream->sample_rate;
  598. }
  599. gcd= av_gcd(*au_scale, *au_rate);
  600. *au_scale /= gcd;
  601. *au_rate /= gcd;
  602. }
  603. void ff_riff_write_info_tag(AVIOContext *pb, const char *tag, const char *str)
  604. {
  605. int len = strlen(str);
  606. if (len > 0) {
  607. len++;
  608. ffio_wfourcc(pb, tag);
  609. avio_wl32(pb, len);
  610. avio_put_str(pb, str);
  611. if (len & 1)
  612. avio_w8(pb, 0);
  613. }
  614. }
  615. static int riff_has_valid_tags(AVFormatContext *s)
  616. {
  617. int i;
  618. for (i = 0; *ff_riff_tags[i]; i++) {
  619. if (av_dict_get(s->metadata, ff_riff_tags[i], NULL, AV_DICT_MATCH_CASE))
  620. return 1;
  621. }
  622. return 0;
  623. }
  624. void ff_riff_write_info(AVFormatContext *s)
  625. {
  626. AVIOContext *pb = s->pb;
  627. int i;
  628. int64_t list_pos;
  629. AVDictionaryEntry *t = NULL;
  630. ff_metadata_conv(&s->metadata, ff_riff_info_conv, NULL);
  631. /* writing empty LIST is not nice and may cause problems */
  632. if (!riff_has_valid_tags(s))
  633. return;
  634. list_pos = ff_start_tag(pb, "LIST");
  635. ffio_wfourcc(pb, "INFO");
  636. for (i = 0; *ff_riff_tags[i]; i++) {
  637. if ((t = av_dict_get(s->metadata, ff_riff_tags[i], NULL, AV_DICT_MATCH_CASE)))
  638. ff_riff_write_info_tag(s->pb, t->key, t->value);
  639. }
  640. ff_end_tag(pb, list_pos);
  641. }
  642. #endif //CONFIG_MUXERS
  643. #if CONFIG_DEMUXERS
  644. /* We could be given one of the three possible structures here:
  645. * WAVEFORMAT, PCMWAVEFORMAT or WAVEFORMATEX. Each structure
  646. * is an expansion of the previous one with the fields added
  647. * at the bottom. PCMWAVEFORMAT adds 'WORD wBitsPerSample' and
  648. * WAVEFORMATEX adds 'WORD cbSize' and basically makes itself
  649. * an openended structure.
  650. */
  651. int ff_get_wav_header(AVIOContext *pb, AVCodecContext *codec, int size)
  652. {
  653. int id;
  654. id = avio_rl16(pb);
  655. codec->codec_type = AVMEDIA_TYPE_AUDIO;
  656. codec->channels = avio_rl16(pb);
  657. codec->sample_rate = avio_rl32(pb);
  658. codec->bit_rate = avio_rl32(pb) * 8;
  659. codec->block_align = avio_rl16(pb);
  660. if (size == 14) { /* We're dealing with plain vanilla WAVEFORMAT */
  661. codec->bits_per_coded_sample = 8;
  662. }else
  663. codec->bits_per_coded_sample = avio_rl16(pb);
  664. if (id == 0xFFFE) {
  665. codec->codec_tag = 0;
  666. } else {
  667. codec->codec_tag = id;
  668. codec->codec_id = ff_wav_codec_get_id(id, codec->bits_per_coded_sample);
  669. }
  670. if (size >= 18) { /* We're obviously dealing with WAVEFORMATEX */
  671. int cbSize = avio_rl16(pb); /* cbSize */
  672. size -= 18;
  673. cbSize = FFMIN(size, cbSize);
  674. if (cbSize >= 22 && id == 0xfffe) { /* WAVEFORMATEXTENSIBLE */
  675. ff_asf_guid subformat;
  676. int bps = avio_rl16(pb);
  677. if (bps)
  678. codec->bits_per_coded_sample = bps;
  679. codec->channel_layout = avio_rl32(pb); /* dwChannelMask */
  680. ff_get_guid(pb, &subformat);
  681. if (!memcmp(subformat + 4, (const uint8_t[]){FF_MEDIASUBTYPE_BASE_GUID}, 12)) {
  682. codec->codec_tag = AV_RL32(subformat);
  683. codec->codec_id = ff_wav_codec_get_id(codec->codec_tag, codec->bits_per_coded_sample);
  684. } else {
  685. codec->codec_id = ff_codec_guid_get_id(ff_codec_wav_guids, subformat);
  686. if (!codec->codec_id)
  687. av_log(codec, AV_LOG_WARNING, "unknown subformat:"FF_PRI_GUID"\n", FF_ARG_GUID(subformat));
  688. }
  689. cbSize -= 22;
  690. size -= 22;
  691. }
  692. codec->extradata_size = cbSize;
  693. if (cbSize > 0) {
  694. av_free(codec->extradata);
  695. codec->extradata = av_mallocz(codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  696. if (!codec->extradata)
  697. return AVERROR(ENOMEM);
  698. avio_read(pb, codec->extradata, codec->extradata_size);
  699. size -= cbSize;
  700. }
  701. /* It is possible for the chunk to contain garbage at the end */
  702. if (size > 0)
  703. avio_skip(pb, size);
  704. }
  705. if (codec->codec_id == AV_CODEC_ID_AAC_LATM) {
  706. /* channels and sample_rate values are those prior to applying SBR and/or PS */
  707. codec->channels = 0;
  708. codec->sample_rate = 0;
  709. }
  710. /* override bits_per_coded_sample for G.726 */
  711. if (codec->codec_id == AV_CODEC_ID_ADPCM_G726 && codec->sample_rate)
  712. codec->bits_per_coded_sample = codec->bit_rate / codec->sample_rate;
  713. return 0;
  714. }
  715. enum AVCodecID ff_wav_codec_get_id(unsigned int tag, int bps)
  716. {
  717. enum AVCodecID id;
  718. id = ff_codec_get_id(ff_codec_wav_tags, tag);
  719. if (id <= 0)
  720. return id;
  721. /* handle specific u8 codec */
  722. if (id == AV_CODEC_ID_PCM_S16LE && bps == 8)
  723. id = AV_CODEC_ID_PCM_U8;
  724. if (id == AV_CODEC_ID_PCM_S16LE && bps == 24)
  725. id = AV_CODEC_ID_PCM_S24LE;
  726. if (id == AV_CODEC_ID_PCM_S16LE && bps == 32)
  727. id = AV_CODEC_ID_PCM_S32LE;
  728. if (id == AV_CODEC_ID_PCM_F32LE && bps == 64)
  729. id = AV_CODEC_ID_PCM_F64LE;
  730. if (id == AV_CODEC_ID_ADPCM_IMA_WAV && bps == 8)
  731. id = AV_CODEC_ID_PCM_ZORK;
  732. return id;
  733. }
  734. int ff_get_bmp_header(AVIOContext *pb, AVStream *st, unsigned *esize)
  735. {
  736. int tag1;
  737. if(esize) *esize = avio_rl32(pb);
  738. else avio_rl32(pb);
  739. st->codec->width = avio_rl32(pb);
  740. st->codec->height = (int32_t)avio_rl32(pb);
  741. avio_rl16(pb); /* planes */
  742. st->codec->bits_per_coded_sample= avio_rl16(pb); /* depth */
  743. tag1 = avio_rl32(pb);
  744. avio_rl32(pb); /* ImageSize */
  745. avio_rl32(pb); /* XPelsPerMeter */
  746. avio_rl32(pb); /* YPelsPerMeter */
  747. avio_rl32(pb); /* ClrUsed */
  748. avio_rl32(pb); /* ClrImportant */
  749. return tag1;
  750. }
  751. void ff_get_guid(AVIOContext *s, ff_asf_guid *g)
  752. {
  753. av_assert0(sizeof(*g) == 16); //compiler will optimize this out
  754. if (avio_read(s, *g, sizeof(*g)) < (int)sizeof(*g))
  755. memset(*g, 0, sizeof(*g));
  756. }
  757. enum AVCodecID ff_codec_guid_get_id(const AVCodecGuid *guids, ff_asf_guid guid)
  758. {
  759. int i;
  760. for (i = 0; guids[i].id != AV_CODEC_ID_NONE; i++) {
  761. if (!ff_guidcmp(guids[i].guid, guid))
  762. return guids[i].id;
  763. }
  764. return AV_CODEC_ID_NONE;
  765. }
  766. int ff_read_riff_info(AVFormatContext *s, int64_t size)
  767. {
  768. int64_t start, end, cur;
  769. AVIOContext *pb = s->pb;
  770. start = avio_tell(pb);
  771. end = start + size;
  772. while ((cur = avio_tell(pb)) >= 0 && cur <= end - 8 /* = tag + size */) {
  773. uint32_t chunk_code;
  774. int64_t chunk_size;
  775. char key[5] = {0};
  776. char *value;
  777. chunk_code = avio_rl32(pb);
  778. chunk_size = avio_rl32(pb);
  779. if (chunk_size > end || end - chunk_size < cur || chunk_size == UINT_MAX) {
  780. avio_seek(pb, -9, SEEK_CUR);
  781. chunk_code = avio_rl32(pb);
  782. chunk_size = avio_rl32(pb);
  783. if (chunk_size > end || end - chunk_size < cur || chunk_size == UINT_MAX) {
  784. av_log(s, AV_LOG_ERROR, "too big INFO subchunk\n");
  785. return AVERROR_INVALIDDATA;
  786. }
  787. }
  788. chunk_size += (chunk_size & 1);
  789. value = av_mallocz(chunk_size + 1);
  790. if (!value) {
  791. av_log(s, AV_LOG_ERROR, "out of memory, unable to read INFO tag\n");
  792. return AVERROR(ENOMEM);
  793. }
  794. AV_WL32(key, chunk_code);
  795. if (avio_read(pb, value, chunk_size) != chunk_size) {
  796. av_log(s, AV_LOG_WARNING, "premature end of file while reading INFO tag\n");
  797. }
  798. av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
  799. }
  800. return 0;
  801. }
  802. #endif // CONFIG_DEMUXERS