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.

1163 lines
34KB

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