utils.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  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 "libavutil/avstring.h"
  27. #include "libavutil/integer.h"
  28. #include "libavutil/crc.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libavutil/audioconvert.h"
  31. #include "libavutil/imgutils.h"
  32. #include "libavutil/samplefmt.h"
  33. #include "avcodec.h"
  34. #include "dsputil.h"
  35. #include "libavutil/opt.h"
  36. #include "imgconvert.h"
  37. #include "thread.h"
  38. #include "audioconvert.h"
  39. #include "internal.h"
  40. #include <stdlib.h>
  41. #include <stdarg.h>
  42. #include <limits.h>
  43. #include <float.h>
  44. static int volatile entangled_thread_counter=0;
  45. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  46. static void *codec_mutex;
  47. void *av_fast_realloc(void *ptr, unsigned int *size, FF_INTERNALC_MEM_TYPE min_size)
  48. {
  49. if(min_size < *size)
  50. return ptr;
  51. min_size= FFMAX(17*min_size/16 + 32, min_size);
  52. ptr= av_realloc(ptr, min_size);
  53. if(!ptr) //we could set this to the unmodified min_size but this is safer if the user lost the ptr and uses NULL now
  54. min_size= 0;
  55. *size= min_size;
  56. return ptr;
  57. }
  58. void av_fast_malloc(void *ptr, unsigned int *size, FF_INTERNALC_MEM_TYPE min_size)
  59. {
  60. void **p = ptr;
  61. if (min_size < *size)
  62. return;
  63. min_size= FFMAX(17*min_size/16 + 32, min_size);
  64. av_free(*p);
  65. *p = av_malloc(min_size);
  66. if (!*p) min_size = 0;
  67. *size= min_size;
  68. }
  69. /* encoder management */
  70. static AVCodec *first_avcodec = NULL;
  71. AVCodec *av_codec_next(AVCodec *c){
  72. if(c) return c->next;
  73. else return first_avcodec;
  74. }
  75. void avcodec_register(AVCodec *codec)
  76. {
  77. AVCodec **p;
  78. avcodec_init();
  79. p = &first_avcodec;
  80. while (*p != NULL) p = &(*p)->next;
  81. *p = codec;
  82. codec->next = NULL;
  83. }
  84. #if LIBAVCODEC_VERSION_MAJOR < 53
  85. void register_avcodec(AVCodec *codec)
  86. {
  87. avcodec_register(codec);
  88. }
  89. #endif
  90. unsigned avcodec_get_edge_width(void)
  91. {
  92. return EDGE_WIDTH;
  93. }
  94. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  95. s->coded_width = width;
  96. s->coded_height= height;
  97. s->width = -((-width )>>s->lowres);
  98. s->height= -((-height)>>s->lowres);
  99. }
  100. typedef struct InternalBuffer{
  101. int last_pic_num;
  102. uint8_t *base[4];
  103. uint8_t *data[4];
  104. int linesize[4];
  105. int width, height;
  106. enum PixelFormat pix_fmt;
  107. }InternalBuffer;
  108. #define INTERNAL_BUFFER_SIZE 32
  109. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
  110. int w_align= 1;
  111. int h_align= 1;
  112. switch(s->pix_fmt){
  113. case PIX_FMT_YUV420P:
  114. case PIX_FMT_YUYV422:
  115. case PIX_FMT_UYVY422:
  116. case PIX_FMT_YUV422P:
  117. case PIX_FMT_YUV440P:
  118. case PIX_FMT_YUV444P:
  119. case PIX_FMT_GRAY8:
  120. case PIX_FMT_GRAY16BE:
  121. case PIX_FMT_GRAY16LE:
  122. case PIX_FMT_YUVJ420P:
  123. case PIX_FMT_YUVJ422P:
  124. case PIX_FMT_YUVJ440P:
  125. case PIX_FMT_YUVJ444P:
  126. case PIX_FMT_YUVA420P:
  127. w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
  128. h_align= 16;
  129. if(s->codec_id == CODEC_ID_MPEG2VIDEO || s->codec_id == CODEC_ID_MJPEG || s->codec_id == CODEC_ID_AMV || s->codec_id == CODEC_ID_THP || s->codec_id == CODEC_ID_H264)
  130. h_align= 32; // interlaced is rounded up to 2 MBs
  131. break;
  132. case PIX_FMT_YUV411P:
  133. case PIX_FMT_UYYVYY411:
  134. w_align=32;
  135. h_align=8;
  136. break;
  137. case PIX_FMT_YUV410P:
  138. if(s->codec_id == CODEC_ID_SVQ1){
  139. w_align=64;
  140. h_align=64;
  141. }
  142. case PIX_FMT_RGB555:
  143. if(s->codec_id == CODEC_ID_RPZA){
  144. w_align=4;
  145. h_align=4;
  146. }
  147. case PIX_FMT_PAL8:
  148. case PIX_FMT_BGR8:
  149. case PIX_FMT_RGB8:
  150. if(s->codec_id == CODEC_ID_SMC){
  151. w_align=4;
  152. h_align=4;
  153. }
  154. break;
  155. case PIX_FMT_BGR24:
  156. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  157. w_align=4;
  158. h_align=4;
  159. }
  160. break;
  161. default:
  162. w_align= 1;
  163. h_align= 1;
  164. break;
  165. }
  166. *width = FFALIGN(*width , w_align);
  167. *height= FFALIGN(*height, h_align);
  168. if(s->codec_id == CODEC_ID_H264 || s->lowres)
  169. *height+=2; // some of the optimized chroma MC reads one line too much
  170. // which is also done in mpeg decoders with lowres > 0
  171. linesize_align[0] =
  172. linesize_align[1] =
  173. linesize_align[2] =
  174. linesize_align[3] = STRIDE_ALIGN;
  175. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  176. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  177. //picture size unneccessarily in some cases. The solution here is not
  178. //pretty and better ideas are welcome!
  179. #if HAVE_MMX
  180. if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
  181. s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
  182. s->codec_id == CODEC_ID_VP6A) {
  183. linesize_align[0] =
  184. linesize_align[1] =
  185. linesize_align[2] = 16;
  186. }
  187. #endif
  188. }
  189. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  190. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  191. int linesize_align[4];
  192. int align;
  193. avcodec_align_dimensions2(s, width, height, linesize_align);
  194. align = FFMAX(linesize_align[0], linesize_align[3]);
  195. linesize_align[1] <<= chroma_shift;
  196. linesize_align[2] <<= chroma_shift;
  197. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  198. *width=FFALIGN(*width, align);
  199. }
  200. #if LIBAVCODEC_VERSION_MAJOR < 53
  201. int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
  202. return av_image_check_size(w, h, 0, av_log_ctx);
  203. }
  204. #endif
  205. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  206. int i;
  207. int w= s->width;
  208. int h= s->height;
  209. InternalBuffer *buf;
  210. int *picture_number;
  211. if(pic->data[0]!=NULL) {
  212. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  213. return -1;
  214. }
  215. if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
  216. av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
  217. return -1;
  218. }
  219. if(av_image_check_size(w, h, 0, s))
  220. return -1;
  221. if(s->internal_buffer==NULL){
  222. s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
  223. }
  224. #if 0
  225. s->internal_buffer= av_fast_realloc(
  226. s->internal_buffer,
  227. &s->internal_buffer_size,
  228. sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
  229. );
  230. #endif
  231. buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  232. picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
  233. (*picture_number)++;
  234. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  235. if(s->active_thread_type&FF_THREAD_FRAME) {
  236. av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
  237. return -1;
  238. }
  239. for(i=0; i<4; i++){
  240. av_freep(&buf->base[i]);
  241. buf->data[i]= NULL;
  242. }
  243. }
  244. if(buf->base[0]){
  245. pic->age= *picture_number - buf->last_pic_num;
  246. buf->last_pic_num= *picture_number;
  247. }else{
  248. int h_chroma_shift, v_chroma_shift;
  249. int size[4] = {0};
  250. int tmpsize;
  251. int unaligned;
  252. AVPicture picture;
  253. int stride_align[4];
  254. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  255. avcodec_align_dimensions2(s, &w, &h, stride_align);
  256. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  257. w+= EDGE_WIDTH*2;
  258. h+= EDGE_WIDTH*2;
  259. }
  260. do {
  261. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  262. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  263. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  264. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  265. w += w & ~(w-1);
  266. unaligned = 0;
  267. for (i=0; i<4; i++){
  268. unaligned |= picture.linesize[i] % stride_align[i];
  269. }
  270. } while (unaligned);
  271. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  272. if (tmpsize < 0)
  273. return -1;
  274. for (i=0; i<3 && picture.data[i+1]; i++)
  275. size[i] = picture.data[i+1] - picture.data[i];
  276. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  277. buf->last_pic_num= -256*256*256*64;
  278. memset(buf->base, 0, sizeof(buf->base));
  279. memset(buf->data, 0, sizeof(buf->data));
  280. for(i=0; i<4 && size[i]; i++){
  281. const int h_shift= i==0 ? 0 : h_chroma_shift;
  282. const int v_shift= i==0 ? 0 : v_chroma_shift;
  283. buf->linesize[i]= picture.linesize[i];
  284. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  285. if(buf->base[i]==NULL) return -1;
  286. memset(buf->base[i], 128, size[i]);
  287. // no edge if EDGE EMU or not planar YUV
  288. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  289. buf->data[i] = buf->base[i];
  290. else
  291. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), stride_align[i]);
  292. }
  293. if(size[1] && !size[2])
  294. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  295. buf->width = s->width;
  296. buf->height = s->height;
  297. buf->pix_fmt= s->pix_fmt;
  298. pic->age= 256*256*256*64;
  299. }
  300. pic->type= FF_BUFFER_TYPE_INTERNAL;
  301. for(i=0; i<4; i++){
  302. pic->base[i]= buf->base[i];
  303. pic->data[i]= buf->data[i];
  304. pic->linesize[i]= buf->linesize[i];
  305. }
  306. s->internal_buffer_count++;
  307. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  308. else pic->pkt_pts= AV_NOPTS_VALUE;
  309. pic->reordered_opaque= s->reordered_opaque;
  310. if(s->debug&FF_DEBUG_BUFFERS)
  311. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  312. return 0;
  313. }
  314. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  315. int i;
  316. InternalBuffer *buf, *last;
  317. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  318. assert(s->internal_buffer_count);
  319. buf = NULL; /* avoids warning */
  320. for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  321. buf= &((InternalBuffer*)s->internal_buffer)[i];
  322. if(buf->data[0] == pic->data[0])
  323. break;
  324. }
  325. assert(i < s->internal_buffer_count);
  326. s->internal_buffer_count--;
  327. last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  328. FFSWAP(InternalBuffer, *buf, *last);
  329. for(i=0; i<4; i++){
  330. pic->data[i]=NULL;
  331. // pic->base[i]=NULL;
  332. }
  333. //printf("R%X\n", pic->opaque);
  334. if(s->debug&FF_DEBUG_BUFFERS)
  335. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  336. }
  337. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  338. AVFrame temp_pic;
  339. int i;
  340. /* If no picture return a new buffer */
  341. if(pic->data[0] == NULL) {
  342. /* We will copy from buffer, so must be readable */
  343. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  344. return s->get_buffer(s, pic);
  345. }
  346. /* If internal buffer type return the same buffer */
  347. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  348. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  349. else pic->pkt_pts= AV_NOPTS_VALUE;
  350. pic->reordered_opaque= s->reordered_opaque;
  351. return 0;
  352. }
  353. /*
  354. * Not internal type and reget_buffer not overridden, emulate cr buffer
  355. */
  356. temp_pic = *pic;
  357. for(i = 0; i < 4; i++)
  358. pic->data[i] = pic->base[i] = NULL;
  359. pic->opaque = NULL;
  360. /* Allocate new frame */
  361. if (s->get_buffer(s, pic))
  362. return -1;
  363. /* Copy image data from old buffer to new buffer */
  364. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  365. s->height);
  366. s->release_buffer(s, &temp_pic); // Release old frame
  367. return 0;
  368. }
  369. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  370. int i;
  371. for(i=0; i<count; i++){
  372. int r= func(c, (char*)arg + i*size);
  373. if(ret) ret[i]= r;
  374. }
  375. return 0;
  376. }
  377. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  378. int i;
  379. for(i=0; i<count; i++){
  380. int r= func(c, arg, i, 0);
  381. if(ret) ret[i]= r;
  382. }
  383. return 0;
  384. }
  385. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  386. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  387. ++fmt;
  388. return fmt[0];
  389. }
  390. void avcodec_get_frame_defaults(AVFrame *pic){
  391. memset(pic, 0, sizeof(AVFrame));
  392. pic->pts= AV_NOPTS_VALUE;
  393. pic->key_frame= 1;
  394. }
  395. AVFrame *avcodec_alloc_frame(void){
  396. AVFrame *pic= av_malloc(sizeof(AVFrame));
  397. if(pic==NULL) return NULL;
  398. avcodec_get_frame_defaults(pic);
  399. return pic;
  400. }
  401. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  402. {
  403. int ret= -1;
  404. /* If there is a user-supplied mutex locking routine, call it. */
  405. if (ff_lockmgr_cb) {
  406. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  407. return -1;
  408. }
  409. entangled_thread_counter++;
  410. if(entangled_thread_counter != 1){
  411. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  412. goto end;
  413. }
  414. if(avctx->codec || !codec)
  415. goto end;
  416. if (codec->priv_data_size > 0) {
  417. if(!avctx->priv_data){
  418. avctx->priv_data = av_mallocz(codec->priv_data_size);
  419. if (!avctx->priv_data) {
  420. ret = AVERROR(ENOMEM);
  421. goto end;
  422. }
  423. if(codec->priv_class){ //this can be droped once all user apps use avcodec_get_context_defaults3()
  424. *(AVClass**)avctx->priv_data= codec->priv_class;
  425. av_opt_set_defaults(avctx->priv_data);
  426. }
  427. }
  428. } else {
  429. avctx->priv_data = NULL;
  430. }
  431. if(avctx->coded_width && avctx->coded_height)
  432. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  433. else if(avctx->width && avctx->height)
  434. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  435. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  436. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  437. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  438. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  439. avcodec_set_dimensions(avctx, 0, 0);
  440. }
  441. /* if the decoder init function was already called previously,
  442. free the already allocated subtitle_header before overwriting it */
  443. if (codec->decode)
  444. av_freep(&avctx->subtitle_header);
  445. #define SANE_NB_CHANNELS 128U
  446. if (avctx->channels > SANE_NB_CHANNELS) {
  447. ret = AVERROR(EINVAL);
  448. goto free_and_end;
  449. }
  450. avctx->codec = codec;
  451. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  452. avctx->codec_id == CODEC_ID_NONE) {
  453. avctx->codec_type = codec->type;
  454. avctx->codec_id = codec->id;
  455. }
  456. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  457. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  458. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  459. goto free_and_end;
  460. }
  461. avctx->frame_number = 0;
  462. if (HAVE_THREADS && !avctx->thread_opaque) {
  463. ret = ff_thread_init(avctx, avctx->thread_count);
  464. if (ret < 0) {
  465. goto free_and_end;
  466. }
  467. }
  468. if (avctx->codec->max_lowres < avctx->lowres) {
  469. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  470. avctx->codec->max_lowres);
  471. goto free_and_end;
  472. }
  473. if (avctx->codec->sample_fmts && avctx->codec->encode) {
  474. int i;
  475. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  476. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  477. break;
  478. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  479. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  480. goto free_and_end;
  481. }
  482. }
  483. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  484. ret = avctx->codec->init(avctx);
  485. if (ret < 0) {
  486. goto free_and_end;
  487. }
  488. }
  489. ret=0;
  490. end:
  491. entangled_thread_counter--;
  492. /* Release any user-supplied mutex. */
  493. if (ff_lockmgr_cb) {
  494. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  495. }
  496. return ret;
  497. free_and_end:
  498. av_freep(&avctx->priv_data);
  499. avctx->codec= NULL;
  500. goto end;
  501. }
  502. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  503. const short *samples)
  504. {
  505. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  506. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  507. return -1;
  508. }
  509. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  510. int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
  511. avctx->frame_number++;
  512. return ret;
  513. }else
  514. return 0;
  515. }
  516. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  517. const AVFrame *pict)
  518. {
  519. if(buf_size < FF_MIN_BUFFER_SIZE){
  520. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  521. return -1;
  522. }
  523. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  524. return -1;
  525. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  526. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  527. avctx->frame_number++;
  528. emms_c(); //needed to avoid an emms_c() call before every return;
  529. return ret;
  530. }else
  531. return 0;
  532. }
  533. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  534. const AVSubtitle *sub)
  535. {
  536. int ret;
  537. if(sub->start_display_time) {
  538. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  539. return -1;
  540. }
  541. if(sub->num_rects == 0 || !sub->rects)
  542. return -1;
  543. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  544. avctx->frame_number++;
  545. return ret;
  546. }
  547. #if FF_API_VIDEO_OLD
  548. int attribute_align_arg avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  549. int *got_picture_ptr,
  550. const uint8_t *buf, int buf_size)
  551. {
  552. AVPacket avpkt;
  553. av_init_packet(&avpkt);
  554. avpkt.data = buf;
  555. avpkt.size = buf_size;
  556. // HACK for CorePNG to decode as normal PNG by default
  557. avpkt.flags = AV_PKT_FLAG_KEY;
  558. return avcodec_decode_video2(avctx, picture, got_picture_ptr, &avpkt);
  559. }
  560. #endif
  561. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  562. int *got_picture_ptr,
  563. AVPacket *avpkt)
  564. {
  565. int ret;
  566. *got_picture_ptr= 0;
  567. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  568. return -1;
  569. avctx->pkt = avpkt;
  570. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  571. if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  572. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  573. avpkt);
  574. else {
  575. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  576. avpkt);
  577. picture->pkt_dts= avpkt->dts;
  578. }
  579. emms_c(); //needed to avoid an emms_c() call before every return;
  580. if (*got_picture_ptr)
  581. avctx->frame_number++;
  582. }else
  583. ret= 0;
  584. return ret;
  585. }
  586. #if FF_API_AUDIO_OLD
  587. int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,
  588. int *frame_size_ptr,
  589. const uint8_t *buf, int buf_size)
  590. {
  591. AVPacket avpkt;
  592. av_init_packet(&avpkt);
  593. avpkt.data = buf;
  594. avpkt.size = buf_size;
  595. return avcodec_decode_audio3(avctx, samples, frame_size_ptr, &avpkt);
  596. }
  597. #endif
  598. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  599. int *frame_size_ptr,
  600. AVPacket *avpkt)
  601. {
  602. int ret;
  603. avctx->pkt = avpkt;
  604. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  605. //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
  606. if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
  607. av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
  608. return -1;
  609. }
  610. if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
  611. *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
  612. av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
  613. return -1;
  614. }
  615. ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
  616. avctx->frame_number++;
  617. }else{
  618. ret= 0;
  619. *frame_size_ptr=0;
  620. }
  621. return ret;
  622. }
  623. #if FF_API_SUBTITLE_OLD
  624. int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
  625. int *got_sub_ptr,
  626. const uint8_t *buf, int buf_size)
  627. {
  628. AVPacket avpkt;
  629. av_init_packet(&avpkt);
  630. avpkt.data = buf;
  631. avpkt.size = buf_size;
  632. return avcodec_decode_subtitle2(avctx, sub, got_sub_ptr, &avpkt);
  633. }
  634. #endif
  635. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  636. int *got_sub_ptr,
  637. AVPacket *avpkt)
  638. {
  639. int ret;
  640. avctx->pkt = avpkt;
  641. *got_sub_ptr = 0;
  642. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  643. if (*got_sub_ptr)
  644. avctx->frame_number++;
  645. return ret;
  646. }
  647. void avsubtitle_free(AVSubtitle *sub)
  648. {
  649. int i;
  650. for (i = 0; i < sub->num_rects; i++)
  651. {
  652. av_freep(&sub->rects[i]->pict.data[0]);
  653. av_freep(&sub->rects[i]->pict.data[1]);
  654. av_freep(&sub->rects[i]->pict.data[2]);
  655. av_freep(&sub->rects[i]->pict.data[3]);
  656. av_freep(&sub->rects[i]->text);
  657. av_freep(&sub->rects[i]->ass);
  658. av_freep(&sub->rects[i]);
  659. }
  660. av_freep(&sub->rects);
  661. memset(sub, 0, sizeof(AVSubtitle));
  662. }
  663. av_cold int avcodec_close(AVCodecContext *avctx)
  664. {
  665. /* If there is a user-supplied mutex locking routine, call it. */
  666. if (ff_lockmgr_cb) {
  667. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  668. return -1;
  669. }
  670. entangled_thread_counter++;
  671. if(entangled_thread_counter != 1){
  672. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  673. entangled_thread_counter--;
  674. return -1;
  675. }
  676. if (HAVE_THREADS && avctx->thread_opaque)
  677. ff_thread_free(avctx);
  678. if (avctx->codec && avctx->codec->close)
  679. avctx->codec->close(avctx);
  680. avcodec_default_free_buffers(avctx);
  681. avctx->coded_frame = NULL;
  682. av_freep(&avctx->priv_data);
  683. if(avctx->codec && avctx->codec->encode)
  684. av_freep(&avctx->extradata);
  685. avctx->codec = NULL;
  686. avctx->active_thread_type = 0;
  687. entangled_thread_counter--;
  688. /* Release any user-supplied mutex. */
  689. if (ff_lockmgr_cb) {
  690. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  691. }
  692. return 0;
  693. }
  694. AVCodec *avcodec_find_encoder(enum CodecID id)
  695. {
  696. AVCodec *p, *experimental=NULL;
  697. p = first_avcodec;
  698. while (p) {
  699. if (p->encode != NULL && p->id == id) {
  700. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  701. experimental = p;
  702. } else
  703. return p;
  704. }
  705. p = p->next;
  706. }
  707. return experimental;
  708. }
  709. AVCodec *avcodec_find_encoder_by_name(const char *name)
  710. {
  711. AVCodec *p;
  712. if (!name)
  713. return NULL;
  714. p = first_avcodec;
  715. while (p) {
  716. if (p->encode != NULL && strcmp(name,p->name) == 0)
  717. return p;
  718. p = p->next;
  719. }
  720. return NULL;
  721. }
  722. AVCodec *avcodec_find_decoder(enum CodecID id)
  723. {
  724. AVCodec *p;
  725. p = first_avcodec;
  726. while (p) {
  727. if (p->decode != NULL && p->id == id)
  728. return p;
  729. p = p->next;
  730. }
  731. return NULL;
  732. }
  733. AVCodec *avcodec_find_decoder_by_name(const char *name)
  734. {
  735. AVCodec *p;
  736. if (!name)
  737. return NULL;
  738. p = first_avcodec;
  739. while (p) {
  740. if (p->decode != NULL && strcmp(name,p->name) == 0)
  741. return p;
  742. p = p->next;
  743. }
  744. return NULL;
  745. }
  746. static int get_bit_rate(AVCodecContext *ctx)
  747. {
  748. int bit_rate;
  749. int bits_per_sample;
  750. switch(ctx->codec_type) {
  751. case AVMEDIA_TYPE_VIDEO:
  752. case AVMEDIA_TYPE_DATA:
  753. case AVMEDIA_TYPE_SUBTITLE:
  754. case AVMEDIA_TYPE_ATTACHMENT:
  755. bit_rate = ctx->bit_rate;
  756. break;
  757. case AVMEDIA_TYPE_AUDIO:
  758. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  759. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  760. break;
  761. default:
  762. bit_rate = 0;
  763. break;
  764. }
  765. return bit_rate;
  766. }
  767. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  768. {
  769. int i, len, ret = 0;
  770. for (i = 0; i < 4; i++) {
  771. len = snprintf(buf, buf_size,
  772. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  773. buf += len;
  774. buf_size = buf_size > len ? buf_size - len : 0;
  775. ret += len;
  776. codec_tag>>=8;
  777. }
  778. return ret;
  779. }
  780. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  781. {
  782. const char *codec_name;
  783. const char *profile = NULL;
  784. AVCodec *p;
  785. char buf1[32];
  786. int bitrate;
  787. AVRational display_aspect_ratio;
  788. if (encode)
  789. p = avcodec_find_encoder(enc->codec_id);
  790. else
  791. p = avcodec_find_decoder(enc->codec_id);
  792. if (p) {
  793. codec_name = p->name;
  794. profile = av_get_profile_name(p, enc->profile);
  795. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  796. /* fake mpeg2 transport stream codec (currently not
  797. registered) */
  798. codec_name = "mpeg2ts";
  799. } else if (enc->codec_name[0] != '\0') {
  800. codec_name = enc->codec_name;
  801. } else {
  802. /* output avi tags */
  803. char tag_buf[32];
  804. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  805. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  806. codec_name = buf1;
  807. }
  808. switch(enc->codec_type) {
  809. case AVMEDIA_TYPE_VIDEO:
  810. snprintf(buf, buf_size,
  811. "Video: %s%s",
  812. codec_name, enc->mb_decision ? " (hq)" : "");
  813. if (profile)
  814. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  815. " (%s)", profile);
  816. if (enc->pix_fmt != PIX_FMT_NONE) {
  817. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  818. ", %s",
  819. avcodec_get_pix_fmt_name(enc->pix_fmt));
  820. }
  821. if (enc->width) {
  822. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  823. ", %dx%d",
  824. enc->width, enc->height);
  825. if (enc->sample_aspect_ratio.num) {
  826. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  827. enc->width*enc->sample_aspect_ratio.num,
  828. enc->height*enc->sample_aspect_ratio.den,
  829. 1024*1024);
  830. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  831. " [PAR %d:%d DAR %d:%d]",
  832. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  833. display_aspect_ratio.num, display_aspect_ratio.den);
  834. }
  835. if(av_log_get_level() >= AV_LOG_DEBUG){
  836. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  837. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  838. ", %d/%d",
  839. enc->time_base.num/g, enc->time_base.den/g);
  840. }
  841. }
  842. if (encode) {
  843. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  844. ", q=%d-%d", enc->qmin, enc->qmax);
  845. }
  846. break;
  847. case AVMEDIA_TYPE_AUDIO:
  848. snprintf(buf, buf_size,
  849. "Audio: %s",
  850. codec_name);
  851. if (profile)
  852. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  853. " (%s)", profile);
  854. if (enc->sample_rate) {
  855. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  856. ", %d Hz", enc->sample_rate);
  857. }
  858. av_strlcat(buf, ", ", buf_size);
  859. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  860. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  861. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  862. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  863. }
  864. break;
  865. case AVMEDIA_TYPE_DATA:
  866. snprintf(buf, buf_size, "Data: %s", codec_name);
  867. break;
  868. case AVMEDIA_TYPE_SUBTITLE:
  869. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  870. break;
  871. case AVMEDIA_TYPE_ATTACHMENT:
  872. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  873. break;
  874. default:
  875. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  876. return;
  877. }
  878. if (encode) {
  879. if (enc->flags & CODEC_FLAG_PASS1)
  880. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  881. ", pass 1");
  882. if (enc->flags & CODEC_FLAG_PASS2)
  883. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  884. ", pass 2");
  885. }
  886. bitrate = get_bit_rate(enc);
  887. if (bitrate != 0) {
  888. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  889. ", %d kb/s", bitrate / 1000);
  890. }
  891. }
  892. const char *av_get_profile_name(const AVCodec *codec, int profile)
  893. {
  894. const AVProfile *p;
  895. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  896. return NULL;
  897. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  898. if (p->profile == profile)
  899. return p->name;
  900. return NULL;
  901. }
  902. unsigned avcodec_version( void )
  903. {
  904. return LIBAVCODEC_VERSION_INT;
  905. }
  906. const char *avcodec_configuration(void)
  907. {
  908. return FFMPEG_CONFIGURATION;
  909. }
  910. const char *avcodec_license(void)
  911. {
  912. #define LICENSE_PREFIX "libavcodec license: "
  913. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  914. }
  915. void avcodec_init(void)
  916. {
  917. static int initialized = 0;
  918. if (initialized != 0)
  919. return;
  920. initialized = 1;
  921. dsputil_static_init();
  922. }
  923. void avcodec_flush_buffers(AVCodecContext *avctx)
  924. {
  925. if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  926. ff_thread_flush(avctx);
  927. if(avctx->codec->flush)
  928. avctx->codec->flush(avctx);
  929. }
  930. void avcodec_default_free_buffers(AVCodecContext *s){
  931. int i, j;
  932. if(s->internal_buffer==NULL) return;
  933. if (s->internal_buffer_count)
  934. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
  935. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  936. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  937. for(j=0; j<4; j++){
  938. av_freep(&buf->base[j]);
  939. buf->data[j]= NULL;
  940. }
  941. }
  942. av_freep(&s->internal_buffer);
  943. s->internal_buffer_count=0;
  944. }
  945. char av_get_pict_type_char(int pict_type){
  946. switch(pict_type){
  947. case FF_I_TYPE: return 'I';
  948. case FF_P_TYPE: return 'P';
  949. case FF_B_TYPE: return 'B';
  950. case FF_S_TYPE: return 'S';
  951. case FF_SI_TYPE:return 'i';
  952. case FF_SP_TYPE:return 'p';
  953. case FF_BI_TYPE:return 'b';
  954. default: return '?';
  955. }
  956. }
  957. int av_get_bits_per_sample(enum CodecID codec_id){
  958. switch(codec_id){
  959. case CODEC_ID_ADPCM_SBPRO_2:
  960. return 2;
  961. case CODEC_ID_ADPCM_SBPRO_3:
  962. return 3;
  963. case CODEC_ID_ADPCM_SBPRO_4:
  964. case CODEC_ID_ADPCM_CT:
  965. case CODEC_ID_ADPCM_IMA_WAV:
  966. case CODEC_ID_ADPCM_MS:
  967. case CODEC_ID_ADPCM_YAMAHA:
  968. return 4;
  969. case CODEC_ID_PCM_ALAW:
  970. case CODEC_ID_PCM_MULAW:
  971. case CODEC_ID_PCM_S8:
  972. case CODEC_ID_PCM_U8:
  973. case CODEC_ID_PCM_ZORK:
  974. return 8;
  975. case CODEC_ID_PCM_S16BE:
  976. case CODEC_ID_PCM_S16LE:
  977. case CODEC_ID_PCM_S16LE_PLANAR:
  978. case CODEC_ID_PCM_U16BE:
  979. case CODEC_ID_PCM_U16LE:
  980. return 16;
  981. case CODEC_ID_PCM_S24DAUD:
  982. case CODEC_ID_PCM_S24BE:
  983. case CODEC_ID_PCM_S24LE:
  984. case CODEC_ID_PCM_U24BE:
  985. case CODEC_ID_PCM_U24LE:
  986. return 24;
  987. case CODEC_ID_PCM_S32BE:
  988. case CODEC_ID_PCM_S32LE:
  989. case CODEC_ID_PCM_U32BE:
  990. case CODEC_ID_PCM_U32LE:
  991. case CODEC_ID_PCM_F32BE:
  992. case CODEC_ID_PCM_F32LE:
  993. return 32;
  994. case CODEC_ID_PCM_F64BE:
  995. case CODEC_ID_PCM_F64LE:
  996. return 64;
  997. default:
  998. return 0;
  999. }
  1000. }
  1001. #if FF_API_OLD_SAMPLE_FMT
  1002. int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
  1003. return av_get_bits_per_sample_fmt(sample_fmt);
  1004. }
  1005. #endif
  1006. #if !HAVE_THREADS
  1007. int ff_thread_init(AVCodecContext *s, int thread_count){
  1008. s->thread_count = thread_count;
  1009. return -1;
  1010. }
  1011. #endif
  1012. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1013. {
  1014. unsigned int n = 0;
  1015. while(v >= 0xff) {
  1016. *s++ = 0xff;
  1017. v -= 0xff;
  1018. n++;
  1019. }
  1020. *s = v;
  1021. n++;
  1022. return n;
  1023. }
  1024. #if LIBAVCODEC_VERSION_MAJOR < 53
  1025. #include "libavutil/parseutils.h"
  1026. int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str)
  1027. {
  1028. return av_parse_video_size(width_ptr, height_ptr, str);
  1029. }
  1030. int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg)
  1031. {
  1032. return av_parse_video_rate(frame_rate, arg);
  1033. }
  1034. #endif
  1035. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1036. int i;
  1037. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1038. return i;
  1039. }
  1040. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1041. {
  1042. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  1043. "version to the newest one from Git. If the problem still "
  1044. "occurs, it means that your file has a feature which has not "
  1045. "been implemented.", feature);
  1046. if(want_sample)
  1047. av_log_ask_for_sample(avc, NULL);
  1048. else
  1049. av_log(avc, AV_LOG_WARNING, "\n");
  1050. }
  1051. void av_log_ask_for_sample(void *avc, const char *msg)
  1052. {
  1053. if (msg)
  1054. av_log(avc, AV_LOG_WARNING, "%s ", msg);
  1055. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1056. "of this file to ftp://upload.libav.org/MPlayer/incoming/ "
  1057. "and contact the libav-devel mailing list.\n");
  1058. }
  1059. static AVHWAccel *first_hwaccel = NULL;
  1060. void av_register_hwaccel(AVHWAccel *hwaccel)
  1061. {
  1062. AVHWAccel **p = &first_hwaccel;
  1063. while (*p)
  1064. p = &(*p)->next;
  1065. *p = hwaccel;
  1066. hwaccel->next = NULL;
  1067. }
  1068. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1069. {
  1070. return hwaccel ? hwaccel->next : first_hwaccel;
  1071. }
  1072. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1073. {
  1074. AVHWAccel *hwaccel=NULL;
  1075. while((hwaccel= av_hwaccel_next(hwaccel))){
  1076. if ( hwaccel->id == codec_id
  1077. && hwaccel->pix_fmt == pix_fmt)
  1078. return hwaccel;
  1079. }
  1080. return NULL;
  1081. }
  1082. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1083. {
  1084. if (ff_lockmgr_cb) {
  1085. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1086. return -1;
  1087. }
  1088. ff_lockmgr_cb = cb;
  1089. if (ff_lockmgr_cb) {
  1090. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1091. return -1;
  1092. }
  1093. return 0;
  1094. }
  1095. unsigned int ff_toupper4(unsigned int x)
  1096. {
  1097. return toupper( x &0xFF)
  1098. + (toupper((x>>8 )&0xFF)<<8 )
  1099. + (toupper((x>>16)&0xFF)<<16)
  1100. + (toupper((x>>24)&0xFF)<<24);
  1101. }
  1102. #if !HAVE_PTHREADS
  1103. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1104. {
  1105. f->owner = avctx;
  1106. return avctx->get_buffer(avctx, f);
  1107. }
  1108. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1109. {
  1110. f->owner->release_buffer(f->owner, f);
  1111. }
  1112. void ff_thread_finish_setup(AVCodecContext *avctx)
  1113. {
  1114. }
  1115. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1116. {
  1117. }
  1118. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1119. {
  1120. }
  1121. #endif
  1122. #if LIBAVCODEC_VERSION_MAJOR < 53
  1123. int avcodec_thread_init(AVCodecContext *s, int thread_count)
  1124. {
  1125. return ff_thread_init(s, thread_count);
  1126. }
  1127. void avcodec_thread_free(AVCodecContext *s)
  1128. {
  1129. #if HAVE_THREADS
  1130. ff_thread_free(s);
  1131. #endif
  1132. }
  1133. #endif