utils.c 38 KB

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