utils.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234
  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/attributes.h"
  28. #include "libavutil/avassert.h"
  29. #include "libavutil/avstring.h"
  30. #include "libavutil/bprint.h"
  31. #include "libavutil/channel_layout.h"
  32. #include "libavutil/crc.h"
  33. #include "libavutil/frame.h"
  34. #include "libavutil/hwcontext.h"
  35. #include "libavutil/internal.h"
  36. #include "libavutil/mathematics.h"
  37. #include "libavutil/mem_internal.h"
  38. #include "libavutil/pixdesc.h"
  39. #include "libavutil/imgutils.h"
  40. #include "libavutil/samplefmt.h"
  41. #include "libavutil/dict.h"
  42. #include "libavutil/thread.h"
  43. #include "avcodec.h"
  44. #include "decode.h"
  45. #include "hwaccel.h"
  46. #include "libavutil/opt.h"
  47. #include "mpegvideo.h"
  48. #include "thread.h"
  49. #include "frame_thread_encoder.h"
  50. #include "internal.h"
  51. #include "raw.h"
  52. #include "bytestream.h"
  53. #include "version.h"
  54. #include <stdlib.h>
  55. #include <stdarg.h>
  56. #include <stdatomic.h>
  57. #include <limits.h>
  58. #include <float.h>
  59. #if CONFIG_ICONV
  60. # include <iconv.h>
  61. #endif
  62. #include "libavutil/ffversion.h"
  63. const char av_codec_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
  64. static AVMutex codec_mutex = AV_MUTEX_INITIALIZER;
  65. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  66. {
  67. uint8_t **p = ptr;
  68. if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
  69. av_freep(p);
  70. *size = 0;
  71. return;
  72. }
  73. if (!ff_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE, 1))
  74. memset(*p + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  75. }
  76. void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
  77. {
  78. uint8_t **p = ptr;
  79. if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
  80. av_freep(p);
  81. *size = 0;
  82. return;
  83. }
  84. if (!ff_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE, 1))
  85. memset(*p, 0, min_size + AV_INPUT_BUFFER_PADDING_SIZE);
  86. }
  87. int av_codec_is_encoder(const AVCodec *codec)
  88. {
  89. return codec && (codec->encode_sub || codec->encode2 ||codec->send_frame);
  90. }
  91. int av_codec_is_decoder(const AVCodec *codec)
  92. {
  93. return codec && (codec->decode || codec->receive_frame);
  94. }
  95. int ff_set_dimensions(AVCodecContext *s, int width, int height)
  96. {
  97. int ret = av_image_check_size2(width, height, s->max_pixels, AV_PIX_FMT_NONE, 0, s);
  98. if (ret < 0)
  99. width = height = 0;
  100. s->coded_width = width;
  101. s->coded_height = height;
  102. s->width = AV_CEIL_RSHIFT(width, s->lowres);
  103. s->height = AV_CEIL_RSHIFT(height, s->lowres);
  104. return ret;
  105. }
  106. int ff_set_sar(AVCodecContext *avctx, AVRational sar)
  107. {
  108. int ret = av_image_check_sar(avctx->width, avctx->height, sar);
  109. if (ret < 0) {
  110. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %d/%d\n",
  111. sar.num, sar.den);
  112. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  113. return ret;
  114. } else {
  115. avctx->sample_aspect_ratio = sar;
  116. }
  117. return 0;
  118. }
  119. int ff_side_data_update_matrix_encoding(AVFrame *frame,
  120. enum AVMatrixEncoding matrix_encoding)
  121. {
  122. AVFrameSideData *side_data;
  123. enum AVMatrixEncoding *data;
  124. side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
  125. if (!side_data)
  126. side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
  127. sizeof(enum AVMatrixEncoding));
  128. if (!side_data)
  129. return AVERROR(ENOMEM);
  130. data = (enum AVMatrixEncoding*)side_data->data;
  131. *data = matrix_encoding;
  132. return 0;
  133. }
  134. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  135. int linesize_align[AV_NUM_DATA_POINTERS])
  136. {
  137. int i;
  138. int w_align = 1;
  139. int h_align = 1;
  140. AVPixFmtDescriptor const *desc = av_pix_fmt_desc_get(s->pix_fmt);
  141. if (desc) {
  142. w_align = 1 << desc->log2_chroma_w;
  143. h_align = 1 << desc->log2_chroma_h;
  144. }
  145. switch (s->pix_fmt) {
  146. case AV_PIX_FMT_YUV420P:
  147. case AV_PIX_FMT_YUYV422:
  148. case AV_PIX_FMT_YVYU422:
  149. case AV_PIX_FMT_UYVY422:
  150. case AV_PIX_FMT_YUV422P:
  151. case AV_PIX_FMT_YUV440P:
  152. case AV_PIX_FMT_YUV444P:
  153. case AV_PIX_FMT_GBRP:
  154. case AV_PIX_FMT_GBRAP:
  155. case AV_PIX_FMT_GRAY8:
  156. case AV_PIX_FMT_GRAY16BE:
  157. case AV_PIX_FMT_GRAY16LE:
  158. case AV_PIX_FMT_YUVJ420P:
  159. case AV_PIX_FMT_YUVJ422P:
  160. case AV_PIX_FMT_YUVJ440P:
  161. case AV_PIX_FMT_YUVJ444P:
  162. case AV_PIX_FMT_YUVA420P:
  163. case AV_PIX_FMT_YUVA422P:
  164. case AV_PIX_FMT_YUVA444P:
  165. case AV_PIX_FMT_YUV420P9LE:
  166. case AV_PIX_FMT_YUV420P9BE:
  167. case AV_PIX_FMT_YUV420P10LE:
  168. case AV_PIX_FMT_YUV420P10BE:
  169. case AV_PIX_FMT_YUV420P12LE:
  170. case AV_PIX_FMT_YUV420P12BE:
  171. case AV_PIX_FMT_YUV420P14LE:
  172. case AV_PIX_FMT_YUV420P14BE:
  173. case AV_PIX_FMT_YUV420P16LE:
  174. case AV_PIX_FMT_YUV420P16BE:
  175. case AV_PIX_FMT_YUVA420P9LE:
  176. case AV_PIX_FMT_YUVA420P9BE:
  177. case AV_PIX_FMT_YUVA420P10LE:
  178. case AV_PIX_FMT_YUVA420P10BE:
  179. case AV_PIX_FMT_YUVA420P16LE:
  180. case AV_PIX_FMT_YUVA420P16BE:
  181. case AV_PIX_FMT_YUV422P9LE:
  182. case AV_PIX_FMT_YUV422P9BE:
  183. case AV_PIX_FMT_YUV422P10LE:
  184. case AV_PIX_FMT_YUV422P10BE:
  185. case AV_PIX_FMT_YUV422P12LE:
  186. case AV_PIX_FMT_YUV422P12BE:
  187. case AV_PIX_FMT_YUV422P14LE:
  188. case AV_PIX_FMT_YUV422P14BE:
  189. case AV_PIX_FMT_YUV422P16LE:
  190. case AV_PIX_FMT_YUV422P16BE:
  191. case AV_PIX_FMT_YUVA422P9LE:
  192. case AV_PIX_FMT_YUVA422P9BE:
  193. case AV_PIX_FMT_YUVA422P10LE:
  194. case AV_PIX_FMT_YUVA422P10BE:
  195. case AV_PIX_FMT_YUVA422P16LE:
  196. case AV_PIX_FMT_YUVA422P16BE:
  197. case AV_PIX_FMT_YUV440P10LE:
  198. case AV_PIX_FMT_YUV440P10BE:
  199. case AV_PIX_FMT_YUV440P12LE:
  200. case AV_PIX_FMT_YUV440P12BE:
  201. case AV_PIX_FMT_YUV444P9LE:
  202. case AV_PIX_FMT_YUV444P9BE:
  203. case AV_PIX_FMT_YUV444P10LE:
  204. case AV_PIX_FMT_YUV444P10BE:
  205. case AV_PIX_FMT_YUV444P12LE:
  206. case AV_PIX_FMT_YUV444P12BE:
  207. case AV_PIX_FMT_YUV444P14LE:
  208. case AV_PIX_FMT_YUV444P14BE:
  209. case AV_PIX_FMT_YUV444P16LE:
  210. case AV_PIX_FMT_YUV444P16BE:
  211. case AV_PIX_FMT_YUVA444P9LE:
  212. case AV_PIX_FMT_YUVA444P9BE:
  213. case AV_PIX_FMT_YUVA444P10LE:
  214. case AV_PIX_FMT_YUVA444P10BE:
  215. case AV_PIX_FMT_YUVA444P16LE:
  216. case AV_PIX_FMT_YUVA444P16BE:
  217. case AV_PIX_FMT_GBRP9LE:
  218. case AV_PIX_FMT_GBRP9BE:
  219. case AV_PIX_FMT_GBRP10LE:
  220. case AV_PIX_FMT_GBRP10BE:
  221. case AV_PIX_FMT_GBRP12LE:
  222. case AV_PIX_FMT_GBRP12BE:
  223. case AV_PIX_FMT_GBRP14LE:
  224. case AV_PIX_FMT_GBRP14BE:
  225. case AV_PIX_FMT_GBRP16LE:
  226. case AV_PIX_FMT_GBRP16BE:
  227. case AV_PIX_FMT_GBRAP12LE:
  228. case AV_PIX_FMT_GBRAP12BE:
  229. case AV_PIX_FMT_GBRAP16LE:
  230. case AV_PIX_FMT_GBRAP16BE:
  231. w_align = 16; //FIXME assume 16 pixel per macroblock
  232. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  233. break;
  234. case AV_PIX_FMT_YUV411P:
  235. case AV_PIX_FMT_YUVJ411P:
  236. case AV_PIX_FMT_UYYVYY411:
  237. w_align = 32;
  238. h_align = 16 * 2;
  239. break;
  240. case AV_PIX_FMT_YUV410P:
  241. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  242. w_align = 64;
  243. h_align = 64;
  244. }
  245. break;
  246. case AV_PIX_FMT_RGB555:
  247. if (s->codec_id == AV_CODEC_ID_RPZA) {
  248. w_align = 4;
  249. h_align = 4;
  250. }
  251. if (s->codec_id == AV_CODEC_ID_INTERPLAY_VIDEO) {
  252. w_align = 8;
  253. h_align = 8;
  254. }
  255. break;
  256. case AV_PIX_FMT_PAL8:
  257. case AV_PIX_FMT_BGR8:
  258. case AV_PIX_FMT_RGB8:
  259. if (s->codec_id == AV_CODEC_ID_SMC ||
  260. s->codec_id == AV_CODEC_ID_CINEPAK) {
  261. w_align = 4;
  262. h_align = 4;
  263. }
  264. if (s->codec_id == AV_CODEC_ID_JV ||
  265. s->codec_id == AV_CODEC_ID_INTERPLAY_VIDEO) {
  266. w_align = 8;
  267. h_align = 8;
  268. }
  269. break;
  270. case AV_PIX_FMT_BGR24:
  271. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  272. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  273. w_align = 4;
  274. h_align = 4;
  275. }
  276. break;
  277. case AV_PIX_FMT_RGB24:
  278. if (s->codec_id == AV_CODEC_ID_CINEPAK) {
  279. w_align = 4;
  280. h_align = 4;
  281. }
  282. break;
  283. default:
  284. break;
  285. }
  286. if (s->codec_id == AV_CODEC_ID_IFF_ILBM) {
  287. w_align = FFMAX(w_align, 8);
  288. }
  289. *width = FFALIGN(*width, w_align);
  290. *height = FFALIGN(*height, h_align);
  291. if (s->codec_id == AV_CODEC_ID_H264 || s->lowres ||
  292. s->codec_id == AV_CODEC_ID_VP5 || s->codec_id == AV_CODEC_ID_VP6 ||
  293. s->codec_id == AV_CODEC_ID_VP6F || s->codec_id == AV_CODEC_ID_VP6A
  294. ) {
  295. // some of the optimized chroma MC reads one line too much
  296. // which is also done in mpeg decoders with lowres > 0
  297. *height += 2;
  298. // H.264 uses edge emulation for out of frame motion vectors, for this
  299. // it requires a temporary area large enough to hold a 21x21 block,
  300. // increasing witdth ensure that the temporary area is large enough,
  301. // the next rounded up width is 32
  302. *width = FFMAX(*width, 32);
  303. }
  304. for (i = 0; i < 4; i++)
  305. linesize_align[i] = STRIDE_ALIGN;
  306. }
  307. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  308. {
  309. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  310. int chroma_shift = desc->log2_chroma_w;
  311. int linesize_align[AV_NUM_DATA_POINTERS];
  312. int align;
  313. avcodec_align_dimensions2(s, width, height, linesize_align);
  314. align = FFMAX(linesize_align[0], linesize_align[3]);
  315. linesize_align[1] <<= chroma_shift;
  316. linesize_align[2] <<= chroma_shift;
  317. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  318. *width = FFALIGN(*width, align);
  319. }
  320. int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
  321. {
  322. if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
  323. return AVERROR(EINVAL);
  324. pos--;
  325. *xpos = (pos&1) * 128;
  326. *ypos = ((pos>>1)^(pos<4)) * 128;
  327. return 0;
  328. }
  329. enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos)
  330. {
  331. int pos, xout, yout;
  332. for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
  333. if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
  334. return pos;
  335. }
  336. return AVCHROMA_LOC_UNSPECIFIED;
  337. }
  338. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  339. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  340. int buf_size, int align)
  341. {
  342. int ch, planar, needed_size, ret = 0;
  343. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  344. frame->nb_samples, sample_fmt,
  345. align);
  346. if (buf_size < needed_size)
  347. return AVERROR(EINVAL);
  348. planar = av_sample_fmt_is_planar(sample_fmt);
  349. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  350. if (!(frame->extended_data = av_mallocz_array(nb_channels,
  351. sizeof(*frame->extended_data))))
  352. return AVERROR(ENOMEM);
  353. } else {
  354. frame->extended_data = frame->data;
  355. }
  356. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  357. (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
  358. sample_fmt, align)) < 0) {
  359. if (frame->extended_data != frame->data)
  360. av_freep(&frame->extended_data);
  361. return ret;
  362. }
  363. if (frame->extended_data != frame->data) {
  364. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  365. frame->data[ch] = frame->extended_data[ch];
  366. }
  367. return ret;
  368. }
  369. void ff_color_frame(AVFrame *frame, const int c[4])
  370. {
  371. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  372. int p, y;
  373. av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
  374. for (p = 0; p<desc->nb_components; p++) {
  375. uint8_t *dst = frame->data[p];
  376. int is_chroma = p == 1 || p == 2;
  377. int bytes = is_chroma ? AV_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
  378. int height = is_chroma ? AV_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
  379. if (desc->comp[0].depth >= 9) {
  380. ((uint16_t*)dst)[0] = c[p];
  381. av_memcpy_backptr(dst + 2, 2, bytes - 2);
  382. dst += frame->linesize[p];
  383. for (y = 1; y < height; y++) {
  384. memcpy(dst, frame->data[p], 2*bytes);
  385. dst += frame->linesize[p];
  386. }
  387. } else {
  388. for (y = 0; y < height; y++) {
  389. memset(dst, c[p], bytes);
  390. dst += frame->linesize[p];
  391. }
  392. }
  393. }
  394. }
  395. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  396. {
  397. int i;
  398. for (i = 0; i < count; i++) {
  399. int r = func(c, (char *)arg + i * size);
  400. if (ret)
  401. ret[i] = r;
  402. }
  403. emms_c();
  404. return 0;
  405. }
  406. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  407. {
  408. int i;
  409. for (i = 0; i < count; i++) {
  410. int r = func(c, arg, i, 0);
  411. if (ret)
  412. ret[i] = r;
  413. }
  414. emms_c();
  415. return 0;
  416. }
  417. enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags,
  418. unsigned int fourcc)
  419. {
  420. while (tags->pix_fmt >= 0) {
  421. if (tags->fourcc == fourcc)
  422. return tags->pix_fmt;
  423. tags++;
  424. }
  425. return AV_PIX_FMT_NONE;
  426. }
  427. #if FF_API_CODEC_GET_SET
  428. MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
  429. MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
  430. MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
  431. MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
  432. MAKE_ACCESSORS(AVCodecContext, codec, uint16_t*, chroma_intra_matrix)
  433. unsigned av_codec_get_codec_properties(const AVCodecContext *codec)
  434. {
  435. return codec->properties;
  436. }
  437. int av_codec_get_max_lowres(const AVCodec *codec)
  438. {
  439. return codec->max_lowres;
  440. }
  441. #endif
  442. int avpriv_codec_get_cap_skip_frame_fill_param(const AVCodec *codec){
  443. return !!(codec->caps_internal & FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM);
  444. }
  445. static int64_t get_bit_rate(AVCodecContext *ctx)
  446. {
  447. int64_t bit_rate;
  448. int bits_per_sample;
  449. switch (ctx->codec_type) {
  450. case AVMEDIA_TYPE_VIDEO:
  451. case AVMEDIA_TYPE_DATA:
  452. case AVMEDIA_TYPE_SUBTITLE:
  453. case AVMEDIA_TYPE_ATTACHMENT:
  454. bit_rate = ctx->bit_rate;
  455. break;
  456. case AVMEDIA_TYPE_AUDIO:
  457. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  458. bit_rate = bits_per_sample ? ctx->sample_rate * (int64_t)ctx->channels * bits_per_sample : ctx->bit_rate;
  459. break;
  460. default:
  461. bit_rate = 0;
  462. break;
  463. }
  464. return bit_rate;
  465. }
  466. static void ff_lock_avcodec(AVCodecContext *log_ctx, const AVCodec *codec)
  467. {
  468. if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
  469. ff_mutex_lock(&codec_mutex);
  470. }
  471. static void ff_unlock_avcodec(const AVCodec *codec)
  472. {
  473. if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
  474. ff_mutex_unlock(&codec_mutex);
  475. }
  476. int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  477. {
  478. int ret = 0;
  479. ff_unlock_avcodec(codec);
  480. ret = avcodec_open2(avctx, codec, options);
  481. ff_lock_avcodec(avctx, codec);
  482. return ret;
  483. }
  484. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  485. {
  486. int ret = 0;
  487. int codec_init_ok = 0;
  488. AVDictionary *tmp = NULL;
  489. const AVPixFmtDescriptor *pixdesc;
  490. if (avcodec_is_open(avctx))
  491. return 0;
  492. if ((!codec && !avctx->codec)) {
  493. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
  494. return AVERROR(EINVAL);
  495. }
  496. if ((codec && avctx->codec && codec != avctx->codec)) {
  497. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  498. "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
  499. return AVERROR(EINVAL);
  500. }
  501. if (!codec)
  502. codec = avctx->codec;
  503. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  504. return AVERROR(EINVAL);
  505. if (options)
  506. av_dict_copy(&tmp, *options, 0);
  507. ff_lock_avcodec(avctx, codec);
  508. avctx->internal = av_mallocz(sizeof(*avctx->internal));
  509. if (!avctx->internal) {
  510. ret = AVERROR(ENOMEM);
  511. goto end;
  512. }
  513. avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
  514. if (!avctx->internal->pool) {
  515. ret = AVERROR(ENOMEM);
  516. goto free_and_end;
  517. }
  518. avctx->internal->to_free = av_frame_alloc();
  519. if (!avctx->internal->to_free) {
  520. ret = AVERROR(ENOMEM);
  521. goto free_and_end;
  522. }
  523. avctx->internal->compat_decode_frame = av_frame_alloc();
  524. if (!avctx->internal->compat_decode_frame) {
  525. ret = AVERROR(ENOMEM);
  526. goto free_and_end;
  527. }
  528. avctx->internal->buffer_frame = av_frame_alloc();
  529. if (!avctx->internal->buffer_frame) {
  530. ret = AVERROR(ENOMEM);
  531. goto free_and_end;
  532. }
  533. avctx->internal->buffer_pkt = av_packet_alloc();
  534. if (!avctx->internal->buffer_pkt) {
  535. ret = AVERROR(ENOMEM);
  536. goto free_and_end;
  537. }
  538. avctx->internal->ds.in_pkt = av_packet_alloc();
  539. if (!avctx->internal->ds.in_pkt) {
  540. ret = AVERROR(ENOMEM);
  541. goto free_and_end;
  542. }
  543. avctx->internal->last_pkt_props = av_packet_alloc();
  544. if (!avctx->internal->last_pkt_props) {
  545. ret = AVERROR(ENOMEM);
  546. goto free_and_end;
  547. }
  548. avctx->internal->skip_samples_multiplier = 1;
  549. if (codec->priv_data_size > 0) {
  550. if (!avctx->priv_data) {
  551. avctx->priv_data = av_mallocz(codec->priv_data_size);
  552. if (!avctx->priv_data) {
  553. ret = AVERROR(ENOMEM);
  554. goto end;
  555. }
  556. if (codec->priv_class) {
  557. *(const AVClass **)avctx->priv_data = codec->priv_class;
  558. av_opt_set_defaults(avctx->priv_data);
  559. }
  560. }
  561. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  562. goto free_and_end;
  563. } else {
  564. avctx->priv_data = NULL;
  565. }
  566. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  567. goto free_and_end;
  568. if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
  569. av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist);
  570. ret = AVERROR(EINVAL);
  571. goto free_and_end;
  572. }
  573. // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
  574. if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
  575. (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F || avctx->codec_id == AV_CODEC_ID_DXV))) {
  576. if (avctx->coded_width && avctx->coded_height)
  577. ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  578. else if (avctx->width && avctx->height)
  579. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  580. if (ret < 0)
  581. goto free_and_end;
  582. }
  583. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  584. && ( av_image_check_size2(avctx->coded_width, avctx->coded_height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0
  585. || av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0)) {
  586. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
  587. ff_set_dimensions(avctx, 0, 0);
  588. }
  589. if (avctx->width > 0 && avctx->height > 0) {
  590. if (av_image_check_sar(avctx->width, avctx->height,
  591. avctx->sample_aspect_ratio) < 0) {
  592. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  593. avctx->sample_aspect_ratio.num,
  594. avctx->sample_aspect_ratio.den);
  595. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  596. }
  597. }
  598. /* if the decoder init function was already called previously,
  599. * free the already allocated subtitle_header before overwriting it */
  600. if (av_codec_is_decoder(codec))
  601. av_freep(&avctx->subtitle_header);
  602. if (avctx->channels > FF_SANE_NB_CHANNELS || avctx->channels < 0) {
  603. av_log(avctx, AV_LOG_ERROR, "Too many or invalid channels: %d\n", avctx->channels);
  604. ret = AVERROR(EINVAL);
  605. goto free_and_end;
  606. }
  607. if (avctx->sample_rate < 0) {
  608. av_log(avctx, AV_LOG_ERROR, "Invalid sample rate: %d\n", avctx->sample_rate);
  609. ret = AVERROR(EINVAL);
  610. goto free_and_end;
  611. }
  612. if (avctx->block_align < 0) {
  613. av_log(avctx, AV_LOG_ERROR, "Invalid block align: %d\n", avctx->block_align);
  614. ret = AVERROR(EINVAL);
  615. goto free_and_end;
  616. }
  617. avctx->codec = codec;
  618. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  619. avctx->codec_id == AV_CODEC_ID_NONE) {
  620. avctx->codec_type = codec->type;
  621. avctx->codec_id = codec->id;
  622. }
  623. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  624. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  625. av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
  626. ret = AVERROR(EINVAL);
  627. goto free_and_end;
  628. }
  629. avctx->frame_number = 0;
  630. avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  631. if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
  632. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  633. const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
  634. AVCodec *codec2;
  635. av_log(avctx, AV_LOG_ERROR,
  636. "The %s '%s' is experimental but experimental codecs are not enabled, "
  637. "add '-strict %d' if you want to use it.\n",
  638. codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
  639. codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
  640. if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
  641. av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
  642. codec_string, codec2->name);
  643. ret = AVERROR_EXPERIMENTAL;
  644. goto free_and_end;
  645. }
  646. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  647. (!avctx->time_base.num || !avctx->time_base.den)) {
  648. avctx->time_base.num = 1;
  649. avctx->time_base.den = avctx->sample_rate;
  650. }
  651. if (!HAVE_THREADS)
  652. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  653. if (CONFIG_FRAME_THREAD_ENCODER && av_codec_is_encoder(avctx->codec)) {
  654. ff_unlock_avcodec(codec); //we will instantiate a few encoders thus kick the counter to prevent false detection of a problem
  655. ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
  656. ff_lock_avcodec(avctx, codec);
  657. if (ret < 0)
  658. goto free_and_end;
  659. }
  660. if (av_codec_is_decoder(avctx->codec)) {
  661. ret = ff_decode_bsfs_init(avctx);
  662. if (ret < 0)
  663. goto free_and_end;
  664. }
  665. if (HAVE_THREADS
  666. && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  667. ret = ff_thread_init(avctx);
  668. if (ret < 0) {
  669. goto free_and_end;
  670. }
  671. }
  672. if (!HAVE_THREADS && !(codec->capabilities & AV_CODEC_CAP_AUTO_THREADS))
  673. avctx->thread_count = 1;
  674. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  675. av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
  676. avctx->codec->max_lowres);
  677. avctx->lowres = avctx->codec->max_lowres;
  678. }
  679. if (av_codec_is_encoder(avctx->codec)) {
  680. int i;
  681. #if FF_API_CODED_FRAME
  682. FF_DISABLE_DEPRECATION_WARNINGS
  683. avctx->coded_frame = av_frame_alloc();
  684. if (!avctx->coded_frame) {
  685. ret = AVERROR(ENOMEM);
  686. goto free_and_end;
  687. }
  688. FF_ENABLE_DEPRECATION_WARNINGS
  689. #endif
  690. if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
  691. av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
  692. ret = AVERROR(EINVAL);
  693. goto free_and_end;
  694. }
  695. if (avctx->codec->sample_fmts) {
  696. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  697. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  698. break;
  699. if (avctx->channels == 1 &&
  700. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  701. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  702. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  703. break;
  704. }
  705. }
  706. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  707. char buf[128];
  708. snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
  709. av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
  710. (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
  711. ret = AVERROR(EINVAL);
  712. goto free_and_end;
  713. }
  714. }
  715. if (avctx->codec->pix_fmts) {
  716. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  717. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  718. break;
  719. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
  720. && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
  721. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  722. char buf[128];
  723. snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
  724. av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
  725. (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
  726. ret = AVERROR(EINVAL);
  727. goto free_and_end;
  728. }
  729. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
  730. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
  731. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
  732. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
  733. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
  734. avctx->color_range = AVCOL_RANGE_JPEG;
  735. }
  736. if (avctx->codec->supported_samplerates) {
  737. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  738. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  739. break;
  740. if (avctx->codec->supported_samplerates[i] == 0) {
  741. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  742. avctx->sample_rate);
  743. ret = AVERROR(EINVAL);
  744. goto free_and_end;
  745. }
  746. }
  747. if (avctx->sample_rate < 0) {
  748. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  749. avctx->sample_rate);
  750. ret = AVERROR(EINVAL);
  751. goto free_and_end;
  752. }
  753. if (avctx->codec->channel_layouts) {
  754. if (!avctx->channel_layout) {
  755. av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
  756. } else {
  757. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  758. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  759. break;
  760. if (avctx->codec->channel_layouts[i] == 0) {
  761. char buf[512];
  762. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  763. av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
  764. ret = AVERROR(EINVAL);
  765. goto free_and_end;
  766. }
  767. }
  768. }
  769. if (avctx->channel_layout && avctx->channels) {
  770. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  771. if (channels != avctx->channels) {
  772. char buf[512];
  773. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  774. av_log(avctx, AV_LOG_ERROR,
  775. "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
  776. buf, channels, avctx->channels);
  777. ret = AVERROR(EINVAL);
  778. goto free_and_end;
  779. }
  780. } else if (avctx->channel_layout) {
  781. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  782. }
  783. if (avctx->channels < 0) {
  784. av_log(avctx, AV_LOG_ERROR, "Specified number of channels %d is not supported\n",
  785. avctx->channels);
  786. ret = AVERROR(EINVAL);
  787. goto free_and_end;
  788. }
  789. if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  790. pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
  791. if ( avctx->bits_per_raw_sample < 0
  792. || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
  793. av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
  794. avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
  795. avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
  796. }
  797. if (avctx->width <= 0 || avctx->height <= 0) {
  798. av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
  799. ret = AVERROR(EINVAL);
  800. goto free_and_end;
  801. }
  802. }
  803. if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
  804. && avctx->bit_rate>0 && avctx->bit_rate<1000) {
  805. av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate);
  806. }
  807. if (!avctx->rc_initial_buffer_occupancy)
  808. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
  809. if (avctx->ticks_per_frame && avctx->time_base.num &&
  810. avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
  811. av_log(avctx, AV_LOG_ERROR,
  812. "ticks_per_frame %d too large for the timebase %d/%d.",
  813. avctx->ticks_per_frame,
  814. avctx->time_base.num,
  815. avctx->time_base.den);
  816. goto free_and_end;
  817. }
  818. if (avctx->hw_frames_ctx) {
  819. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  820. if (frames_ctx->format != avctx->pix_fmt) {
  821. av_log(avctx, AV_LOG_ERROR,
  822. "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
  823. ret = AVERROR(EINVAL);
  824. goto free_and_end;
  825. }
  826. if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
  827. avctx->sw_pix_fmt != frames_ctx->sw_format) {
  828. av_log(avctx, AV_LOG_ERROR,
  829. "Mismatching AVCodecContext.sw_pix_fmt (%s) "
  830. "and AVHWFramesContext.sw_format (%s)\n",
  831. av_get_pix_fmt_name(avctx->sw_pix_fmt),
  832. av_get_pix_fmt_name(frames_ctx->sw_format));
  833. ret = AVERROR(EINVAL);
  834. goto free_and_end;
  835. }
  836. avctx->sw_pix_fmt = frames_ctx->sw_format;
  837. }
  838. }
  839. avctx->pts_correction_num_faulty_pts =
  840. avctx->pts_correction_num_faulty_dts = 0;
  841. avctx->pts_correction_last_pts =
  842. avctx->pts_correction_last_dts = INT64_MIN;
  843. if ( !CONFIG_GRAY && avctx->flags & AV_CODEC_FLAG_GRAY
  844. && avctx->codec_descriptor->type == AVMEDIA_TYPE_VIDEO)
  845. av_log(avctx, AV_LOG_WARNING,
  846. "gray decoding requested but not enabled at configuration time\n");
  847. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  848. || avctx->internal->frame_thread_encoder)) {
  849. ret = avctx->codec->init(avctx);
  850. if (ret < 0) {
  851. goto free_and_end;
  852. }
  853. codec_init_ok = 1;
  854. }
  855. ret=0;
  856. if (av_codec_is_decoder(avctx->codec)) {
  857. if (!avctx->bit_rate)
  858. avctx->bit_rate = get_bit_rate(avctx);
  859. /* validate channel layout from the decoder */
  860. if (avctx->channel_layout) {
  861. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  862. if (!avctx->channels)
  863. avctx->channels = channels;
  864. else if (channels != avctx->channels) {
  865. char buf[512];
  866. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  867. av_log(avctx, AV_LOG_WARNING,
  868. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  869. "ignoring specified channel layout\n",
  870. buf, channels, avctx->channels);
  871. avctx->channel_layout = 0;
  872. }
  873. }
  874. if (avctx->channels && avctx->channels < 0 ||
  875. avctx->channels > FF_SANE_NB_CHANNELS) {
  876. ret = AVERROR(EINVAL);
  877. goto free_and_end;
  878. }
  879. if (avctx->bits_per_coded_sample < 0) {
  880. ret = AVERROR(EINVAL);
  881. goto free_and_end;
  882. }
  883. if (avctx->sub_charenc) {
  884. if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  885. av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
  886. "supported with subtitles codecs\n");
  887. ret = AVERROR(EINVAL);
  888. goto free_and_end;
  889. } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
  890. av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
  891. "subtitles character encoding will be ignored\n",
  892. avctx->codec_descriptor->name);
  893. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
  894. } else {
  895. /* input character encoding is set for a text based subtitle
  896. * codec at this point */
  897. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
  898. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
  899. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
  900. #if CONFIG_ICONV
  901. iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
  902. if (cd == (iconv_t)-1) {
  903. ret = AVERROR(errno);
  904. av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
  905. "with input character encoding \"%s\"\n", avctx->sub_charenc);
  906. goto free_and_end;
  907. }
  908. iconv_close(cd);
  909. #else
  910. av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
  911. "conversion needs a libavcodec built with iconv support "
  912. "for this codec\n");
  913. ret = AVERROR(ENOSYS);
  914. goto free_and_end;
  915. #endif
  916. }
  917. }
  918. }
  919. #if FF_API_AVCTX_TIMEBASE
  920. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  921. avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
  922. #endif
  923. }
  924. if (codec->priv_data_size > 0 && avctx->priv_data && codec->priv_class) {
  925. av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
  926. }
  927. end:
  928. ff_unlock_avcodec(codec);
  929. if (options) {
  930. av_dict_free(options);
  931. *options = tmp;
  932. }
  933. return ret;
  934. free_and_end:
  935. if (avctx->codec && avctx->codec->close &&
  936. (codec_init_ok ||
  937. (avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP)))
  938. avctx->codec->close(avctx);
  939. if (HAVE_THREADS && avctx->internal->thread_ctx)
  940. ff_thread_free(avctx);
  941. if (codec->priv_class && codec->priv_data_size)
  942. av_opt_free(avctx->priv_data);
  943. av_opt_free(avctx);
  944. #if FF_API_CODED_FRAME
  945. FF_DISABLE_DEPRECATION_WARNINGS
  946. av_frame_free(&avctx->coded_frame);
  947. FF_ENABLE_DEPRECATION_WARNINGS
  948. #endif
  949. av_dict_free(&tmp);
  950. av_freep(&avctx->priv_data);
  951. av_freep(&avctx->subtitle_header);
  952. if (avctx->internal) {
  953. av_frame_free(&avctx->internal->to_free);
  954. av_frame_free(&avctx->internal->compat_decode_frame);
  955. av_frame_free(&avctx->internal->buffer_frame);
  956. av_packet_free(&avctx->internal->buffer_pkt);
  957. av_packet_free(&avctx->internal->last_pkt_props);
  958. av_packet_free(&avctx->internal->ds.in_pkt);
  959. ff_decode_bsfs_uninit(avctx);
  960. av_freep(&avctx->internal->pool);
  961. }
  962. av_freep(&avctx->internal);
  963. avctx->codec = NULL;
  964. goto end;
  965. }
  966. void avsubtitle_free(AVSubtitle *sub)
  967. {
  968. int i;
  969. for (i = 0; i < sub->num_rects; i++) {
  970. av_freep(&sub->rects[i]->data[0]);
  971. av_freep(&sub->rects[i]->data[1]);
  972. av_freep(&sub->rects[i]->data[2]);
  973. av_freep(&sub->rects[i]->data[3]);
  974. av_freep(&sub->rects[i]->text);
  975. av_freep(&sub->rects[i]->ass);
  976. av_freep(&sub->rects[i]);
  977. }
  978. av_freep(&sub->rects);
  979. memset(sub, 0, sizeof(*sub));
  980. }
  981. av_cold int avcodec_close(AVCodecContext *avctx)
  982. {
  983. int i;
  984. if (!avctx)
  985. return 0;
  986. if (avcodec_is_open(avctx)) {
  987. FramePool *pool = avctx->internal->pool;
  988. if (CONFIG_FRAME_THREAD_ENCODER &&
  989. avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  990. ff_frame_thread_encoder_free(avctx);
  991. }
  992. if (HAVE_THREADS && avctx->internal->thread_ctx)
  993. ff_thread_free(avctx);
  994. if (avctx->codec && avctx->codec->close)
  995. avctx->codec->close(avctx);
  996. avctx->internal->byte_buffer_size = 0;
  997. av_freep(&avctx->internal->byte_buffer);
  998. av_frame_free(&avctx->internal->to_free);
  999. av_frame_free(&avctx->internal->compat_decode_frame);
  1000. av_frame_free(&avctx->internal->buffer_frame);
  1001. av_packet_free(&avctx->internal->buffer_pkt);
  1002. av_packet_free(&avctx->internal->last_pkt_props);
  1003. av_packet_free(&avctx->internal->ds.in_pkt);
  1004. for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
  1005. av_buffer_pool_uninit(&pool->pools[i]);
  1006. av_freep(&avctx->internal->pool);
  1007. if (avctx->hwaccel && avctx->hwaccel->uninit)
  1008. avctx->hwaccel->uninit(avctx);
  1009. av_freep(&avctx->internal->hwaccel_priv_data);
  1010. ff_decode_bsfs_uninit(avctx);
  1011. av_freep(&avctx->internal);
  1012. }
  1013. for (i = 0; i < avctx->nb_coded_side_data; i++)
  1014. av_freep(&avctx->coded_side_data[i].data);
  1015. av_freep(&avctx->coded_side_data);
  1016. avctx->nb_coded_side_data = 0;
  1017. av_buffer_unref(&avctx->hw_frames_ctx);
  1018. av_buffer_unref(&avctx->hw_device_ctx);
  1019. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1020. av_opt_free(avctx->priv_data);
  1021. av_opt_free(avctx);
  1022. av_freep(&avctx->priv_data);
  1023. if (av_codec_is_encoder(avctx->codec)) {
  1024. av_freep(&avctx->extradata);
  1025. #if FF_API_CODED_FRAME
  1026. FF_DISABLE_DEPRECATION_WARNINGS
  1027. av_frame_free(&avctx->coded_frame);
  1028. FF_ENABLE_DEPRECATION_WARNINGS
  1029. #endif
  1030. }
  1031. avctx->codec = NULL;
  1032. avctx->active_thread_type = 0;
  1033. return 0;
  1034. }
  1035. const char *avcodec_get_name(enum AVCodecID id)
  1036. {
  1037. const AVCodecDescriptor *cd;
  1038. AVCodec *codec;
  1039. if (id == AV_CODEC_ID_NONE)
  1040. return "none";
  1041. cd = avcodec_descriptor_get(id);
  1042. if (cd)
  1043. return cd->name;
  1044. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  1045. codec = avcodec_find_decoder(id);
  1046. if (codec)
  1047. return codec->name;
  1048. codec = avcodec_find_encoder(id);
  1049. if (codec)
  1050. return codec->name;
  1051. return "unknown_codec";
  1052. }
  1053. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1054. {
  1055. int i, len, ret = 0;
  1056. #define TAG_PRINT(x) \
  1057. (((x) >= '0' && (x) <= '9') || \
  1058. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  1059. ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
  1060. for (i = 0; i < 4; i++) {
  1061. len = snprintf(buf, buf_size,
  1062. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  1063. buf += len;
  1064. buf_size = buf_size > len ? buf_size - len : 0;
  1065. ret += len;
  1066. codec_tag >>= 8;
  1067. }
  1068. return ret;
  1069. }
  1070. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1071. {
  1072. const char *codec_type;
  1073. const char *codec_name;
  1074. const char *profile = NULL;
  1075. int64_t bitrate;
  1076. int new_line = 0;
  1077. AVRational display_aspect_ratio;
  1078. const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
  1079. if (!buf || buf_size <= 0)
  1080. return;
  1081. codec_type = av_get_media_type_string(enc->codec_type);
  1082. codec_name = avcodec_get_name(enc->codec_id);
  1083. profile = avcodec_profile_name(enc->codec_id, enc->profile);
  1084. snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
  1085. codec_name);
  1086. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  1087. if (enc->codec && strcmp(enc->codec->name, codec_name))
  1088. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
  1089. if (profile)
  1090. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  1091. if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
  1092. && av_log_get_level() >= AV_LOG_VERBOSE
  1093. && enc->refs)
  1094. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1095. ", %d reference frame%s",
  1096. enc->refs, enc->refs > 1 ? "s" : "");
  1097. if (enc->codec_tag)
  1098. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s / 0x%04X)",
  1099. av_fourcc2str(enc->codec_tag), enc->codec_tag);
  1100. switch (enc->codec_type) {
  1101. case AVMEDIA_TYPE_VIDEO:
  1102. {
  1103. char detail[256] = "(";
  1104. av_strlcat(buf, separator, buf_size);
  1105. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1106. "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
  1107. av_get_pix_fmt_name(enc->pix_fmt));
  1108. if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
  1109. enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
  1110. av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
  1111. if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
  1112. av_strlcatf(detail, sizeof(detail), "%s, ",
  1113. av_color_range_name(enc->color_range));
  1114. if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
  1115. enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  1116. enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
  1117. if (enc->colorspace != (int)enc->color_primaries ||
  1118. enc->colorspace != (int)enc->color_trc) {
  1119. new_line = 1;
  1120. av_strlcatf(detail, sizeof(detail), "%s/%s/%s, ",
  1121. av_color_space_name(enc->colorspace),
  1122. av_color_primaries_name(enc->color_primaries),
  1123. av_color_transfer_name(enc->color_trc));
  1124. } else
  1125. av_strlcatf(detail, sizeof(detail), "%s, ",
  1126. av_get_colorspace_name(enc->colorspace));
  1127. }
  1128. if (enc->field_order != AV_FIELD_UNKNOWN) {
  1129. const char *field_order = "progressive";
  1130. if (enc->field_order == AV_FIELD_TT)
  1131. field_order = "top first";
  1132. else if (enc->field_order == AV_FIELD_BB)
  1133. field_order = "bottom first";
  1134. else if (enc->field_order == AV_FIELD_TB)
  1135. field_order = "top coded first (swapped)";
  1136. else if (enc->field_order == AV_FIELD_BT)
  1137. field_order = "bottom coded first (swapped)";
  1138. av_strlcatf(detail, sizeof(detail), "%s, ", field_order);
  1139. }
  1140. if (av_log_get_level() >= AV_LOG_VERBOSE &&
  1141. enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
  1142. av_strlcatf(detail, sizeof(detail), "%s, ",
  1143. av_chroma_location_name(enc->chroma_sample_location));
  1144. if (strlen(detail) > 1) {
  1145. detail[strlen(detail) - 2] = 0;
  1146. av_strlcatf(buf, buf_size, "%s)", detail);
  1147. }
  1148. }
  1149. if (enc->width) {
  1150. av_strlcat(buf, new_line ? separator : ", ", buf_size);
  1151. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1152. "%dx%d",
  1153. enc->width, enc->height);
  1154. if (av_log_get_level() >= AV_LOG_VERBOSE &&
  1155. (enc->width != enc->coded_width ||
  1156. enc->height != enc->coded_height))
  1157. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1158. " (%dx%d)", enc->coded_width, enc->coded_height);
  1159. if (enc->sample_aspect_ratio.num) {
  1160. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1161. enc->width * (int64_t)enc->sample_aspect_ratio.num,
  1162. enc->height * (int64_t)enc->sample_aspect_ratio.den,
  1163. 1024 * 1024);
  1164. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1165. " [SAR %d:%d DAR %d:%d]",
  1166. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1167. display_aspect_ratio.num, display_aspect_ratio.den);
  1168. }
  1169. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1170. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1171. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1172. ", %d/%d",
  1173. enc->time_base.num / g, enc->time_base.den / g);
  1174. }
  1175. }
  1176. if (encode) {
  1177. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1178. ", q=%d-%d", enc->qmin, enc->qmax);
  1179. } else {
  1180. if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
  1181. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1182. ", Closed Captions");
  1183. if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
  1184. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1185. ", lossless");
  1186. }
  1187. break;
  1188. case AVMEDIA_TYPE_AUDIO:
  1189. av_strlcat(buf, separator, buf_size);
  1190. if (enc->sample_rate) {
  1191. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1192. "%d Hz, ", enc->sample_rate);
  1193. }
  1194. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1195. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1196. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1197. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1198. }
  1199. if ( enc->bits_per_raw_sample > 0
  1200. && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
  1201. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1202. " (%d bit)", enc->bits_per_raw_sample);
  1203. if (av_log_get_level() >= AV_LOG_VERBOSE) {
  1204. if (enc->initial_padding)
  1205. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1206. ", delay %d", enc->initial_padding);
  1207. if (enc->trailing_padding)
  1208. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1209. ", padding %d", enc->trailing_padding);
  1210. }
  1211. break;
  1212. case AVMEDIA_TYPE_DATA:
  1213. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1214. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1215. if (g)
  1216. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1217. ", %d/%d",
  1218. enc->time_base.num / g, enc->time_base.den / g);
  1219. }
  1220. break;
  1221. case AVMEDIA_TYPE_SUBTITLE:
  1222. if (enc->width)
  1223. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1224. ", %dx%d", enc->width, enc->height);
  1225. break;
  1226. default:
  1227. return;
  1228. }
  1229. if (encode) {
  1230. if (enc->flags & AV_CODEC_FLAG_PASS1)
  1231. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1232. ", pass 1");
  1233. if (enc->flags & AV_CODEC_FLAG_PASS2)
  1234. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1235. ", pass 2");
  1236. }
  1237. bitrate = get_bit_rate(enc);
  1238. if (bitrate != 0) {
  1239. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1240. ", %"PRId64" kb/s", bitrate / 1000);
  1241. } else if (enc->rc_max_rate > 0) {
  1242. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1243. ", max. %"PRId64" kb/s", enc->rc_max_rate / 1000);
  1244. }
  1245. }
  1246. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1247. {
  1248. const AVProfile *p;
  1249. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1250. return NULL;
  1251. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1252. if (p->profile == profile)
  1253. return p->name;
  1254. return NULL;
  1255. }
  1256. const char *avcodec_profile_name(enum AVCodecID codec_id, int profile)
  1257. {
  1258. const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
  1259. const AVProfile *p;
  1260. if (profile == FF_PROFILE_UNKNOWN || !desc || !desc->profiles)
  1261. return NULL;
  1262. for (p = desc->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1263. if (p->profile == profile)
  1264. return p->name;
  1265. return NULL;
  1266. }
  1267. unsigned avcodec_version(void)
  1268. {
  1269. // av_assert0(AV_CODEC_ID_V410==164);
  1270. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  1271. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  1272. // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
  1273. av_assert0(AV_CODEC_ID_SRT==94216);
  1274. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  1275. return LIBAVCODEC_VERSION_INT;
  1276. }
  1277. const char *avcodec_configuration(void)
  1278. {
  1279. return FFMPEG_CONFIGURATION;
  1280. }
  1281. const char *avcodec_license(void)
  1282. {
  1283. #define LICENSE_PREFIX "libavcodec license: "
  1284. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1285. }
  1286. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  1287. {
  1288. switch (codec_id) {
  1289. case AV_CODEC_ID_8SVX_EXP:
  1290. case AV_CODEC_ID_8SVX_FIB:
  1291. case AV_CODEC_ID_ADPCM_CT:
  1292. case AV_CODEC_ID_ADPCM_IMA_APC:
  1293. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  1294. case AV_CODEC_ID_ADPCM_IMA_OKI:
  1295. case AV_CODEC_ID_ADPCM_IMA_WS:
  1296. case AV_CODEC_ID_ADPCM_G722:
  1297. case AV_CODEC_ID_ADPCM_YAMAHA:
  1298. case AV_CODEC_ID_ADPCM_AICA:
  1299. return 4;
  1300. case AV_CODEC_ID_DSD_LSBF:
  1301. case AV_CODEC_ID_DSD_MSBF:
  1302. case AV_CODEC_ID_DSD_LSBF_PLANAR:
  1303. case AV_CODEC_ID_DSD_MSBF_PLANAR:
  1304. case AV_CODEC_ID_PCM_ALAW:
  1305. case AV_CODEC_ID_PCM_MULAW:
  1306. case AV_CODEC_ID_PCM_VIDC:
  1307. case AV_CODEC_ID_PCM_S8:
  1308. case AV_CODEC_ID_PCM_S8_PLANAR:
  1309. case AV_CODEC_ID_PCM_U8:
  1310. case AV_CODEC_ID_PCM_ZORK:
  1311. case AV_CODEC_ID_SDX2_DPCM:
  1312. return 8;
  1313. case AV_CODEC_ID_PCM_S16BE:
  1314. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  1315. case AV_CODEC_ID_PCM_S16LE:
  1316. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  1317. case AV_CODEC_ID_PCM_U16BE:
  1318. case AV_CODEC_ID_PCM_U16LE:
  1319. return 16;
  1320. case AV_CODEC_ID_PCM_S24DAUD:
  1321. case AV_CODEC_ID_PCM_S24BE:
  1322. case AV_CODEC_ID_PCM_S24LE:
  1323. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  1324. case AV_CODEC_ID_PCM_U24BE:
  1325. case AV_CODEC_ID_PCM_U24LE:
  1326. return 24;
  1327. case AV_CODEC_ID_PCM_S32BE:
  1328. case AV_CODEC_ID_PCM_S32LE:
  1329. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  1330. case AV_CODEC_ID_PCM_U32BE:
  1331. case AV_CODEC_ID_PCM_U32LE:
  1332. case AV_CODEC_ID_PCM_F32BE:
  1333. case AV_CODEC_ID_PCM_F32LE:
  1334. case AV_CODEC_ID_PCM_F24LE:
  1335. case AV_CODEC_ID_PCM_F16LE:
  1336. return 32;
  1337. case AV_CODEC_ID_PCM_F64BE:
  1338. case AV_CODEC_ID_PCM_F64LE:
  1339. case AV_CODEC_ID_PCM_S64BE:
  1340. case AV_CODEC_ID_PCM_S64LE:
  1341. return 64;
  1342. default:
  1343. return 0;
  1344. }
  1345. }
  1346. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  1347. {
  1348. static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
  1349. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  1350. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  1351. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  1352. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  1353. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  1354. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  1355. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  1356. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  1357. [AV_SAMPLE_FMT_S64P] = { AV_CODEC_ID_PCM_S64LE, AV_CODEC_ID_PCM_S64BE },
  1358. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  1359. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  1360. };
  1361. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  1362. return AV_CODEC_ID_NONE;
  1363. if (be < 0 || be > 1)
  1364. be = AV_NE(1, 0);
  1365. return map[fmt][be];
  1366. }
  1367. int av_get_bits_per_sample(enum AVCodecID codec_id)
  1368. {
  1369. switch (codec_id) {
  1370. case AV_CODEC_ID_ADPCM_SBPRO_2:
  1371. return 2;
  1372. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1373. return 3;
  1374. case AV_CODEC_ID_ADPCM_SBPRO_4:
  1375. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1376. case AV_CODEC_ID_ADPCM_IMA_QT:
  1377. case AV_CODEC_ID_ADPCM_SWF:
  1378. case AV_CODEC_ID_ADPCM_MS:
  1379. return 4;
  1380. default:
  1381. return av_get_exact_bits_per_sample(codec_id);
  1382. }
  1383. }
  1384. static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
  1385. uint32_t tag, int bits_per_coded_sample, int64_t bitrate,
  1386. uint8_t * extradata, int frame_size, int frame_bytes)
  1387. {
  1388. int bps = av_get_exact_bits_per_sample(id);
  1389. int framecount = (ba > 0 && frame_bytes / ba > 0) ? frame_bytes / ba : 1;
  1390. /* codecs with an exact constant bits per sample */
  1391. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  1392. return (frame_bytes * 8LL) / (bps * ch);
  1393. bps = bits_per_coded_sample;
  1394. /* codecs with a fixed packet duration */
  1395. switch (id) {
  1396. case AV_CODEC_ID_ADPCM_ADX: return 32;
  1397. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  1398. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  1399. case AV_CODEC_ID_AMR_NB:
  1400. case AV_CODEC_ID_EVRC:
  1401. case AV_CODEC_ID_GSM:
  1402. case AV_CODEC_ID_QCELP:
  1403. case AV_CODEC_ID_RA_288: return 160;
  1404. case AV_CODEC_ID_AMR_WB:
  1405. case AV_CODEC_ID_GSM_MS: return 320;
  1406. case AV_CODEC_ID_MP1: return 384;
  1407. case AV_CODEC_ID_ATRAC1: return 512;
  1408. case AV_CODEC_ID_ATRAC9:
  1409. case AV_CODEC_ID_ATRAC3: return 1024 * framecount;
  1410. case AV_CODEC_ID_ATRAC3P: return 2048;
  1411. case AV_CODEC_ID_MP2:
  1412. case AV_CODEC_ID_MUSEPACK7: return 1152;
  1413. case AV_CODEC_ID_AC3: return 1536;
  1414. }
  1415. if (sr > 0) {
  1416. /* calc from sample rate */
  1417. if (id == AV_CODEC_ID_TTA)
  1418. return 256 * sr / 245;
  1419. else if (id == AV_CODEC_ID_DST)
  1420. return 588 * sr / 44100;
  1421. if (ch > 0) {
  1422. /* calc from sample rate and channels */
  1423. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  1424. return (480 << (sr / 22050)) / ch;
  1425. }
  1426. if (id == AV_CODEC_ID_MP3)
  1427. return sr <= 24000 ? 576 : 1152;
  1428. }
  1429. if (ba > 0) {
  1430. /* calc from block_align */
  1431. if (id == AV_CODEC_ID_SIPR) {
  1432. switch (ba) {
  1433. case 20: return 160;
  1434. case 19: return 144;
  1435. case 29: return 288;
  1436. case 37: return 480;
  1437. }
  1438. } else if (id == AV_CODEC_ID_ILBC) {
  1439. switch (ba) {
  1440. case 38: return 160;
  1441. case 50: return 240;
  1442. }
  1443. }
  1444. }
  1445. if (frame_bytes > 0) {
  1446. /* calc from frame_bytes only */
  1447. if (id == AV_CODEC_ID_TRUESPEECH)
  1448. return 240 * (frame_bytes / 32);
  1449. if (id == AV_CODEC_ID_NELLYMOSER)
  1450. return 256 * (frame_bytes / 64);
  1451. if (id == AV_CODEC_ID_RA_144)
  1452. return 160 * (frame_bytes / 20);
  1453. if (id == AV_CODEC_ID_G723_1)
  1454. return 240 * (frame_bytes / 24);
  1455. if (bps > 0) {
  1456. /* calc from frame_bytes and bits_per_coded_sample */
  1457. if (id == AV_CODEC_ID_ADPCM_G726 || id == AV_CODEC_ID_ADPCM_G726LE)
  1458. return frame_bytes * 8 / bps;
  1459. }
  1460. if (ch > 0 && ch < INT_MAX/16) {
  1461. /* calc from frame_bytes and channels */
  1462. switch (id) {
  1463. case AV_CODEC_ID_ADPCM_AFC:
  1464. return frame_bytes / (9 * ch) * 16;
  1465. case AV_CODEC_ID_ADPCM_PSX:
  1466. case AV_CODEC_ID_ADPCM_DTK:
  1467. return frame_bytes / (16 * ch) * 28;
  1468. case AV_CODEC_ID_ADPCM_4XM:
  1469. case AV_CODEC_ID_ADPCM_IMA_DAT4:
  1470. case AV_CODEC_ID_ADPCM_IMA_ISS:
  1471. return (frame_bytes - 4 * ch) * 2 / ch;
  1472. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  1473. return (frame_bytes - 4) * 2 / ch;
  1474. case AV_CODEC_ID_ADPCM_IMA_AMV:
  1475. return (frame_bytes - 8) * 2 / ch;
  1476. case AV_CODEC_ID_ADPCM_THP:
  1477. case AV_CODEC_ID_ADPCM_THP_LE:
  1478. if (extradata)
  1479. return frame_bytes * 14 / (8 * ch);
  1480. break;
  1481. case AV_CODEC_ID_ADPCM_XA:
  1482. return (frame_bytes / 128) * 224 / ch;
  1483. case AV_CODEC_ID_INTERPLAY_DPCM:
  1484. return (frame_bytes - 6 - ch) / ch;
  1485. case AV_CODEC_ID_ROQ_DPCM:
  1486. return (frame_bytes - 8) / ch;
  1487. case AV_CODEC_ID_XAN_DPCM:
  1488. return (frame_bytes - 2 * ch) / ch;
  1489. case AV_CODEC_ID_MACE3:
  1490. return 3 * frame_bytes / ch;
  1491. case AV_CODEC_ID_MACE6:
  1492. return 6 * frame_bytes / ch;
  1493. case AV_CODEC_ID_PCM_LXF:
  1494. return 2 * (frame_bytes / (5 * ch));
  1495. case AV_CODEC_ID_IAC:
  1496. case AV_CODEC_ID_IMC:
  1497. return 4 * frame_bytes / ch;
  1498. }
  1499. if (tag) {
  1500. /* calc from frame_bytes, channels, and codec_tag */
  1501. if (id == AV_CODEC_ID_SOL_DPCM) {
  1502. if (tag == 3)
  1503. return frame_bytes / ch;
  1504. else
  1505. return frame_bytes * 2 / ch;
  1506. }
  1507. }
  1508. if (ba > 0) {
  1509. /* calc from frame_bytes, channels, and block_align */
  1510. int blocks = frame_bytes / ba;
  1511. switch (id) {
  1512. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1513. if (bps < 2 || bps > 5)
  1514. return 0;
  1515. return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
  1516. case AV_CODEC_ID_ADPCM_IMA_DK3:
  1517. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  1518. case AV_CODEC_ID_ADPCM_IMA_DK4:
  1519. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  1520. case AV_CODEC_ID_ADPCM_IMA_RAD:
  1521. return blocks * ((ba - 4 * ch) * 2 / ch);
  1522. case AV_CODEC_ID_ADPCM_MS:
  1523. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  1524. case AV_CODEC_ID_ADPCM_MTAF:
  1525. return blocks * (ba - 16) * 2 / ch;
  1526. }
  1527. }
  1528. if (bps > 0) {
  1529. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  1530. switch (id) {
  1531. case AV_CODEC_ID_PCM_DVD:
  1532. if(bps<4 || frame_bytes<3)
  1533. return 0;
  1534. return 2 * ((frame_bytes - 3) / ((bps * 2 / 8) * ch));
  1535. case AV_CODEC_ID_PCM_BLURAY:
  1536. if(bps<4 || frame_bytes<4)
  1537. return 0;
  1538. return (frame_bytes - 4) / ((FFALIGN(ch, 2) * bps) / 8);
  1539. case AV_CODEC_ID_S302M:
  1540. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  1541. }
  1542. }
  1543. }
  1544. }
  1545. /* Fall back on using frame_size */
  1546. if (frame_size > 1 && frame_bytes)
  1547. return frame_size;
  1548. //For WMA we currently have no other means to calculate duration thus we
  1549. //do it here by assuming CBR, which is true for all known cases.
  1550. if (bitrate > 0 && frame_bytes > 0 && sr > 0 && ba > 1) {
  1551. if (id == AV_CODEC_ID_WMAV1 || id == AV_CODEC_ID_WMAV2)
  1552. return (frame_bytes * 8LL * sr) / bitrate;
  1553. }
  1554. return 0;
  1555. }
  1556. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1557. {
  1558. return get_audio_frame_duration(avctx->codec_id, avctx->sample_rate,
  1559. avctx->channels, avctx->block_align,
  1560. avctx->codec_tag, avctx->bits_per_coded_sample,
  1561. avctx->bit_rate, avctx->extradata, avctx->frame_size,
  1562. frame_bytes);
  1563. }
  1564. int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
  1565. {
  1566. return get_audio_frame_duration(par->codec_id, par->sample_rate,
  1567. par->channels, par->block_align,
  1568. par->codec_tag, par->bits_per_coded_sample,
  1569. par->bit_rate, par->extradata, par->frame_size,
  1570. frame_bytes);
  1571. }
  1572. #if !HAVE_THREADS
  1573. int ff_thread_init(AVCodecContext *s)
  1574. {
  1575. return -1;
  1576. }
  1577. #endif
  1578. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1579. {
  1580. unsigned int n = 0;
  1581. while (v >= 0xff) {
  1582. *s++ = 0xff;
  1583. v -= 0xff;
  1584. n++;
  1585. }
  1586. *s = v;
  1587. n++;
  1588. return n;
  1589. }
  1590. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  1591. {
  1592. int i;
  1593. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  1594. return i;
  1595. }
  1596. const AVCodecHWConfig *avcodec_get_hw_config(const AVCodec *codec, int index)
  1597. {
  1598. int i;
  1599. if (!codec->hw_configs || index < 0)
  1600. return NULL;
  1601. for (i = 0; i <= index; i++)
  1602. if (!codec->hw_configs[i])
  1603. return NULL;
  1604. return &codec->hw_configs[index]->public;
  1605. }
  1606. #if FF_API_USER_VISIBLE_AVHWACCEL
  1607. AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
  1608. {
  1609. return NULL;
  1610. }
  1611. void av_register_hwaccel(AVHWAccel *hwaccel)
  1612. {
  1613. }
  1614. #endif
  1615. #if FF_API_LOCKMGR
  1616. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1617. {
  1618. return 0;
  1619. }
  1620. #endif
  1621. unsigned int avpriv_toupper4(unsigned int x)
  1622. {
  1623. return av_toupper(x & 0xFF) +
  1624. (av_toupper((x >> 8) & 0xFF) << 8) +
  1625. (av_toupper((x >> 16) & 0xFF) << 16) +
  1626. ((unsigned)av_toupper((x >> 24) & 0xFF) << 24);
  1627. }
  1628. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  1629. {
  1630. int ret;
  1631. dst->owner[0] = src->owner[0];
  1632. dst->owner[1] = src->owner[1];
  1633. ret = av_frame_ref(dst->f, src->f);
  1634. if (ret < 0)
  1635. return ret;
  1636. av_assert0(!dst->progress);
  1637. if (src->progress &&
  1638. !(dst->progress = av_buffer_ref(src->progress))) {
  1639. ff_thread_release_buffer(dst->owner[0], dst);
  1640. return AVERROR(ENOMEM);
  1641. }
  1642. return 0;
  1643. }
  1644. #if !HAVE_THREADS
  1645. enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  1646. {
  1647. return ff_get_format(avctx, fmt);
  1648. }
  1649. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  1650. {
  1651. f->owner[0] = f->owner[1] = avctx;
  1652. return ff_get_buffer(avctx, f->f, flags);
  1653. }
  1654. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  1655. {
  1656. if (f->f)
  1657. av_frame_unref(f->f);
  1658. }
  1659. void ff_thread_finish_setup(AVCodecContext *avctx)
  1660. {
  1661. }
  1662. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  1663. {
  1664. }
  1665. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  1666. {
  1667. }
  1668. int ff_thread_can_start_frame(AVCodecContext *avctx)
  1669. {
  1670. return 1;
  1671. }
  1672. int ff_alloc_entries(AVCodecContext *avctx, int count)
  1673. {
  1674. return 0;
  1675. }
  1676. void ff_reset_entries(AVCodecContext *avctx)
  1677. {
  1678. }
  1679. void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
  1680. {
  1681. }
  1682. void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
  1683. {
  1684. }
  1685. #endif
  1686. int avcodec_is_open(AVCodecContext *s)
  1687. {
  1688. return !!s->internal;
  1689. }
  1690. int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
  1691. {
  1692. int ret;
  1693. char *str;
  1694. ret = av_bprint_finalize(buf, &str);
  1695. if (ret < 0)
  1696. return ret;
  1697. if (!av_bprint_is_complete(buf)) {
  1698. av_free(str);
  1699. return AVERROR(ENOMEM);
  1700. }
  1701. avctx->extradata = str;
  1702. /* Note: the string is NUL terminated (so extradata can be read as a
  1703. * string), but the ending character is not accounted in the size (in
  1704. * binary formats you are likely not supposed to mux that character). When
  1705. * extradata is copied, it is also padded with AV_INPUT_BUFFER_PADDING_SIZE
  1706. * zeros. */
  1707. avctx->extradata_size = buf->len;
  1708. return 0;
  1709. }
  1710. const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
  1711. const uint8_t *end,
  1712. uint32_t *av_restrict state)
  1713. {
  1714. int i;
  1715. av_assert0(p <= end);
  1716. if (p >= end)
  1717. return end;
  1718. for (i = 0; i < 3; i++) {
  1719. uint32_t tmp = *state << 8;
  1720. *state = tmp + *(p++);
  1721. if (tmp == 0x100 || p == end)
  1722. return p;
  1723. }
  1724. while (p < end) {
  1725. if (p[-1] > 1 ) p += 3;
  1726. else if (p[-2] ) p += 2;
  1727. else if (p[-3]|(p[-1]-1)) p++;
  1728. else {
  1729. p++;
  1730. break;
  1731. }
  1732. }
  1733. p = FFMIN(p, end) - 4;
  1734. *state = AV_RB32(p);
  1735. return p + 4;
  1736. }
  1737. AVCPBProperties *av_cpb_properties_alloc(size_t *size)
  1738. {
  1739. AVCPBProperties *props = av_mallocz(sizeof(AVCPBProperties));
  1740. if (!props)
  1741. return NULL;
  1742. if (size)
  1743. *size = sizeof(*props);
  1744. props->vbv_delay = UINT64_MAX;
  1745. return props;
  1746. }
  1747. AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx)
  1748. {
  1749. AVPacketSideData *tmp;
  1750. AVCPBProperties *props;
  1751. size_t size;
  1752. props = av_cpb_properties_alloc(&size);
  1753. if (!props)
  1754. return NULL;
  1755. tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
  1756. if (!tmp) {
  1757. av_freep(&props);
  1758. return NULL;
  1759. }
  1760. avctx->coded_side_data = tmp;
  1761. avctx->nb_coded_side_data++;
  1762. avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
  1763. avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
  1764. avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
  1765. return props;
  1766. }
  1767. static void codec_parameters_reset(AVCodecParameters *par)
  1768. {
  1769. av_freep(&par->extradata);
  1770. memset(par, 0, sizeof(*par));
  1771. par->codec_type = AVMEDIA_TYPE_UNKNOWN;
  1772. par->codec_id = AV_CODEC_ID_NONE;
  1773. par->format = -1;
  1774. par->field_order = AV_FIELD_UNKNOWN;
  1775. par->color_range = AVCOL_RANGE_UNSPECIFIED;
  1776. par->color_primaries = AVCOL_PRI_UNSPECIFIED;
  1777. par->color_trc = AVCOL_TRC_UNSPECIFIED;
  1778. par->color_space = AVCOL_SPC_UNSPECIFIED;
  1779. par->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
  1780. par->sample_aspect_ratio = (AVRational){ 0, 1 };
  1781. par->profile = FF_PROFILE_UNKNOWN;
  1782. par->level = FF_LEVEL_UNKNOWN;
  1783. }
  1784. AVCodecParameters *avcodec_parameters_alloc(void)
  1785. {
  1786. AVCodecParameters *par = av_mallocz(sizeof(*par));
  1787. if (!par)
  1788. return NULL;
  1789. codec_parameters_reset(par);
  1790. return par;
  1791. }
  1792. void avcodec_parameters_free(AVCodecParameters **ppar)
  1793. {
  1794. AVCodecParameters *par = *ppar;
  1795. if (!par)
  1796. return;
  1797. codec_parameters_reset(par);
  1798. av_freep(ppar);
  1799. }
  1800. int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
  1801. {
  1802. codec_parameters_reset(dst);
  1803. memcpy(dst, src, sizeof(*dst));
  1804. dst->extradata = NULL;
  1805. dst->extradata_size = 0;
  1806. if (src->extradata) {
  1807. dst->extradata = av_mallocz(src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  1808. if (!dst->extradata)
  1809. return AVERROR(ENOMEM);
  1810. memcpy(dst->extradata, src->extradata, src->extradata_size);
  1811. dst->extradata_size = src->extradata_size;
  1812. }
  1813. return 0;
  1814. }
  1815. int avcodec_parameters_from_context(AVCodecParameters *par,
  1816. const AVCodecContext *codec)
  1817. {
  1818. codec_parameters_reset(par);
  1819. par->codec_type = codec->codec_type;
  1820. par->codec_id = codec->codec_id;
  1821. par->codec_tag = codec->codec_tag;
  1822. par->bit_rate = codec->bit_rate;
  1823. par->bits_per_coded_sample = codec->bits_per_coded_sample;
  1824. par->bits_per_raw_sample = codec->bits_per_raw_sample;
  1825. par->profile = codec->profile;
  1826. par->level = codec->level;
  1827. switch (par->codec_type) {
  1828. case AVMEDIA_TYPE_VIDEO:
  1829. par->format = codec->pix_fmt;
  1830. par->width = codec->width;
  1831. par->height = codec->height;
  1832. par->field_order = codec->field_order;
  1833. par->color_range = codec->color_range;
  1834. par->color_primaries = codec->color_primaries;
  1835. par->color_trc = codec->color_trc;
  1836. par->color_space = codec->colorspace;
  1837. par->chroma_location = codec->chroma_sample_location;
  1838. par->sample_aspect_ratio = codec->sample_aspect_ratio;
  1839. par->video_delay = codec->has_b_frames;
  1840. break;
  1841. case AVMEDIA_TYPE_AUDIO:
  1842. par->format = codec->sample_fmt;
  1843. par->channel_layout = codec->channel_layout;
  1844. par->channels = codec->channels;
  1845. par->sample_rate = codec->sample_rate;
  1846. par->block_align = codec->block_align;
  1847. par->frame_size = codec->frame_size;
  1848. par->initial_padding = codec->initial_padding;
  1849. par->trailing_padding = codec->trailing_padding;
  1850. par->seek_preroll = codec->seek_preroll;
  1851. break;
  1852. case AVMEDIA_TYPE_SUBTITLE:
  1853. par->width = codec->width;
  1854. par->height = codec->height;
  1855. break;
  1856. }
  1857. if (codec->extradata) {
  1858. par->extradata = av_mallocz(codec->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  1859. if (!par->extradata)
  1860. return AVERROR(ENOMEM);
  1861. memcpy(par->extradata, codec->extradata, codec->extradata_size);
  1862. par->extradata_size = codec->extradata_size;
  1863. }
  1864. return 0;
  1865. }
  1866. int avcodec_parameters_to_context(AVCodecContext *codec,
  1867. const AVCodecParameters *par)
  1868. {
  1869. codec->codec_type = par->codec_type;
  1870. codec->codec_id = par->codec_id;
  1871. codec->codec_tag = par->codec_tag;
  1872. codec->bit_rate = par->bit_rate;
  1873. codec->bits_per_coded_sample = par->bits_per_coded_sample;
  1874. codec->bits_per_raw_sample = par->bits_per_raw_sample;
  1875. codec->profile = par->profile;
  1876. codec->level = par->level;
  1877. switch (par->codec_type) {
  1878. case AVMEDIA_TYPE_VIDEO:
  1879. codec->pix_fmt = par->format;
  1880. codec->width = par->width;
  1881. codec->height = par->height;
  1882. codec->field_order = par->field_order;
  1883. codec->color_range = par->color_range;
  1884. codec->color_primaries = par->color_primaries;
  1885. codec->color_trc = par->color_trc;
  1886. codec->colorspace = par->color_space;
  1887. codec->chroma_sample_location = par->chroma_location;
  1888. codec->sample_aspect_ratio = par->sample_aspect_ratio;
  1889. codec->has_b_frames = par->video_delay;
  1890. break;
  1891. case AVMEDIA_TYPE_AUDIO:
  1892. codec->sample_fmt = par->format;
  1893. codec->channel_layout = par->channel_layout;
  1894. codec->channels = par->channels;
  1895. codec->sample_rate = par->sample_rate;
  1896. codec->block_align = par->block_align;
  1897. codec->frame_size = par->frame_size;
  1898. codec->delay =
  1899. codec->initial_padding = par->initial_padding;
  1900. codec->trailing_padding = par->trailing_padding;
  1901. codec->seek_preroll = par->seek_preroll;
  1902. break;
  1903. case AVMEDIA_TYPE_SUBTITLE:
  1904. codec->width = par->width;
  1905. codec->height = par->height;
  1906. break;
  1907. }
  1908. if (par->extradata) {
  1909. av_freep(&codec->extradata);
  1910. codec->extradata = av_mallocz(par->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  1911. if (!codec->extradata)
  1912. return AVERROR(ENOMEM);
  1913. memcpy(codec->extradata, par->extradata, par->extradata_size);
  1914. codec->extradata_size = par->extradata_size;
  1915. }
  1916. return 0;
  1917. }
  1918. int ff_alloc_a53_sei(const AVFrame *frame, size_t prefix_len,
  1919. void **data, size_t *sei_size)
  1920. {
  1921. AVFrameSideData *side_data = NULL;
  1922. uint8_t *sei_data;
  1923. if (frame)
  1924. side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_A53_CC);
  1925. if (!side_data) {
  1926. *data = NULL;
  1927. return 0;
  1928. }
  1929. *sei_size = side_data->size + 11;
  1930. *data = av_mallocz(*sei_size + prefix_len);
  1931. if (!*data)
  1932. return AVERROR(ENOMEM);
  1933. sei_data = (uint8_t*)*data + prefix_len;
  1934. // country code
  1935. sei_data[0] = 181;
  1936. sei_data[1] = 0;
  1937. sei_data[2] = 49;
  1938. /**
  1939. * 'GA94' is standard in North America for ATSC, but hard coding
  1940. * this style may not be the right thing to do -- other formats
  1941. * do exist. This information is not available in the side_data
  1942. * so we are going with this right now.
  1943. */
  1944. AV_WL32(sei_data + 3, MKTAG('G', 'A', '9', '4'));
  1945. sei_data[7] = 3;
  1946. sei_data[8] = ((side_data->size/3) & 0x1f) | 0x40;
  1947. sei_data[9] = 0;
  1948. memcpy(sei_data + 10, side_data->data, side_data->size);
  1949. sei_data[side_data->size+10] = 255;
  1950. return 0;
  1951. }
  1952. int64_t ff_guess_coded_bitrate(AVCodecContext *avctx)
  1953. {
  1954. AVRational framerate = avctx->framerate;
  1955. int bits_per_coded_sample = avctx->bits_per_coded_sample;
  1956. int64_t bitrate;
  1957. if (!(framerate.num && framerate.den))
  1958. framerate = av_inv_q(avctx->time_base);
  1959. if (!(framerate.num && framerate.den))
  1960. return 0;
  1961. if (!bits_per_coded_sample) {
  1962. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  1963. bits_per_coded_sample = av_get_bits_per_pixel(desc);
  1964. }
  1965. bitrate = (int64_t)bits_per_coded_sample * avctx->width * avctx->height *
  1966. framerate.num / framerate.den;
  1967. return bitrate;
  1968. }