utils.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  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 libavcodec/utils.c
  24. * utils.
  25. */
  26. /* needed for mkstemp() */
  27. #define _XOPEN_SOURCE 600
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/integer.h"
  30. #include "libavutil/crc.h"
  31. #include "avcodec.h"
  32. #include "dsputil.h"
  33. #include "opt.h"
  34. #include "imgconvert.h"
  35. #include "audioconvert.h"
  36. #include "internal.h"
  37. #include <stdlib.h>
  38. #include <stdarg.h>
  39. #include <limits.h>
  40. #include <float.h>
  41. #if !HAVE_MKSTEMP
  42. #include <fcntl.h>
  43. #endif
  44. const uint8_t ff_reverse[256]={
  45. 0x00,0x80,0x40,0xC0,0x20,0xA0,0x60,0xE0,0x10,0x90,0x50,0xD0,0x30,0xB0,0x70,0xF0,
  46. 0x08,0x88,0x48,0xC8,0x28,0xA8,0x68,0xE8,0x18,0x98,0x58,0xD8,0x38,0xB8,0x78,0xF8,
  47. 0x04,0x84,0x44,0xC4,0x24,0xA4,0x64,0xE4,0x14,0x94,0x54,0xD4,0x34,0xB4,0x74,0xF4,
  48. 0x0C,0x8C,0x4C,0xCC,0x2C,0xAC,0x6C,0xEC,0x1C,0x9C,0x5C,0xDC,0x3C,0xBC,0x7C,0xFC,
  49. 0x02,0x82,0x42,0xC2,0x22,0xA2,0x62,0xE2,0x12,0x92,0x52,0xD2,0x32,0xB2,0x72,0xF2,
  50. 0x0A,0x8A,0x4A,0xCA,0x2A,0xAA,0x6A,0xEA,0x1A,0x9A,0x5A,0xDA,0x3A,0xBA,0x7A,0xFA,
  51. 0x06,0x86,0x46,0xC6,0x26,0xA6,0x66,0xE6,0x16,0x96,0x56,0xD6,0x36,0xB6,0x76,0xF6,
  52. 0x0E,0x8E,0x4E,0xCE,0x2E,0xAE,0x6E,0xEE,0x1E,0x9E,0x5E,0xDE,0x3E,0xBE,0x7E,0xFE,
  53. 0x01,0x81,0x41,0xC1,0x21,0xA1,0x61,0xE1,0x11,0x91,0x51,0xD1,0x31,0xB1,0x71,0xF1,
  54. 0x09,0x89,0x49,0xC9,0x29,0xA9,0x69,0xE9,0x19,0x99,0x59,0xD9,0x39,0xB9,0x79,0xF9,
  55. 0x05,0x85,0x45,0xC5,0x25,0xA5,0x65,0xE5,0x15,0x95,0x55,0xD5,0x35,0xB5,0x75,0xF5,
  56. 0x0D,0x8D,0x4D,0xCD,0x2D,0xAD,0x6D,0xED,0x1D,0x9D,0x5D,0xDD,0x3D,0xBD,0x7D,0xFD,
  57. 0x03,0x83,0x43,0xC3,0x23,0xA3,0x63,0xE3,0x13,0x93,0x53,0xD3,0x33,0xB3,0x73,0xF3,
  58. 0x0B,0x8B,0x4B,0xCB,0x2B,0xAB,0x6B,0xEB,0x1B,0x9B,0x5B,0xDB,0x3B,0xBB,0x7B,0xFB,
  59. 0x07,0x87,0x47,0xC7,0x27,0xA7,0x67,0xE7,0x17,0x97,0x57,0xD7,0x37,0xB7,0x77,0xF7,
  60. 0x0F,0x8F,0x4F,0xCF,0x2F,0xAF,0x6F,0xEF,0x1F,0x9F,0x5F,0xDF,0x3F,0xBF,0x7F,0xFF,
  61. };
  62. static int volatile entangled_thread_counter=0;
  63. int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  64. static void *codec_mutex;
  65. void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size)
  66. {
  67. if(min_size < *size)
  68. return ptr;
  69. *size= FFMAX(17*min_size/16 + 32, min_size);
  70. ptr= av_realloc(ptr, *size);
  71. 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
  72. *size= 0;
  73. return ptr;
  74. }
  75. /* encoder management */
  76. static AVCodec *first_avcodec = NULL;
  77. AVCodec *av_codec_next(AVCodec *c){
  78. if(c) return c->next;
  79. else return first_avcodec;
  80. }
  81. void avcodec_register(AVCodec *codec)
  82. {
  83. AVCodec **p;
  84. avcodec_init();
  85. p = &first_avcodec;
  86. while (*p != NULL) p = &(*p)->next;
  87. *p = codec;
  88. codec->next = NULL;
  89. }
  90. #if LIBAVCODEC_VERSION_MAJOR < 53
  91. void register_avcodec(AVCodec *codec)
  92. {
  93. avcodec_register(codec);
  94. }
  95. #endif
  96. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  97. s->coded_width = width;
  98. s->coded_height= height;
  99. s->width = -((-width )>>s->lowres);
  100. s->height= -((-height)>>s->lowres);
  101. }
  102. typedef struct InternalBuffer{
  103. int last_pic_num;
  104. uint8_t *base[4];
  105. uint8_t *data[4];
  106. int linesize[4];
  107. int width, height;
  108. enum PixelFormat pix_fmt;
  109. }InternalBuffer;
  110. #define INTERNAL_BUFFER_SIZE 32
  111. #define ALIGN(x, a) (((x)+(a)-1)&~((a)-1))
  112. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  113. int w_align= 1;
  114. int h_align= 1;
  115. switch(s->pix_fmt){
  116. case PIX_FMT_YUV420P:
  117. case PIX_FMT_YUYV422:
  118. case PIX_FMT_UYVY422:
  119. case PIX_FMT_YUV422P:
  120. case PIX_FMT_YUV444P:
  121. case PIX_FMT_GRAY8:
  122. case PIX_FMT_GRAY16BE:
  123. case PIX_FMT_GRAY16LE:
  124. case PIX_FMT_YUVJ420P:
  125. case PIX_FMT_YUVJ422P:
  126. case PIX_FMT_YUVJ444P:
  127. case PIX_FMT_YUVA420P:
  128. w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
  129. h_align= 16;
  130. break;
  131. case PIX_FMT_YUV411P:
  132. case PIX_FMT_UYYVYY411:
  133. w_align=32;
  134. h_align=8;
  135. break;
  136. case PIX_FMT_YUV410P:
  137. if(s->codec_id == CODEC_ID_SVQ1){
  138. w_align=64;
  139. h_align=64;
  140. }
  141. case PIX_FMT_RGB555:
  142. if(s->codec_id == CODEC_ID_RPZA){
  143. w_align=4;
  144. h_align=4;
  145. }
  146. case PIX_FMT_PAL8:
  147. case PIX_FMT_BGR8:
  148. case PIX_FMT_RGB8:
  149. if (s->codec_id == CODEC_ID_SMC ||
  150. s->codec_id == CODEC_ID_CINEPAK) {
  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. case PIX_FMT_RGB24:
  162. if (s->codec_id == CODEC_ID_CINEPAK) {
  163. w_align = 4;
  164. h_align = 4;
  165. }
  166. break;
  167. default:
  168. w_align= 1;
  169. h_align= 1;
  170. break;
  171. }
  172. *width = ALIGN(*width , w_align);
  173. *height= ALIGN(*height, h_align);
  174. if(s->codec_id == CODEC_ID_H264)
  175. *height+=2; // some of the optimized chroma MC reads one line too much
  176. }
  177. int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
  178. if((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
  179. return 0;
  180. av_log(av_log_ctx, AV_LOG_ERROR, "picture size invalid (%ux%u)\n", w, h);
  181. return -1;
  182. }
  183. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  184. int i;
  185. int w= s->width;
  186. int h= s->height;
  187. InternalBuffer *buf;
  188. int *picture_number;
  189. if(pic->data[0]!=NULL) {
  190. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  191. return -1;
  192. }
  193. if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
  194. av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
  195. return -1;
  196. }
  197. if(avcodec_check_dimensions(s,w,h))
  198. return -1;
  199. if(s->internal_buffer==NULL){
  200. s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
  201. }
  202. #if 0
  203. s->internal_buffer= av_fast_realloc(
  204. s->internal_buffer,
  205. &s->internal_buffer_size,
  206. sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
  207. );
  208. #endif
  209. buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  210. picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
  211. (*picture_number)++;
  212. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  213. for(i=0; i<4; i++){
  214. av_freep(&buf->base[i]);
  215. buf->data[i]= NULL;
  216. }
  217. }
  218. if(buf->base[0]){
  219. pic->age= *picture_number - buf->last_pic_num;
  220. buf->last_pic_num= *picture_number;
  221. }else{
  222. int h_chroma_shift, v_chroma_shift;
  223. int size[4] = {0};
  224. int tmpsize;
  225. AVPicture picture;
  226. int stride_align[4];
  227. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  228. avcodec_align_dimensions(s, &w, &h);
  229. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  230. w+= EDGE_WIDTH*2;
  231. h+= EDGE_WIDTH*2;
  232. }
  233. ff_fill_linesize(&picture, s->pix_fmt, w);
  234. for (i=0; i<4; i++){
  235. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  236. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  237. //picture size unneccessarily in some cases. The solution here is not
  238. //pretty and better ideas are welcome!
  239. #if HAVE_MMX
  240. if(s->codec_id == CODEC_ID_SVQ1)
  241. stride_align[i]= 16;
  242. else
  243. #endif
  244. stride_align[i] = STRIDE_ALIGN;
  245. picture.linesize[i] = ALIGN(picture.linesize[i], stride_align[i]);
  246. }
  247. tmpsize = ff_fill_pointer(&picture, NULL, s->pix_fmt, h);
  248. if (tmpsize < 0)
  249. return -1;
  250. for (i=0; i<3 && picture.data[i+1]; i++)
  251. size[i] = picture.data[i+1] - picture.data[i];
  252. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  253. buf->last_pic_num= -256*256*256*64;
  254. memset(buf->base, 0, sizeof(buf->base));
  255. memset(buf->data, 0, sizeof(buf->data));
  256. for(i=0; i<4 && size[i]; i++){
  257. const int h_shift= i==0 ? 0 : h_chroma_shift;
  258. const int v_shift= i==0 ? 0 : v_chroma_shift;
  259. buf->linesize[i]= picture.linesize[i];
  260. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  261. if(buf->base[i]==NULL) return -1;
  262. memset(buf->base[i], 128, size[i]);
  263. // no edge if EDEG EMU or not planar YUV
  264. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  265. buf->data[i] = buf->base[i];
  266. else
  267. buf->data[i] = buf->base[i] + ALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), stride_align[i]);
  268. }
  269. if(size[1] && !size[2])
  270. ff_set_systematic_pal((uint32_t*)buf->data[1], s->pix_fmt);
  271. buf->width = s->width;
  272. buf->height = s->height;
  273. buf->pix_fmt= s->pix_fmt;
  274. pic->age= 256*256*256*64;
  275. }
  276. pic->type= FF_BUFFER_TYPE_INTERNAL;
  277. for(i=0; i<4; i++){
  278. pic->base[i]= buf->base[i];
  279. pic->data[i]= buf->data[i];
  280. pic->linesize[i]= buf->linesize[i];
  281. }
  282. s->internal_buffer_count++;
  283. pic->reordered_opaque= s->reordered_opaque;
  284. if(s->debug&FF_DEBUG_BUFFERS)
  285. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  286. return 0;
  287. }
  288. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  289. int i;
  290. InternalBuffer *buf, *last;
  291. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  292. assert(s->internal_buffer_count);
  293. buf = NULL; /* avoids warning */
  294. for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  295. buf= &((InternalBuffer*)s->internal_buffer)[i];
  296. if(buf->data[0] == pic->data[0])
  297. break;
  298. }
  299. assert(i < s->internal_buffer_count);
  300. s->internal_buffer_count--;
  301. last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  302. FFSWAP(InternalBuffer, *buf, *last);
  303. for(i=0; i<4; i++){
  304. pic->data[i]=NULL;
  305. // pic->base[i]=NULL;
  306. }
  307. //printf("R%X\n", pic->opaque);
  308. if(s->debug&FF_DEBUG_BUFFERS)
  309. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  310. }
  311. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  312. AVFrame temp_pic;
  313. int i;
  314. /* If no picture return a new buffer */
  315. if(pic->data[0] == NULL) {
  316. /* We will copy from buffer, so must be readable */
  317. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  318. return s->get_buffer(s, pic);
  319. }
  320. /* If internal buffer type return the same buffer */
  321. if(pic->type == FF_BUFFER_TYPE_INTERNAL)
  322. return 0;
  323. /*
  324. * Not internal type and reget_buffer not overridden, emulate cr buffer
  325. */
  326. temp_pic = *pic;
  327. for(i = 0; i < 4; i++)
  328. pic->data[i] = pic->base[i] = NULL;
  329. pic->opaque = NULL;
  330. /* Allocate new frame */
  331. if (s->get_buffer(s, pic))
  332. return -1;
  333. /* Copy image data from old buffer to new buffer */
  334. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  335. s->height);
  336. s->release_buffer(s, &temp_pic); // Release old frame
  337. return 0;
  338. }
  339. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  340. int i;
  341. for(i=0; i<count; i++){
  342. int r= func(c, (char*)arg + i*size);
  343. if(ret) ret[i]= r;
  344. }
  345. return 0;
  346. }
  347. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  348. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  349. ++fmt;
  350. return fmt[0];
  351. }
  352. void avcodec_get_frame_defaults(AVFrame *pic){
  353. memset(pic, 0, sizeof(AVFrame));
  354. pic->pts= AV_NOPTS_VALUE;
  355. pic->key_frame= 1;
  356. }
  357. AVFrame *avcodec_alloc_frame(void){
  358. AVFrame *pic= av_malloc(sizeof(AVFrame));
  359. if(pic==NULL) return NULL;
  360. avcodec_get_frame_defaults(pic);
  361. return pic;
  362. }
  363. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  364. {
  365. int ret= -1;
  366. /* If there is a user-supplied mutex locking routine, call it. */
  367. if (ff_lockmgr_cb) {
  368. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  369. return -1;
  370. }
  371. entangled_thread_counter++;
  372. if(entangled_thread_counter != 1){
  373. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  374. goto end;
  375. }
  376. if(avctx->codec || !codec)
  377. goto end;
  378. if (codec->priv_data_size > 0) {
  379. avctx->priv_data = av_mallocz(codec->priv_data_size);
  380. if (!avctx->priv_data) {
  381. ret = AVERROR(ENOMEM);
  382. goto end;
  383. }
  384. } else {
  385. avctx->priv_data = NULL;
  386. }
  387. if(avctx->coded_width && avctx->coded_height)
  388. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  389. else if(avctx->width && avctx->height)
  390. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  391. if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height)){
  392. av_freep(&avctx->priv_data);
  393. ret = AVERROR(EINVAL);
  394. goto end;
  395. }
  396. avctx->codec = codec;
  397. avctx->codec_id = codec->id;
  398. avctx->frame_number = 0;
  399. if(avctx->codec->init){
  400. ret = avctx->codec->init(avctx);
  401. if (ret < 0) {
  402. av_freep(&avctx->priv_data);
  403. avctx->codec= NULL;
  404. goto end;
  405. }
  406. }
  407. ret=0;
  408. end:
  409. entangled_thread_counter--;
  410. /* Release any user-supplied mutex. */
  411. if (ff_lockmgr_cb) {
  412. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  413. }
  414. return ret;
  415. }
  416. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  417. const short *samples)
  418. {
  419. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  420. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  421. return -1;
  422. }
  423. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  424. int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
  425. avctx->frame_number++;
  426. return ret;
  427. }else
  428. return 0;
  429. }
  430. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  431. const AVFrame *pict)
  432. {
  433. if(buf_size < FF_MIN_BUFFER_SIZE){
  434. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  435. return -1;
  436. }
  437. if(avcodec_check_dimensions(avctx,avctx->width,avctx->height))
  438. return -1;
  439. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  440. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  441. avctx->frame_number++;
  442. emms_c(); //needed to avoid an emms_c() call before every return;
  443. return ret;
  444. }else
  445. return 0;
  446. }
  447. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  448. const AVSubtitle *sub)
  449. {
  450. int ret;
  451. if(sub->start_display_time) {
  452. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  453. return -1;
  454. }
  455. if(sub->num_rects == 0 || !sub->rects)
  456. return -1;
  457. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  458. avctx->frame_number++;
  459. return ret;
  460. }
  461. int attribute_align_arg avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  462. int *got_picture_ptr,
  463. const uint8_t *buf, int buf_size)
  464. {
  465. int ret;
  466. *got_picture_ptr= 0;
  467. if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height))
  468. return -1;
  469. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || buf_size){
  470. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  471. buf, buf_size);
  472. emms_c(); //needed to avoid an emms_c() call before every return;
  473. if (*got_picture_ptr)
  474. avctx->frame_number++;
  475. }else
  476. ret= 0;
  477. return ret;
  478. }
  479. int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,
  480. int *frame_size_ptr,
  481. const uint8_t *buf, int buf_size)
  482. {
  483. int ret;
  484. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || buf_size){
  485. //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
  486. if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
  487. av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
  488. return -1;
  489. }
  490. if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
  491. *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
  492. av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
  493. return -1;
  494. }
  495. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  496. buf, buf_size);
  497. avctx->frame_number++;
  498. }else{
  499. ret= 0;
  500. *frame_size_ptr=0;
  501. }
  502. return ret;
  503. }
  504. int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
  505. int *got_sub_ptr,
  506. const uint8_t *buf, int buf_size)
  507. {
  508. int ret;
  509. *got_sub_ptr = 0;
  510. ret = avctx->codec->decode(avctx, sub, got_sub_ptr,
  511. buf, buf_size);
  512. if (*got_sub_ptr)
  513. avctx->frame_number++;
  514. return ret;
  515. }
  516. int avcodec_close(AVCodecContext *avctx)
  517. {
  518. /* If there is a user-supplied mutex locking routine, call it. */
  519. if (ff_lockmgr_cb) {
  520. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  521. return -1;
  522. }
  523. entangled_thread_counter++;
  524. if(entangled_thread_counter != 1){
  525. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  526. entangled_thread_counter--;
  527. return -1;
  528. }
  529. if (HAVE_THREADS && avctx->thread_opaque)
  530. avcodec_thread_free(avctx);
  531. if (avctx->codec->close)
  532. avctx->codec->close(avctx);
  533. avcodec_default_free_buffers(avctx);
  534. av_freep(&avctx->priv_data);
  535. avctx->codec = NULL;
  536. entangled_thread_counter--;
  537. /* Release any user-supplied mutex. */
  538. if (ff_lockmgr_cb) {
  539. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  540. }
  541. return 0;
  542. }
  543. AVCodec *avcodec_find_encoder(enum CodecID id)
  544. {
  545. AVCodec *p;
  546. p = first_avcodec;
  547. while (p) {
  548. if (p->encode != NULL && p->id == id)
  549. return p;
  550. p = p->next;
  551. }
  552. return NULL;
  553. }
  554. AVCodec *avcodec_find_encoder_by_name(const char *name)
  555. {
  556. AVCodec *p;
  557. if (!name)
  558. return NULL;
  559. p = first_avcodec;
  560. while (p) {
  561. if (p->encode != NULL && strcmp(name,p->name) == 0)
  562. return p;
  563. p = p->next;
  564. }
  565. return NULL;
  566. }
  567. AVCodec *avcodec_find_decoder(enum CodecID id)
  568. {
  569. AVCodec *p;
  570. p = first_avcodec;
  571. while (p) {
  572. if (p->decode != NULL && p->id == id)
  573. return p;
  574. p = p->next;
  575. }
  576. return NULL;
  577. }
  578. AVCodec *avcodec_find_decoder_by_name(const char *name)
  579. {
  580. AVCodec *p;
  581. if (!name)
  582. return NULL;
  583. p = first_avcodec;
  584. while (p) {
  585. if (p->decode != NULL && strcmp(name,p->name) == 0)
  586. return p;
  587. p = p->next;
  588. }
  589. return NULL;
  590. }
  591. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  592. {
  593. const char *codec_name;
  594. AVCodec *p;
  595. char buf1[32];
  596. int bitrate;
  597. AVRational display_aspect_ratio;
  598. if (encode)
  599. p = avcodec_find_encoder(enc->codec_id);
  600. else
  601. p = avcodec_find_decoder(enc->codec_id);
  602. if (p) {
  603. codec_name = p->name;
  604. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  605. /* fake mpeg2 transport stream codec (currently not
  606. registered) */
  607. codec_name = "mpeg2ts";
  608. } else if (enc->codec_name[0] != '\0') {
  609. codec_name = enc->codec_name;
  610. } else {
  611. /* output avi tags */
  612. if( isprint(enc->codec_tag&0xFF) && isprint((enc->codec_tag>>8)&0xFF)
  613. && isprint((enc->codec_tag>>16)&0xFF) && isprint((enc->codec_tag>>24)&0xFF)){
  614. snprintf(buf1, sizeof(buf1), "%c%c%c%c / 0x%04X",
  615. enc->codec_tag & 0xff,
  616. (enc->codec_tag >> 8) & 0xff,
  617. (enc->codec_tag >> 16) & 0xff,
  618. (enc->codec_tag >> 24) & 0xff,
  619. enc->codec_tag);
  620. } else {
  621. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  622. }
  623. codec_name = buf1;
  624. }
  625. switch(enc->codec_type) {
  626. case CODEC_TYPE_VIDEO:
  627. snprintf(buf, buf_size,
  628. "Video: %s%s",
  629. codec_name, enc->mb_decision ? " (hq)" : "");
  630. if (enc->pix_fmt != PIX_FMT_NONE) {
  631. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  632. ", %s",
  633. avcodec_get_pix_fmt_name(enc->pix_fmt));
  634. }
  635. if (enc->width) {
  636. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  637. ", %dx%d",
  638. enc->width, enc->height);
  639. if (enc->sample_aspect_ratio.num) {
  640. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  641. enc->width*enc->sample_aspect_ratio.num,
  642. enc->height*enc->sample_aspect_ratio.den,
  643. 1024*1024);
  644. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  645. " [PAR %d:%d DAR %d:%d]",
  646. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  647. display_aspect_ratio.num, display_aspect_ratio.den);
  648. }
  649. if(av_log_get_level() >= AV_LOG_DEBUG){
  650. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  651. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  652. ", %d/%d",
  653. enc->time_base.num/g, enc->time_base.den/g);
  654. }
  655. }
  656. if (encode) {
  657. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  658. ", q=%d-%d", enc->qmin, enc->qmax);
  659. }
  660. bitrate = enc->bit_rate;
  661. break;
  662. case CODEC_TYPE_AUDIO:
  663. snprintf(buf, buf_size,
  664. "Audio: %s",
  665. codec_name);
  666. if (enc->sample_rate) {
  667. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  668. ", %d Hz", enc->sample_rate);
  669. }
  670. av_strlcat(buf, ", ", buf_size);
  671. avcodec_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  672. if (enc->sample_fmt != SAMPLE_FMT_NONE) {
  673. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  674. ", %s", avcodec_get_sample_fmt_name(enc->sample_fmt));
  675. }
  676. /* for PCM codecs, compute bitrate directly */
  677. switch(enc->codec_id) {
  678. case CODEC_ID_PCM_F64BE:
  679. case CODEC_ID_PCM_F64LE:
  680. bitrate = enc->sample_rate * enc->channels * 64;
  681. break;
  682. case CODEC_ID_PCM_S32LE:
  683. case CODEC_ID_PCM_S32BE:
  684. case CODEC_ID_PCM_U32LE:
  685. case CODEC_ID_PCM_U32BE:
  686. case CODEC_ID_PCM_F32BE:
  687. case CODEC_ID_PCM_F32LE:
  688. bitrate = enc->sample_rate * enc->channels * 32;
  689. break;
  690. case CODEC_ID_PCM_S24LE:
  691. case CODEC_ID_PCM_S24BE:
  692. case CODEC_ID_PCM_U24LE:
  693. case CODEC_ID_PCM_U24BE:
  694. case CODEC_ID_PCM_S24DAUD:
  695. bitrate = enc->sample_rate * enc->channels * 24;
  696. break;
  697. case CODEC_ID_PCM_S16LE:
  698. case CODEC_ID_PCM_S16BE:
  699. case CODEC_ID_PCM_S16LE_PLANAR:
  700. case CODEC_ID_PCM_U16LE:
  701. case CODEC_ID_PCM_U16BE:
  702. bitrate = enc->sample_rate * enc->channels * 16;
  703. break;
  704. case CODEC_ID_PCM_S8:
  705. case CODEC_ID_PCM_U8:
  706. case CODEC_ID_PCM_ALAW:
  707. case CODEC_ID_PCM_MULAW:
  708. case CODEC_ID_PCM_ZORK:
  709. bitrate = enc->sample_rate * enc->channels * 8;
  710. break;
  711. default:
  712. bitrate = enc->bit_rate;
  713. break;
  714. }
  715. break;
  716. case CODEC_TYPE_DATA:
  717. snprintf(buf, buf_size, "Data: %s", codec_name);
  718. bitrate = enc->bit_rate;
  719. break;
  720. case CODEC_TYPE_SUBTITLE:
  721. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  722. bitrate = enc->bit_rate;
  723. break;
  724. case CODEC_TYPE_ATTACHMENT:
  725. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  726. bitrate = enc->bit_rate;
  727. break;
  728. default:
  729. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  730. return;
  731. }
  732. if (encode) {
  733. if (enc->flags & CODEC_FLAG_PASS1)
  734. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  735. ", pass 1");
  736. if (enc->flags & CODEC_FLAG_PASS2)
  737. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  738. ", pass 2");
  739. }
  740. if (bitrate != 0) {
  741. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  742. ", %d kb/s", bitrate / 1000);
  743. }
  744. }
  745. unsigned avcodec_version( void )
  746. {
  747. return LIBAVCODEC_VERSION_INT;
  748. }
  749. void avcodec_init(void)
  750. {
  751. static int initialized = 0;
  752. if (initialized != 0)
  753. return;
  754. initialized = 1;
  755. dsputil_static_init();
  756. }
  757. void avcodec_flush_buffers(AVCodecContext *avctx)
  758. {
  759. if(avctx->codec->flush)
  760. avctx->codec->flush(avctx);
  761. }
  762. void avcodec_default_free_buffers(AVCodecContext *s){
  763. int i, j;
  764. if(s->internal_buffer==NULL) return;
  765. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  766. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  767. for(j=0; j<4; j++){
  768. av_freep(&buf->base[j]);
  769. buf->data[j]= NULL;
  770. }
  771. }
  772. av_freep(&s->internal_buffer);
  773. s->internal_buffer_count=0;
  774. }
  775. char av_get_pict_type_char(int pict_type){
  776. switch(pict_type){
  777. case FF_I_TYPE: return 'I';
  778. case FF_P_TYPE: return 'P';
  779. case FF_B_TYPE: return 'B';
  780. case FF_S_TYPE: return 'S';
  781. case FF_SI_TYPE:return 'i';
  782. case FF_SP_TYPE:return 'p';
  783. case FF_BI_TYPE:return 'b';
  784. default: return '?';
  785. }
  786. }
  787. int av_get_bits_per_sample(enum CodecID codec_id){
  788. switch(codec_id){
  789. case CODEC_ID_ADPCM_SBPRO_2:
  790. return 2;
  791. case CODEC_ID_ADPCM_SBPRO_3:
  792. return 3;
  793. case CODEC_ID_ADPCM_SBPRO_4:
  794. case CODEC_ID_ADPCM_CT:
  795. return 4;
  796. case CODEC_ID_PCM_ALAW:
  797. case CODEC_ID_PCM_MULAW:
  798. case CODEC_ID_PCM_S8:
  799. case CODEC_ID_PCM_U8:
  800. case CODEC_ID_PCM_ZORK:
  801. return 8;
  802. case CODEC_ID_PCM_S16BE:
  803. case CODEC_ID_PCM_S16LE:
  804. case CODEC_ID_PCM_S16LE_PLANAR:
  805. case CODEC_ID_PCM_U16BE:
  806. case CODEC_ID_PCM_U16LE:
  807. return 16;
  808. case CODEC_ID_PCM_S24DAUD:
  809. case CODEC_ID_PCM_S24BE:
  810. case CODEC_ID_PCM_S24LE:
  811. case CODEC_ID_PCM_U24BE:
  812. case CODEC_ID_PCM_U24LE:
  813. return 24;
  814. case CODEC_ID_PCM_S32BE:
  815. case CODEC_ID_PCM_S32LE:
  816. case CODEC_ID_PCM_U32BE:
  817. case CODEC_ID_PCM_U32LE:
  818. case CODEC_ID_PCM_F32BE:
  819. case CODEC_ID_PCM_F32LE:
  820. return 32;
  821. case CODEC_ID_PCM_F64BE:
  822. case CODEC_ID_PCM_F64LE:
  823. return 64;
  824. default:
  825. return 0;
  826. }
  827. }
  828. int av_get_bits_per_sample_format(enum SampleFormat sample_fmt) {
  829. switch (sample_fmt) {
  830. case SAMPLE_FMT_U8:
  831. return 8;
  832. case SAMPLE_FMT_S16:
  833. return 16;
  834. case SAMPLE_FMT_S32:
  835. case SAMPLE_FMT_FLT:
  836. return 32;
  837. case SAMPLE_FMT_DBL:
  838. return 64;
  839. default:
  840. return 0;
  841. }
  842. }
  843. #if !HAVE_THREADS
  844. int avcodec_thread_init(AVCodecContext *s, int thread_count){
  845. return -1;
  846. }
  847. #endif
  848. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  849. {
  850. unsigned int n = 0;
  851. while(v >= 0xff) {
  852. *s++ = 0xff;
  853. v -= 0xff;
  854. n++;
  855. }
  856. *s = v;
  857. n++;
  858. return n;
  859. }
  860. /* Wrapper to work around the lack of mkstemp() on mingw/cygin.
  861. * Also, tries to create file in /tmp first, if possible.
  862. * *prefix can be a character constant; *filename will be allocated internally.
  863. * Returns file descriptor of opened file (or -1 on error)
  864. * and opened file name in **filename. */
  865. int av_tempfile(char *prefix, char **filename) {
  866. int fd=-1;
  867. #if !HAVE_MKSTEMP
  868. *filename = tempnam(".", prefix);
  869. #else
  870. size_t len = strlen(prefix) + 12; /* room for "/tmp/" and "XXXXXX\0" */
  871. *filename = av_malloc(len);
  872. #endif
  873. /* -----common section-----*/
  874. if (*filename == NULL) {
  875. av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot allocate file name\n");
  876. return -1;
  877. }
  878. #if !HAVE_MKSTEMP
  879. fd = open(*filename, O_RDWR | O_BINARY | O_CREAT, 0444);
  880. #else
  881. snprintf(*filename, len, "/tmp/%sXXXXXX", prefix);
  882. fd = mkstemp(*filename);
  883. if (fd < 0) {
  884. snprintf(*filename, len, "./%sXXXXXX", prefix);
  885. fd = mkstemp(*filename);
  886. }
  887. #endif
  888. /* -----common section-----*/
  889. if (fd < 0) {
  890. av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot open temporary file %s\n", *filename);
  891. return -1;
  892. }
  893. return fd; /* success */
  894. }
  895. typedef struct {
  896. const char *abbr;
  897. int width, height;
  898. } VideoFrameSizeAbbr;
  899. typedef struct {
  900. const char *abbr;
  901. int rate_num, rate_den;
  902. } VideoFrameRateAbbr;
  903. static const VideoFrameSizeAbbr video_frame_size_abbrs[] = {
  904. { "ntsc", 720, 480 },
  905. { "pal", 720, 576 },
  906. { "qntsc", 352, 240 }, /* VCD compliant NTSC */
  907. { "qpal", 352, 288 }, /* VCD compliant PAL */
  908. { "sntsc", 640, 480 }, /* square pixel NTSC */
  909. { "spal", 768, 576 }, /* square pixel PAL */
  910. { "film", 352, 240 },
  911. { "ntsc-film", 352, 240 },
  912. { "sqcif", 128, 96 },
  913. { "qcif", 176, 144 },
  914. { "cif", 352, 288 },
  915. { "4cif", 704, 576 },
  916. { "qqvga", 160, 120 },
  917. { "qvga", 320, 240 },
  918. { "vga", 640, 480 },
  919. { "svga", 800, 600 },
  920. { "xga", 1024, 768 },
  921. { "uxga", 1600,1200 },
  922. { "qxga", 2048,1536 },
  923. { "sxga", 1280,1024 },
  924. { "qsxga", 2560,2048 },
  925. { "hsxga", 5120,4096 },
  926. { "wvga", 852, 480 },
  927. { "wxga", 1366, 768 },
  928. { "wsxga", 1600,1024 },
  929. { "wuxga", 1920,1200 },
  930. { "woxga", 2560,1600 },
  931. { "wqsxga", 3200,2048 },
  932. { "wquxga", 3840,2400 },
  933. { "whsxga", 6400,4096 },
  934. { "whuxga", 7680,4800 },
  935. { "cga", 320, 200 },
  936. { "ega", 640, 350 },
  937. { "hd480", 852, 480 },
  938. { "hd720", 1280, 720 },
  939. { "hd1080", 1920,1080 },
  940. };
  941. static const VideoFrameRateAbbr video_frame_rate_abbrs[]= {
  942. { "ntsc", 30000, 1001 },
  943. { "pal", 25, 1 },
  944. { "qntsc", 30000, 1001 }, /* VCD compliant NTSC */
  945. { "qpal", 25, 1 }, /* VCD compliant PAL */
  946. { "sntsc", 30000, 1001 }, /* square pixel NTSC */
  947. { "spal", 25, 1 }, /* square pixel PAL */
  948. { "film", 24, 1 },
  949. { "ntsc-film", 24000, 1001 },
  950. };
  951. int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str)
  952. {
  953. int i;
  954. int n = FF_ARRAY_ELEMS(video_frame_size_abbrs);
  955. char *p;
  956. int frame_width = 0, frame_height = 0;
  957. for(i=0;i<n;i++) {
  958. if (!strcmp(video_frame_size_abbrs[i].abbr, str)) {
  959. frame_width = video_frame_size_abbrs[i].width;
  960. frame_height = video_frame_size_abbrs[i].height;
  961. break;
  962. }
  963. }
  964. if (i == n) {
  965. p = str;
  966. frame_width = strtol(p, &p, 10);
  967. if (*p)
  968. p++;
  969. frame_height = strtol(p, &p, 10);
  970. }
  971. if (frame_width <= 0 || frame_height <= 0)
  972. return -1;
  973. *width_ptr = frame_width;
  974. *height_ptr = frame_height;
  975. return 0;
  976. }
  977. int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg)
  978. {
  979. int i;
  980. int n = FF_ARRAY_ELEMS(video_frame_rate_abbrs);
  981. char* cp;
  982. /* First, we check our abbreviation table */
  983. for (i = 0; i < n; ++i)
  984. if (!strcmp(video_frame_rate_abbrs[i].abbr, arg)) {
  985. frame_rate->num = video_frame_rate_abbrs[i].rate_num;
  986. frame_rate->den = video_frame_rate_abbrs[i].rate_den;
  987. return 0;
  988. }
  989. /* Then, we try to parse it as fraction */
  990. cp = strchr(arg, '/');
  991. if (!cp)
  992. cp = strchr(arg, ':');
  993. if (cp) {
  994. char* cpp;
  995. frame_rate->num = strtol(arg, &cpp, 10);
  996. if (cpp != arg || cpp == cp)
  997. frame_rate->den = strtol(cp+1, &cpp, 10);
  998. else
  999. frame_rate->num = 0;
  1000. }
  1001. else {
  1002. /* Finally we give up and parse it as double */
  1003. AVRational time_base = av_d2q(strtod(arg, 0), 1001000);
  1004. frame_rate->den = time_base.den;
  1005. frame_rate->num = time_base.num;
  1006. }
  1007. if (!frame_rate->num || !frame_rate->den)
  1008. return -1;
  1009. else
  1010. return 0;
  1011. }
  1012. void ff_log_missing_feature(void *avc, const char *feature, int want_sample)
  1013. {
  1014. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  1015. "version to the newest one from SVN. If the problem still "
  1016. "occurs, it means that your file has a feature which has not "
  1017. "been implemented.", feature);
  1018. if(want_sample)
  1019. ff_log_ask_for_sample(avc, NULL);
  1020. else
  1021. av_log(avc, AV_LOG_WARNING, "\n");
  1022. }
  1023. void ff_log_ask_for_sample(void *avc, const char *msg)
  1024. {
  1025. if (msg)
  1026. av_log(avc, AV_LOG_WARNING, "%s ", msg);
  1027. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1028. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  1029. "and contact the ffmpeg-devel mailing list.\n");
  1030. }
  1031. static AVHWAccel *first_hwaccel = NULL;
  1032. void av_register_hwaccel(AVHWAccel *hwaccel)
  1033. {
  1034. AVHWAccel **p = &first_hwaccel;
  1035. while (*p)
  1036. p = &(*p)->next;
  1037. *p = hwaccel;
  1038. hwaccel->next = NULL;
  1039. }
  1040. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1041. {
  1042. return hwaccel ? hwaccel->next : first_hwaccel;
  1043. }
  1044. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1045. {
  1046. AVHWAccel *hwaccel=NULL;
  1047. while((hwaccel= av_hwaccel_next(hwaccel))){
  1048. if ( hwaccel->id == codec_id
  1049. && hwaccel->pix_fmt == pix_fmt)
  1050. return hwaccel;
  1051. }
  1052. return NULL;
  1053. }
  1054. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1055. {
  1056. if (ff_lockmgr_cb) {
  1057. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1058. return -1;
  1059. }
  1060. ff_lockmgr_cb = cb;
  1061. if (ff_lockmgr_cb) {
  1062. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1063. return -1;
  1064. }
  1065. return 0;
  1066. }