utils.c 43 KB

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