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.

1195 lines
35KB

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