You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1282 lines
38KB

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