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.

1263 lines
36KB

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