utils.c 37 KB

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