utils.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * utils.
  25. */
  26. #include "config.h"
  27. #include "libavutil/avassert.h"
  28. #include "libavutil/channel_layout.h"
  29. #include "libavutil/intreadwrite.h"
  30. #include "libavutil/mem.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "libavutil/imgutils.h"
  33. #include "libavutil/pixfmt.h"
  34. #include "avcodec.h"
  35. #include "codec.h"
  36. #include "codec_internal.h"
  37. #include "hwconfig.h"
  38. #include "thread.h"
  39. #include "threadframe.h"
  40. #include "internal.h"
  41. #include "put_bits.h"
  42. #include "startcode.h"
  43. #include <stdlib.h>
  44. #include <limits.h>
  45. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  46. {
  47. uint8_t **p = ptr;
  48. if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
  49. av_freep(p);
  50. *size = 0;
  51. return;
  52. }
  53. av_fast_mallocz(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE);
  54. if (*p)
  55. memset(*p + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  56. }
  57. void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
  58. {
  59. uint8_t **p = ptr;
  60. if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
  61. av_freep(p);
  62. *size = 0;
  63. return;
  64. }
  65. av_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE);
  66. if (*p)
  67. memset(*p, 0, min_size + AV_INPUT_BUFFER_PADDING_SIZE);
  68. }
  69. int av_codec_is_encoder(const AVCodec *avcodec)
  70. {
  71. const FFCodec *const codec = ffcodec(avcodec);
  72. return codec && (codec->encode_sub || codec->encode2 || codec->receive_packet);
  73. }
  74. int av_codec_is_decoder(const AVCodec *avcodec)
  75. {
  76. const FFCodec *const codec = ffcodec(avcodec);
  77. return codec && (codec->decode || codec->decode_sub || codec->receive_frame);
  78. }
  79. int ff_set_dimensions(AVCodecContext *s, int width, int height)
  80. {
  81. int ret = av_image_check_size2(width, height, s->max_pixels, AV_PIX_FMT_NONE, 0, s);
  82. if (ret < 0)
  83. width = height = 0;
  84. s->coded_width = width;
  85. s->coded_height = height;
  86. s->width = AV_CEIL_RSHIFT(width, s->lowres);
  87. s->height = AV_CEIL_RSHIFT(height, s->lowres);
  88. return ret;
  89. }
  90. int ff_set_sar(AVCodecContext *avctx, AVRational sar)
  91. {
  92. int ret = av_image_check_sar(avctx->width, avctx->height, sar);
  93. if (ret < 0) {
  94. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %d/%d\n",
  95. sar.num, sar.den);
  96. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  97. return ret;
  98. } else {
  99. avctx->sample_aspect_ratio = sar;
  100. }
  101. return 0;
  102. }
  103. int ff_side_data_update_matrix_encoding(AVFrame *frame,
  104. enum AVMatrixEncoding matrix_encoding)
  105. {
  106. AVFrameSideData *side_data;
  107. enum AVMatrixEncoding *data;
  108. side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
  109. if (!side_data)
  110. side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
  111. sizeof(enum AVMatrixEncoding));
  112. if (!side_data)
  113. return AVERROR(ENOMEM);
  114. data = (enum AVMatrixEncoding*)side_data->data;
  115. *data = matrix_encoding;
  116. return 0;
  117. }
  118. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  119. int linesize_align[AV_NUM_DATA_POINTERS])
  120. {
  121. int i;
  122. int w_align = 1;
  123. int h_align = 1;
  124. AVPixFmtDescriptor const *desc = av_pix_fmt_desc_get(s->pix_fmt);
  125. if (desc) {
  126. w_align = 1 << desc->log2_chroma_w;
  127. h_align = 1 << desc->log2_chroma_h;
  128. }
  129. switch (s->pix_fmt) {
  130. case AV_PIX_FMT_YUV420P:
  131. case AV_PIX_FMT_YUYV422:
  132. case AV_PIX_FMT_YVYU422:
  133. case AV_PIX_FMT_UYVY422:
  134. case AV_PIX_FMT_YUV422P:
  135. case AV_PIX_FMT_YUV440P:
  136. case AV_PIX_FMT_YUV444P:
  137. case AV_PIX_FMT_GBRP:
  138. case AV_PIX_FMT_GBRAP:
  139. case AV_PIX_FMT_GRAY8:
  140. case AV_PIX_FMT_GRAY16BE:
  141. case AV_PIX_FMT_GRAY16LE:
  142. case AV_PIX_FMT_YUVJ420P:
  143. case AV_PIX_FMT_YUVJ422P:
  144. case AV_PIX_FMT_YUVJ440P:
  145. case AV_PIX_FMT_YUVJ444P:
  146. case AV_PIX_FMT_YUVA420P:
  147. case AV_PIX_FMT_YUVA422P:
  148. case AV_PIX_FMT_YUVA444P:
  149. case AV_PIX_FMT_YUV420P9LE:
  150. case AV_PIX_FMT_YUV420P9BE:
  151. case AV_PIX_FMT_YUV420P10LE:
  152. case AV_PIX_FMT_YUV420P10BE:
  153. case AV_PIX_FMT_YUV420P12LE:
  154. case AV_PIX_FMT_YUV420P12BE:
  155. case AV_PIX_FMT_YUV420P14LE:
  156. case AV_PIX_FMT_YUV420P14BE:
  157. case AV_PIX_FMT_YUV420P16LE:
  158. case AV_PIX_FMT_YUV420P16BE:
  159. case AV_PIX_FMT_YUVA420P9LE:
  160. case AV_PIX_FMT_YUVA420P9BE:
  161. case AV_PIX_FMT_YUVA420P10LE:
  162. case AV_PIX_FMT_YUVA420P10BE:
  163. case AV_PIX_FMT_YUVA420P16LE:
  164. case AV_PIX_FMT_YUVA420P16BE:
  165. case AV_PIX_FMT_YUV422P9LE:
  166. case AV_PIX_FMT_YUV422P9BE:
  167. case AV_PIX_FMT_YUV422P10LE:
  168. case AV_PIX_FMT_YUV422P10BE:
  169. case AV_PIX_FMT_YUV422P12LE:
  170. case AV_PIX_FMT_YUV422P12BE:
  171. case AV_PIX_FMT_YUV422P14LE:
  172. case AV_PIX_FMT_YUV422P14BE:
  173. case AV_PIX_FMT_YUV422P16LE:
  174. case AV_PIX_FMT_YUV422P16BE:
  175. case AV_PIX_FMT_YUVA422P9LE:
  176. case AV_PIX_FMT_YUVA422P9BE:
  177. case AV_PIX_FMT_YUVA422P10LE:
  178. case AV_PIX_FMT_YUVA422P10BE:
  179. case AV_PIX_FMT_YUVA422P12LE:
  180. case AV_PIX_FMT_YUVA422P12BE:
  181. case AV_PIX_FMT_YUVA422P16LE:
  182. case AV_PIX_FMT_YUVA422P16BE:
  183. case AV_PIX_FMT_YUV440P10LE:
  184. case AV_PIX_FMT_YUV440P10BE:
  185. case AV_PIX_FMT_YUV440P12LE:
  186. case AV_PIX_FMT_YUV440P12BE:
  187. case AV_PIX_FMT_YUV444P9LE:
  188. case AV_PIX_FMT_YUV444P9BE:
  189. case AV_PIX_FMT_YUV444P10LE:
  190. case AV_PIX_FMT_YUV444P10BE:
  191. case AV_PIX_FMT_YUV444P12LE:
  192. case AV_PIX_FMT_YUV444P12BE:
  193. case AV_PIX_FMT_YUV444P14LE:
  194. case AV_PIX_FMT_YUV444P14BE:
  195. case AV_PIX_FMT_YUV444P16LE:
  196. case AV_PIX_FMT_YUV444P16BE:
  197. case AV_PIX_FMT_YUVA444P9LE:
  198. case AV_PIX_FMT_YUVA444P9BE:
  199. case AV_PIX_FMT_YUVA444P10LE:
  200. case AV_PIX_FMT_YUVA444P10BE:
  201. case AV_PIX_FMT_YUVA444P12LE:
  202. case AV_PIX_FMT_YUVA444P12BE:
  203. case AV_PIX_FMT_YUVA444P16LE:
  204. case AV_PIX_FMT_YUVA444P16BE:
  205. case AV_PIX_FMT_GBRP9LE:
  206. case AV_PIX_FMT_GBRP9BE:
  207. case AV_PIX_FMT_GBRP10LE:
  208. case AV_PIX_FMT_GBRP10BE:
  209. case AV_PIX_FMT_GBRP12LE:
  210. case AV_PIX_FMT_GBRP12BE:
  211. case AV_PIX_FMT_GBRP14LE:
  212. case AV_PIX_FMT_GBRP14BE:
  213. case AV_PIX_FMT_GBRP16LE:
  214. case AV_PIX_FMT_GBRP16BE:
  215. case AV_PIX_FMT_GBRAP12LE:
  216. case AV_PIX_FMT_GBRAP12BE:
  217. case AV_PIX_FMT_GBRAP16LE:
  218. case AV_PIX_FMT_GBRAP16BE:
  219. w_align = 16; //FIXME assume 16 pixel per macroblock
  220. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  221. break;
  222. case AV_PIX_FMT_YUV411P:
  223. case AV_PIX_FMT_YUVJ411P:
  224. case AV_PIX_FMT_UYYVYY411:
  225. w_align = 32;
  226. h_align = 16 * 2;
  227. break;
  228. case AV_PIX_FMT_YUV410P:
  229. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  230. w_align = 64;
  231. h_align = 64;
  232. }
  233. break;
  234. case AV_PIX_FMT_RGB555:
  235. if (s->codec_id == AV_CODEC_ID_RPZA) {
  236. w_align = 4;
  237. h_align = 4;
  238. }
  239. if (s->codec_id == AV_CODEC_ID_INTERPLAY_VIDEO) {
  240. w_align = 8;
  241. h_align = 8;
  242. }
  243. break;
  244. case AV_PIX_FMT_PAL8:
  245. case AV_PIX_FMT_BGR8:
  246. case AV_PIX_FMT_RGB8:
  247. if (s->codec_id == AV_CODEC_ID_SMC ||
  248. s->codec_id == AV_CODEC_ID_CINEPAK) {
  249. w_align = 4;
  250. h_align = 4;
  251. }
  252. if (s->codec_id == AV_CODEC_ID_JV ||
  253. s->codec_id == AV_CODEC_ID_ARGO ||
  254. s->codec_id == AV_CODEC_ID_INTERPLAY_VIDEO) {
  255. w_align = 8;
  256. h_align = 8;
  257. }
  258. if (s->codec_id == AV_CODEC_ID_MJPEG ||
  259. s->codec_id == AV_CODEC_ID_MJPEGB ||
  260. s->codec_id == AV_CODEC_ID_LJPEG ||
  261. s->codec_id == AV_CODEC_ID_SMVJPEG ||
  262. s->codec_id == AV_CODEC_ID_AMV ||
  263. s->codec_id == AV_CODEC_ID_SP5X ||
  264. s->codec_id == AV_CODEC_ID_JPEGLS) {
  265. w_align = 8;
  266. h_align = 2*8;
  267. }
  268. break;
  269. case AV_PIX_FMT_BGR24:
  270. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  271. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  272. w_align = 4;
  273. h_align = 4;
  274. }
  275. break;
  276. case AV_PIX_FMT_RGB24:
  277. if (s->codec_id == AV_CODEC_ID_CINEPAK) {
  278. w_align = 4;
  279. h_align = 4;
  280. }
  281. break;
  282. case AV_PIX_FMT_BGR0:
  283. if (s->codec_id == AV_CODEC_ID_ARGO) {
  284. w_align = 8;
  285. h_align = 8;
  286. }
  287. break;
  288. default:
  289. break;
  290. }
  291. if (s->codec_id == AV_CODEC_ID_IFF_ILBM) {
  292. w_align = FFMAX(w_align, 8);
  293. }
  294. *width = FFALIGN(*width, w_align);
  295. *height = FFALIGN(*height, h_align);
  296. if (s->codec_id == AV_CODEC_ID_H264 || s->lowres ||
  297. s->codec_id == AV_CODEC_ID_VP5 || s->codec_id == AV_CODEC_ID_VP6 ||
  298. s->codec_id == AV_CODEC_ID_VP6F || s->codec_id == AV_CODEC_ID_VP6A
  299. ) {
  300. // some of the optimized chroma MC reads one line too much
  301. // which is also done in mpeg decoders with lowres > 0
  302. *height += 2;
  303. // H.264 uses edge emulation for out of frame motion vectors, for this
  304. // it requires a temporary area large enough to hold a 21x21 block,
  305. // increasing witdth ensure that the temporary area is large enough,
  306. // the next rounded up width is 32
  307. *width = FFMAX(*width, 32);
  308. }
  309. for (i = 0; i < 4; i++)
  310. linesize_align[i] = STRIDE_ALIGN;
  311. }
  312. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  313. {
  314. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  315. int chroma_shift = desc->log2_chroma_w;
  316. int linesize_align[AV_NUM_DATA_POINTERS];
  317. int align;
  318. avcodec_align_dimensions2(s, width, height, linesize_align);
  319. align = FFMAX(linesize_align[0], linesize_align[3]);
  320. linesize_align[1] <<= chroma_shift;
  321. linesize_align[2] <<= chroma_shift;
  322. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  323. *width = FFALIGN(*width, align);
  324. }
  325. int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
  326. {
  327. if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
  328. return AVERROR(EINVAL);
  329. pos--;
  330. *xpos = (pos&1) * 128;
  331. *ypos = ((pos>>1)^(pos<4)) * 128;
  332. return 0;
  333. }
  334. enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos)
  335. {
  336. int pos, xout, yout;
  337. for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
  338. if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
  339. return pos;
  340. }
  341. return AVCHROMA_LOC_UNSPECIFIED;
  342. }
  343. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  344. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  345. int buf_size, int align)
  346. {
  347. int ch, planar, needed_size, ret = 0;
  348. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  349. frame->nb_samples, sample_fmt,
  350. align);
  351. if (buf_size < needed_size)
  352. return AVERROR(EINVAL);
  353. planar = av_sample_fmt_is_planar(sample_fmt);
  354. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  355. if (!FF_ALLOCZ_TYPED_ARRAY(frame->extended_data, nb_channels))
  356. return AVERROR(ENOMEM);
  357. } else {
  358. frame->extended_data = frame->data;
  359. }
  360. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  361. (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
  362. sample_fmt, align)) < 0) {
  363. if (frame->extended_data != frame->data)
  364. av_freep(&frame->extended_data);
  365. return ret;
  366. }
  367. if (frame->extended_data != frame->data) {
  368. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  369. frame->data[ch] = frame->extended_data[ch];
  370. }
  371. return ret;
  372. }
  373. void ff_color_frame(AVFrame *frame, const int c[4])
  374. {
  375. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  376. int p, y;
  377. av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
  378. for (p = 0; p<desc->nb_components; p++) {
  379. uint8_t *dst = frame->data[p];
  380. int is_chroma = p == 1 || p == 2;
  381. int bytes = is_chroma ? AV_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
  382. int height = is_chroma ? AV_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
  383. if (desc->comp[0].depth >= 9) {
  384. ((uint16_t*)dst)[0] = c[p];
  385. av_memcpy_backptr(dst + 2, 2, bytes - 2);
  386. dst += frame->linesize[p];
  387. for (y = 1; y < height; y++) {
  388. memcpy(dst, frame->data[p], 2*bytes);
  389. dst += frame->linesize[p];
  390. }
  391. } else {
  392. for (y = 0; y < height; y++) {
  393. memset(dst, c[p], bytes);
  394. dst += frame->linesize[p];
  395. }
  396. }
  397. }
  398. }
  399. int avpriv_codec_get_cap_skip_frame_fill_param(const AVCodec *codec){
  400. return !!(ffcodec(codec)->caps_internal & FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM);
  401. }
  402. const char *avcodec_get_name(enum AVCodecID id)
  403. {
  404. const AVCodecDescriptor *cd;
  405. const AVCodec *codec;
  406. if (id == AV_CODEC_ID_NONE)
  407. return "none";
  408. cd = avcodec_descriptor_get(id);
  409. if (cd)
  410. return cd->name;
  411. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  412. codec = avcodec_find_decoder(id);
  413. if (codec)
  414. return codec->name;
  415. codec = avcodec_find_encoder(id);
  416. if (codec)
  417. return codec->name;
  418. return "unknown_codec";
  419. }
  420. const char *av_get_profile_name(const AVCodec *codec, int profile)
  421. {
  422. const AVProfile *p;
  423. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  424. return NULL;
  425. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  426. if (p->profile == profile)
  427. return p->name;
  428. return NULL;
  429. }
  430. const char *avcodec_profile_name(enum AVCodecID codec_id, int profile)
  431. {
  432. const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
  433. const AVProfile *p;
  434. if (profile == FF_PROFILE_UNKNOWN || !desc || !desc->profiles)
  435. return NULL;
  436. for (p = desc->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  437. if (p->profile == profile)
  438. return p->name;
  439. return NULL;
  440. }
  441. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  442. {
  443. switch (codec_id) {
  444. case AV_CODEC_ID_8SVX_EXP:
  445. case AV_CODEC_ID_8SVX_FIB:
  446. case AV_CODEC_ID_ADPCM_ARGO:
  447. case AV_CODEC_ID_ADPCM_CT:
  448. case AV_CODEC_ID_ADPCM_IMA_ALP:
  449. case AV_CODEC_ID_ADPCM_IMA_AMV:
  450. case AV_CODEC_ID_ADPCM_IMA_APC:
  451. case AV_CODEC_ID_ADPCM_IMA_APM:
  452. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  453. case AV_CODEC_ID_ADPCM_IMA_OKI:
  454. case AV_CODEC_ID_ADPCM_IMA_WS:
  455. case AV_CODEC_ID_ADPCM_IMA_SSI:
  456. case AV_CODEC_ID_ADPCM_G722:
  457. case AV_CODEC_ID_ADPCM_YAMAHA:
  458. case AV_CODEC_ID_ADPCM_AICA:
  459. return 4;
  460. case AV_CODEC_ID_DSD_LSBF:
  461. case AV_CODEC_ID_DSD_MSBF:
  462. case AV_CODEC_ID_DSD_LSBF_PLANAR:
  463. case AV_CODEC_ID_DSD_MSBF_PLANAR:
  464. case AV_CODEC_ID_PCM_ALAW:
  465. case AV_CODEC_ID_PCM_MULAW:
  466. case AV_CODEC_ID_PCM_VIDC:
  467. case AV_CODEC_ID_PCM_S8:
  468. case AV_CODEC_ID_PCM_S8_PLANAR:
  469. case AV_CODEC_ID_PCM_SGA:
  470. case AV_CODEC_ID_PCM_U8:
  471. case AV_CODEC_ID_SDX2_DPCM:
  472. case AV_CODEC_ID_DERF_DPCM:
  473. return 8;
  474. case AV_CODEC_ID_PCM_S16BE:
  475. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  476. case AV_CODEC_ID_PCM_S16LE:
  477. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  478. case AV_CODEC_ID_PCM_U16BE:
  479. case AV_CODEC_ID_PCM_U16LE:
  480. return 16;
  481. case AV_CODEC_ID_PCM_S24DAUD:
  482. case AV_CODEC_ID_PCM_S24BE:
  483. case AV_CODEC_ID_PCM_S24LE:
  484. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  485. case AV_CODEC_ID_PCM_U24BE:
  486. case AV_CODEC_ID_PCM_U24LE:
  487. return 24;
  488. case AV_CODEC_ID_PCM_S32BE:
  489. case AV_CODEC_ID_PCM_S32LE:
  490. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  491. case AV_CODEC_ID_PCM_U32BE:
  492. case AV_CODEC_ID_PCM_U32LE:
  493. case AV_CODEC_ID_PCM_F32BE:
  494. case AV_CODEC_ID_PCM_F32LE:
  495. case AV_CODEC_ID_PCM_F24LE:
  496. case AV_CODEC_ID_PCM_F16LE:
  497. return 32;
  498. case AV_CODEC_ID_PCM_F64BE:
  499. case AV_CODEC_ID_PCM_F64LE:
  500. case AV_CODEC_ID_PCM_S64BE:
  501. case AV_CODEC_ID_PCM_S64LE:
  502. return 64;
  503. default:
  504. return 0;
  505. }
  506. }
  507. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  508. {
  509. static const enum AVCodecID map[][2] = {
  510. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  511. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  512. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  513. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  514. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  515. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  516. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  517. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  518. [AV_SAMPLE_FMT_S64P] = { AV_CODEC_ID_PCM_S64LE, AV_CODEC_ID_PCM_S64BE },
  519. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  520. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  521. };
  522. if (fmt < 0 || fmt >= FF_ARRAY_ELEMS(map))
  523. return AV_CODEC_ID_NONE;
  524. if (be < 0 || be > 1)
  525. be = AV_NE(1, 0);
  526. return map[fmt][be];
  527. }
  528. int av_get_bits_per_sample(enum AVCodecID codec_id)
  529. {
  530. switch (codec_id) {
  531. case AV_CODEC_ID_DFPWM:
  532. return 1;
  533. case AV_CODEC_ID_ADPCM_SBPRO_2:
  534. return 2;
  535. case AV_CODEC_ID_ADPCM_SBPRO_3:
  536. return 3;
  537. case AV_CODEC_ID_ADPCM_SBPRO_4:
  538. case AV_CODEC_ID_ADPCM_IMA_WAV:
  539. case AV_CODEC_ID_ADPCM_IMA_QT:
  540. case AV_CODEC_ID_ADPCM_SWF:
  541. case AV_CODEC_ID_ADPCM_MS:
  542. return 4;
  543. default:
  544. return av_get_exact_bits_per_sample(codec_id);
  545. }
  546. }
  547. static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
  548. uint32_t tag, int bits_per_coded_sample, int64_t bitrate,
  549. uint8_t * extradata, int frame_size, int frame_bytes)
  550. {
  551. int bps = av_get_exact_bits_per_sample(id);
  552. int framecount = (ba > 0 && frame_bytes / ba > 0) ? frame_bytes / ba : 1;
  553. /* codecs with an exact constant bits per sample */
  554. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  555. return (frame_bytes * 8LL) / (bps * ch);
  556. bps = bits_per_coded_sample;
  557. /* codecs with a fixed packet duration */
  558. switch (id) {
  559. case AV_CODEC_ID_ADPCM_ADX: return 32;
  560. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  561. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  562. case AV_CODEC_ID_AMR_NB:
  563. case AV_CODEC_ID_EVRC:
  564. case AV_CODEC_ID_GSM:
  565. case AV_CODEC_ID_QCELP:
  566. case AV_CODEC_ID_RA_288: return 160;
  567. case AV_CODEC_ID_AMR_WB:
  568. case AV_CODEC_ID_GSM_MS: return 320;
  569. case AV_CODEC_ID_MP1: return 384;
  570. case AV_CODEC_ID_ATRAC1: return 512;
  571. case AV_CODEC_ID_ATRAC9:
  572. case AV_CODEC_ID_ATRAC3:
  573. if (framecount > INT_MAX/1024)
  574. return 0;
  575. return 1024 * framecount;
  576. case AV_CODEC_ID_ATRAC3P: return 2048;
  577. case AV_CODEC_ID_MP2:
  578. case AV_CODEC_ID_MUSEPACK7: return 1152;
  579. case AV_CODEC_ID_AC3: return 1536;
  580. }
  581. if (sr > 0) {
  582. /* calc from sample rate */
  583. if (id == AV_CODEC_ID_TTA)
  584. return 256 * sr / 245;
  585. else if (id == AV_CODEC_ID_DST)
  586. return 588 * sr / 44100;
  587. else if (id == AV_CODEC_ID_BINKAUDIO_DCT) {
  588. if (sr / 22050 > 22)
  589. return 0;
  590. return (480 << (sr / 22050));
  591. }
  592. if (id == AV_CODEC_ID_MP3)
  593. return sr <= 24000 ? 576 : 1152;
  594. }
  595. if (ba > 0) {
  596. /* calc from block_align */
  597. if (id == AV_CODEC_ID_SIPR) {
  598. switch (ba) {
  599. case 20: return 160;
  600. case 19: return 144;
  601. case 29: return 288;
  602. case 37: return 480;
  603. }
  604. } else if (id == AV_CODEC_ID_ILBC) {
  605. switch (ba) {
  606. case 38: return 160;
  607. case 50: return 240;
  608. }
  609. }
  610. }
  611. if (frame_bytes > 0) {
  612. /* calc from frame_bytes only */
  613. if (id == AV_CODEC_ID_TRUESPEECH)
  614. return 240 * (frame_bytes / 32);
  615. if (id == AV_CODEC_ID_NELLYMOSER)
  616. return 256 * (frame_bytes / 64);
  617. if (id == AV_CODEC_ID_RA_144)
  618. return 160 * (frame_bytes / 20);
  619. if (bps > 0) {
  620. /* calc from frame_bytes and bits_per_coded_sample */
  621. if (id == AV_CODEC_ID_ADPCM_G726 || id == AV_CODEC_ID_ADPCM_G726LE)
  622. return frame_bytes * 8 / bps;
  623. }
  624. if (ch > 0 && ch < INT_MAX/16) {
  625. /* calc from frame_bytes and channels */
  626. switch (id) {
  627. case AV_CODEC_ID_FASTAUDIO:
  628. return frame_bytes / (40 * ch) * 256;
  629. case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
  630. return (frame_bytes - 4 * ch) / (128 * ch) * 256;
  631. case AV_CODEC_ID_ADPCM_AFC:
  632. return frame_bytes / (9 * ch) * 16;
  633. case AV_CODEC_ID_ADPCM_PSX:
  634. case AV_CODEC_ID_ADPCM_DTK:
  635. frame_bytes /= 16 * ch;
  636. if (frame_bytes > INT_MAX / 28)
  637. return 0;
  638. return frame_bytes * 28;
  639. case AV_CODEC_ID_ADPCM_4XM:
  640. case AV_CODEC_ID_ADPCM_IMA_ACORN:
  641. case AV_CODEC_ID_ADPCM_IMA_DAT4:
  642. case AV_CODEC_ID_ADPCM_IMA_ISS:
  643. return (frame_bytes - 4 * ch) * 2 / ch;
  644. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  645. return (frame_bytes - 4) * 2 / ch;
  646. case AV_CODEC_ID_ADPCM_IMA_AMV:
  647. return (frame_bytes - 8) * 2;
  648. case AV_CODEC_ID_ADPCM_THP:
  649. case AV_CODEC_ID_ADPCM_THP_LE:
  650. if (extradata)
  651. return frame_bytes * 14LL / (8 * ch);
  652. break;
  653. case AV_CODEC_ID_ADPCM_XA:
  654. return (frame_bytes / 128) * 224 / ch;
  655. case AV_CODEC_ID_INTERPLAY_DPCM:
  656. return (frame_bytes - 6 - ch) / ch;
  657. case AV_CODEC_ID_ROQ_DPCM:
  658. return (frame_bytes - 8) / ch;
  659. case AV_CODEC_ID_XAN_DPCM:
  660. return (frame_bytes - 2 * ch) / ch;
  661. case AV_CODEC_ID_MACE3:
  662. return 3 * frame_bytes / ch;
  663. case AV_CODEC_ID_MACE6:
  664. return 6 * frame_bytes / ch;
  665. case AV_CODEC_ID_PCM_LXF:
  666. return 2 * (frame_bytes / (5 * ch));
  667. case AV_CODEC_ID_IAC:
  668. case AV_CODEC_ID_IMC:
  669. return 4 * frame_bytes / ch;
  670. }
  671. if (tag) {
  672. /* calc from frame_bytes, channels, and codec_tag */
  673. if (id == AV_CODEC_ID_SOL_DPCM) {
  674. if (tag == 3)
  675. return frame_bytes / ch;
  676. else
  677. return frame_bytes * 2 / ch;
  678. }
  679. }
  680. if (ba > 0) {
  681. /* calc from frame_bytes, channels, and block_align */
  682. int blocks = frame_bytes / ba;
  683. int64_t tmp = 0;
  684. switch (id) {
  685. case AV_CODEC_ID_ADPCM_IMA_WAV:
  686. if (bps < 2 || bps > 5)
  687. return 0;
  688. tmp = blocks * (1LL + (ba - 4 * ch) / (bps * ch) * 8);
  689. break;
  690. case AV_CODEC_ID_ADPCM_IMA_DK3:
  691. tmp = blocks * (((ba - 16LL) * 2 / 3 * 4) / ch);
  692. break;
  693. case AV_CODEC_ID_ADPCM_IMA_DK4:
  694. tmp = blocks * (1 + (ba - 4LL * ch) * 2 / ch);
  695. break;
  696. case AV_CODEC_ID_ADPCM_IMA_RAD:
  697. tmp = blocks * ((ba - 4LL * ch) * 2 / ch);
  698. break;
  699. case AV_CODEC_ID_ADPCM_MS:
  700. tmp = blocks * (2 + (ba - 7LL * ch) * 2LL / ch);
  701. break;
  702. case AV_CODEC_ID_ADPCM_MTAF:
  703. tmp = blocks * (ba - 16LL) * 2 / ch;
  704. break;
  705. }
  706. if (tmp) {
  707. if (tmp != (int)tmp)
  708. return 0;
  709. return tmp;
  710. }
  711. }
  712. if (bps > 0) {
  713. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  714. switch (id) {
  715. case AV_CODEC_ID_PCM_DVD:
  716. if(bps<4 || frame_bytes<3)
  717. return 0;
  718. return 2 * ((frame_bytes - 3) / ((bps * 2 / 8) * ch));
  719. case AV_CODEC_ID_PCM_BLURAY:
  720. if(bps<4 || frame_bytes<4)
  721. return 0;
  722. return (frame_bytes - 4) / ((FFALIGN(ch, 2) * bps) / 8);
  723. case AV_CODEC_ID_S302M:
  724. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  725. }
  726. }
  727. }
  728. }
  729. /* Fall back on using frame_size */
  730. if (frame_size > 1 && frame_bytes)
  731. return frame_size;
  732. //For WMA we currently have no other means to calculate duration thus we
  733. //do it here by assuming CBR, which is true for all known cases.
  734. if (bitrate > 0 && frame_bytes > 0 && sr > 0 && ba > 1) {
  735. if (id == AV_CODEC_ID_WMAV1 || id == AV_CODEC_ID_WMAV2)
  736. return (frame_bytes * 8LL * sr) / bitrate;
  737. }
  738. return 0;
  739. }
  740. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  741. {
  742. int channels = avctx->ch_layout.nb_channels;
  743. int duration;
  744. #if FF_API_OLD_CHANNEL_LAYOUT
  745. FF_DISABLE_DEPRECATION_WARNINGS
  746. if (!channels)
  747. channels = avctx->channels;
  748. FF_ENABLE_DEPRECATION_WARNINGS
  749. #endif
  750. duration = get_audio_frame_duration(avctx->codec_id, avctx->sample_rate,
  751. channels, avctx->block_align,
  752. avctx->codec_tag, avctx->bits_per_coded_sample,
  753. avctx->bit_rate, avctx->extradata, avctx->frame_size,
  754. frame_bytes);
  755. return FFMAX(0, duration);
  756. }
  757. int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
  758. {
  759. int channels = par->ch_layout.nb_channels;
  760. int duration;
  761. #if FF_API_OLD_CHANNEL_LAYOUT
  762. FF_DISABLE_DEPRECATION_WARNINGS
  763. if (!channels)
  764. channels = par->channels;
  765. FF_ENABLE_DEPRECATION_WARNINGS
  766. #endif
  767. duration = get_audio_frame_duration(par->codec_id, par->sample_rate,
  768. channels, par->block_align,
  769. par->codec_tag, par->bits_per_coded_sample,
  770. par->bit_rate, par->extradata, par->frame_size,
  771. frame_bytes);
  772. return FFMAX(0, duration);
  773. }
  774. #if !HAVE_THREADS
  775. int ff_thread_init(AVCodecContext *s)
  776. {
  777. return -1;
  778. }
  779. #endif
  780. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  781. {
  782. unsigned int n = 0;
  783. while (v >= 0xff) {
  784. *s++ = 0xff;
  785. v -= 0xff;
  786. n++;
  787. }
  788. *s = v;
  789. n++;
  790. return n;
  791. }
  792. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  793. {
  794. int i;
  795. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  796. return i;
  797. }
  798. const AVCodecHWConfig *avcodec_get_hw_config(const AVCodec *avcodec, int index)
  799. {
  800. const FFCodec *const codec = ffcodec(avcodec);
  801. int i;
  802. if (!codec->hw_configs || index < 0)
  803. return NULL;
  804. for (i = 0; i <= index; i++)
  805. if (!codec->hw_configs[i])
  806. return NULL;
  807. return &codec->hw_configs[index]->public;
  808. }
  809. int ff_thread_ref_frame(ThreadFrame *dst, const ThreadFrame *src)
  810. {
  811. int ret;
  812. dst->owner[0] = src->owner[0];
  813. dst->owner[1] = src->owner[1];
  814. ret = av_frame_ref(dst->f, src->f);
  815. if (ret < 0)
  816. return ret;
  817. av_assert0(!dst->progress);
  818. if (src->progress &&
  819. !(dst->progress = av_buffer_ref(src->progress))) {
  820. ff_thread_release_ext_buffer(dst->owner[0], dst);
  821. return AVERROR(ENOMEM);
  822. }
  823. return 0;
  824. }
  825. #if !HAVE_THREADS
  826. enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  827. {
  828. return ff_get_format(avctx, fmt);
  829. }
  830. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
  831. {
  832. return ff_get_buffer(avctx, f, flags);
  833. }
  834. int ff_thread_get_ext_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  835. {
  836. f->owner[0] = f->owner[1] = avctx;
  837. return ff_get_buffer(avctx, f->f, flags);
  838. }
  839. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  840. {
  841. if (f)
  842. av_frame_unref(f);
  843. }
  844. void ff_thread_release_ext_buffer(AVCodecContext *avctx, ThreadFrame *f)
  845. {
  846. f->owner[0] = f->owner[1] = NULL;
  847. if (f->f)
  848. av_frame_unref(f->f);
  849. }
  850. void ff_thread_finish_setup(AVCodecContext *avctx)
  851. {
  852. }
  853. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  854. {
  855. }
  856. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  857. {
  858. }
  859. int ff_thread_can_start_frame(AVCodecContext *avctx)
  860. {
  861. return 1;
  862. }
  863. int ff_alloc_entries(AVCodecContext *avctx, int count)
  864. {
  865. return 0;
  866. }
  867. void ff_reset_entries(AVCodecContext *avctx)
  868. {
  869. }
  870. void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
  871. {
  872. }
  873. void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
  874. {
  875. }
  876. #endif
  877. const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
  878. const uint8_t *end,
  879. uint32_t *av_restrict state)
  880. {
  881. int i;
  882. av_assert0(p <= end);
  883. if (p >= end)
  884. return end;
  885. for (i = 0; i < 3; i++) {
  886. uint32_t tmp = *state << 8;
  887. *state = tmp + *(p++);
  888. if (tmp == 0x100 || p == end)
  889. return p;
  890. }
  891. while (p < end) {
  892. if (p[-1] > 1 ) p += 3;
  893. else if (p[-2] ) p += 2;
  894. else if (p[-3]|(p[-1]-1)) p++;
  895. else {
  896. p++;
  897. break;
  898. }
  899. }
  900. p = FFMIN(p, end) - 4;
  901. *state = AV_RB32(p);
  902. return p + 4;
  903. }
  904. AVCPBProperties *av_cpb_properties_alloc(size_t *size)
  905. {
  906. AVCPBProperties *props = av_mallocz(sizeof(AVCPBProperties));
  907. if (!props)
  908. return NULL;
  909. if (size)
  910. *size = sizeof(*props);
  911. props->vbv_delay = UINT64_MAX;
  912. return props;
  913. }
  914. AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx)
  915. {
  916. AVPacketSideData *tmp;
  917. AVCPBProperties *props;
  918. size_t size;
  919. int i;
  920. for (i = 0; i < avctx->nb_coded_side_data; i++)
  921. if (avctx->coded_side_data[i].type == AV_PKT_DATA_CPB_PROPERTIES)
  922. return (AVCPBProperties *)avctx->coded_side_data[i].data;
  923. props = av_cpb_properties_alloc(&size);
  924. if (!props)
  925. return NULL;
  926. tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
  927. if (!tmp) {
  928. av_freep(&props);
  929. return NULL;
  930. }
  931. avctx->coded_side_data = tmp;
  932. avctx->nb_coded_side_data++;
  933. avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
  934. avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
  935. avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
  936. return props;
  937. }
  938. static unsigned bcd2uint(uint8_t bcd)
  939. {
  940. unsigned low = bcd & 0xf;
  941. unsigned high = bcd >> 4;
  942. if (low > 9 || high > 9)
  943. return 0;
  944. return low + 10*high;
  945. }
  946. int ff_alloc_timecode_sei(const AVFrame *frame, AVRational rate, size_t prefix_len,
  947. void **data, size_t *sei_size)
  948. {
  949. AVFrameSideData *sd = NULL;
  950. uint8_t *sei_data;
  951. PutBitContext pb;
  952. uint32_t *tc;
  953. int m;
  954. if (frame)
  955. sd = av_frame_get_side_data(frame, AV_FRAME_DATA_S12M_TIMECODE);
  956. if (!sd) {
  957. *data = NULL;
  958. return 0;
  959. }
  960. tc = (uint32_t*)sd->data;
  961. m = tc[0] & 3;
  962. *sei_size = sizeof(uint32_t) * 4;
  963. *data = av_mallocz(*sei_size + prefix_len);
  964. if (!*data)
  965. return AVERROR(ENOMEM);
  966. sei_data = (uint8_t*)*data + prefix_len;
  967. init_put_bits(&pb, sei_data, *sei_size);
  968. put_bits(&pb, 2, m); // num_clock_ts
  969. for (int j = 1; j <= m; j++) {
  970. uint32_t tcsmpte = tc[j];
  971. unsigned hh = bcd2uint(tcsmpte & 0x3f); // 6-bit hours
  972. unsigned mm = bcd2uint(tcsmpte>>8 & 0x7f); // 7-bit minutes
  973. unsigned ss = bcd2uint(tcsmpte>>16 & 0x7f); // 7-bit seconds
  974. unsigned ff = bcd2uint(tcsmpte>>24 & 0x3f); // 6-bit frames
  975. unsigned drop = tcsmpte & 1<<30 && !0; // 1-bit drop if not arbitrary bit
  976. /* Calculate frame number of HEVC by SMPTE ST 12-1:2014 Sec 12.2 if rate > 30FPS */
  977. if (av_cmp_q(rate, (AVRational) {30, 1}) == 1) {
  978. unsigned pc;
  979. ff *= 2;
  980. if (av_cmp_q(rate, (AVRational) {50, 1}) == 0)
  981. pc = !!(tcsmpte & 1 << 7);
  982. else
  983. pc = !!(tcsmpte & 1 << 23);
  984. ff = (ff + pc) & 0x7f;
  985. }
  986. put_bits(&pb, 1, 1); // clock_timestamp_flag
  987. put_bits(&pb, 1, 1); // units_field_based_flag
  988. put_bits(&pb, 5, 0); // counting_type
  989. put_bits(&pb, 1, 1); // full_timestamp_flag
  990. put_bits(&pb, 1, 0); // discontinuity_flag
  991. put_bits(&pb, 1, drop);
  992. put_bits(&pb, 9, ff);
  993. put_bits(&pb, 6, ss);
  994. put_bits(&pb, 6, mm);
  995. put_bits(&pb, 5, hh);
  996. put_bits(&pb, 5, 0);
  997. }
  998. flush_put_bits(&pb);
  999. return 0;
  1000. }
  1001. int64_t ff_guess_coded_bitrate(AVCodecContext *avctx)
  1002. {
  1003. AVRational framerate = avctx->framerate;
  1004. int bits_per_coded_sample = avctx->bits_per_coded_sample;
  1005. int64_t bitrate;
  1006. if (!(framerate.num && framerate.den))
  1007. framerate = av_inv_q(avctx->time_base);
  1008. if (!(framerate.num && framerate.den))
  1009. return 0;
  1010. if (!bits_per_coded_sample) {
  1011. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  1012. bits_per_coded_sample = av_get_bits_per_pixel(desc);
  1013. }
  1014. bitrate = (int64_t)bits_per_coded_sample * avctx->width * avctx->height *
  1015. framerate.num / framerate.den;
  1016. return bitrate;
  1017. }
  1018. int ff_int_from_list_or_default(void *ctx, const char * val_name, int val,
  1019. const int * array_valid_values, int default_value)
  1020. {
  1021. int i = 0, ref_val;
  1022. while (1) {
  1023. ref_val = array_valid_values[i];
  1024. if (ref_val == INT_MAX)
  1025. break;
  1026. if (val == ref_val)
  1027. return val;
  1028. i++;
  1029. }
  1030. /* val is not a valid value */
  1031. av_log(ctx, AV_LOG_DEBUG,
  1032. "%s %d are not supported. Set to default value : %d\n", val_name, val, default_value);
  1033. return default_value;
  1034. }