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.

712 lines
18KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avcodec.h"
  20. #include "dsputil.h"
  21. #include "mpegvideo.h"
  22. void *av_mallocz(unsigned int size)
  23. {
  24. void *ptr;
  25. if(size == 0) fprintf(stderr, "Warning, allocating 0 bytes\n");
  26. ptr = av_malloc(size);
  27. if (!ptr)
  28. return NULL;
  29. memset(ptr, 0, size);
  30. return ptr;
  31. }
  32. /* allocation of static arrays - do not use for normal allocation */
  33. static unsigned int last_static = 0;
  34. static char*** array_static = NULL;
  35. static const unsigned int grow_static = 64; // ^2
  36. void *__av_mallocz_static(void** location, unsigned int size)
  37. {
  38. int l = (last_static + grow_static) & ~(grow_static - 1);
  39. void *ptr = av_mallocz(size);
  40. if (!ptr)
  41. return NULL;
  42. if (location)
  43. {
  44. if (l > last_static)
  45. array_static = realloc(array_static, l);
  46. array_static[last_static++] = (char**) location;
  47. *location = ptr;
  48. }
  49. return ptr;
  50. }
  51. /* free all static arrays and reset pointers to 0 */
  52. void av_free_static()
  53. {
  54. if (array_static)
  55. {
  56. unsigned i;
  57. for (i = 0; i < last_static; i++)
  58. {
  59. free(*array_static[i]);
  60. *array_static[i] = NULL;
  61. }
  62. free(array_static);
  63. array_static = 0;
  64. }
  65. last_static = 0;
  66. }
  67. /* cannot call it directly because of 'void **' casting is not automatic */
  68. void __av_freep(void **ptr)
  69. {
  70. av_free(*ptr);
  71. *ptr = NULL;
  72. }
  73. /* encoder management */
  74. AVCodec *first_avcodec;
  75. void register_avcodec(AVCodec *format)
  76. {
  77. AVCodec **p;
  78. p = &first_avcodec;
  79. while (*p != NULL) p = &(*p)->next;
  80. *p = format;
  81. format->next = NULL;
  82. }
  83. void avcodec_get_chroma_sub_sample(int fmt, int *h_shift, int *v_shift){
  84. switch(fmt){
  85. case PIX_FMT_YUV410P:
  86. *h_shift=2;
  87. *v_shift=2;
  88. break;
  89. case PIX_FMT_YUV420P:
  90. *h_shift=1;
  91. *v_shift=1;
  92. break;
  93. case PIX_FMT_YUV411P:
  94. *h_shift=2;
  95. *v_shift=0;
  96. break;
  97. case PIX_FMT_YUV422P:
  98. case PIX_FMT_YUV422:
  99. *h_shift=1;
  100. *v_shift=0;
  101. break;
  102. default: //RGB/...
  103. *h_shift=0;
  104. *v_shift=0;
  105. break;
  106. }
  107. }
  108. typedef struct DefaultPicOpaque{
  109. int last_pic_num;
  110. uint8_t *data[4];
  111. }DefaultPicOpaque;
  112. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  113. int i;
  114. const int width = s->width;
  115. const int height= s->height;
  116. DefaultPicOpaque *opaque;
  117. assert(pic->data[0]==NULL);
  118. assert(pic->type==0 || pic->type==FF_TYPE_INTERNAL);
  119. if(pic->opaque){
  120. opaque= (DefaultPicOpaque *)pic->opaque;
  121. for(i=0; i<3; i++)
  122. pic->data[i]= opaque->data[i];
  123. // printf("get_buffer %X coded_pic_num:%d last:%d\n", pic->opaque, pic->coded_picture_number, opaque->last_pic_num);
  124. pic->age= pic->coded_picture_number - opaque->last_pic_num;
  125. opaque->last_pic_num= pic->coded_picture_number;
  126. //printf("age: %d %d %d\n", pic->age, c->picture_number, pic->coded_picture_number);
  127. }else{
  128. int align, h_chroma_shift, v_chroma_shift;
  129. int w, h, pixel_size;
  130. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  131. switch(s->pix_fmt){
  132. case PIX_FMT_YUV422:
  133. pixel_size=2;
  134. break;
  135. case PIX_FMT_RGB24:
  136. case PIX_FMT_BGR24:
  137. pixel_size=3;
  138. break;
  139. case PIX_FMT_BGRA32:
  140. case PIX_FMT_RGBA32:
  141. pixel_size=4;
  142. break;
  143. default:
  144. pixel_size=1;
  145. }
  146. if(s->codec_id==CODEC_ID_SVQ1) align=63;
  147. else align=15;
  148. w= (width +align)&~align;
  149. h= (height+align)&~align;
  150. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  151. w+= EDGE_WIDTH*2;
  152. h+= EDGE_WIDTH*2;
  153. }
  154. opaque= av_mallocz(sizeof(DefaultPicOpaque));
  155. if(opaque==NULL) return -1;
  156. pic->opaque= opaque;
  157. opaque->last_pic_num= -256*256*256*64;
  158. for(i=0; i<3; i++){
  159. int h_shift= i==0 ? 0 : h_chroma_shift;
  160. int v_shift= i==0 ? 0 : v_chroma_shift;
  161. pic->linesize[i]= pixel_size*w>>h_shift;
  162. pic->base[i]= av_mallocz((pic->linesize[i]*h>>v_shift)+16); //FIXME 16
  163. if(pic->base[i]==NULL) return -1;
  164. memset(pic->base[i], 128, pic->linesize[i]*h>>v_shift);
  165. if(s->flags&CODEC_FLAG_EMU_EDGE)
  166. pic->data[i] = pic->base[i] + 16; //FIXME 16
  167. else
  168. pic->data[i] = pic->base[i] + (pic->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift) + 16; //FIXME 16
  169. opaque->data[i]= pic->data[i];
  170. }
  171. pic->age= 256*256*256*64;
  172. pic->type= FF_BUFFER_TYPE_INTERNAL;
  173. }
  174. return 0;
  175. }
  176. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  177. int i;
  178. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  179. for(i=0; i<3; i++)
  180. pic->data[i]=NULL;
  181. //printf("R%X\n", pic->opaque);
  182. }
  183. void avcodec_get_context_defaults(AVCodecContext *s){
  184. s->bit_rate= 800*1000;
  185. s->bit_rate_tolerance= s->bit_rate*10;
  186. s->qmin= 2;
  187. s->qmax= 31;
  188. s->rc_eq= "tex^qComp";
  189. s->qcompress= 0.5;
  190. s->max_qdiff= 3;
  191. s->b_quant_factor=1.25;
  192. s->b_quant_offset=1.25;
  193. s->i_quant_factor=-0.8;
  194. s->i_quant_offset=0.0;
  195. s->error_concealment= 3;
  196. s->error_resilience= 1;
  197. s->workaround_bugs= FF_BUG_AUTODETECT;
  198. s->frame_rate = 25 * FRAME_RATE_BASE;
  199. s->gop_size= 50;
  200. s->me_method= ME_EPZS;
  201. s->get_buffer= avcodec_default_get_buffer;
  202. s->release_buffer= avcodec_default_release_buffer;
  203. }
  204. /**
  205. * allocates a AVCodecContext and set it to defaults.
  206. * this can be deallocated by simply calling free()
  207. */
  208. AVCodecContext *avcodec_alloc_context(void){
  209. AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext));
  210. if(avctx==NULL) return NULL;
  211. avcodec_get_context_defaults(avctx);
  212. return avctx;
  213. }
  214. /**
  215. * allocates a AVPFrame and set it to defaults.
  216. * this can be deallocated by simply calling free()
  217. */
  218. AVFrame *avcodec_alloc_frame(void){
  219. AVFrame *pic= av_mallocz(sizeof(AVFrame));
  220. return pic;
  221. }
  222. int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  223. {
  224. int ret;
  225. avctx->codec = codec;
  226. avctx->frame_number = 0;
  227. if (codec->priv_data_size > 0) {
  228. avctx->priv_data = av_mallocz(codec->priv_data_size);
  229. if (!avctx->priv_data)
  230. return -ENOMEM;
  231. } else {
  232. avctx->priv_data = NULL;
  233. }
  234. ret = avctx->codec->init(avctx);
  235. if (ret < 0) {
  236. av_freep(&avctx->priv_data);
  237. return ret;
  238. }
  239. return 0;
  240. }
  241. int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  242. const short *samples)
  243. {
  244. int ret;
  245. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  246. avctx->frame_number++;
  247. return ret;
  248. }
  249. int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  250. const AVFrame *pict)
  251. {
  252. int ret;
  253. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  254. emms_c(); //needed to avoid a emms_c() call before every return;
  255. avctx->frame_number++;
  256. return ret;
  257. }
  258. /* decode a frame. return -1 if error, otherwise return the number of
  259. bytes used. If no frame could be decompressed, *got_picture_ptr is
  260. zero. Otherwise, it is non zero */
  261. int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  262. int *got_picture_ptr,
  263. UINT8 *buf, int buf_size)
  264. {
  265. int ret;
  266. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  267. buf, buf_size);
  268. emms_c(); //needed to avoid a emms_c() call before every return;
  269. if (*got_picture_ptr)
  270. avctx->frame_number++;
  271. return ret;
  272. }
  273. /* decode an audio frame. return -1 if error, otherwise return the
  274. *number of bytes used. If no frame could be decompressed,
  275. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  276. *size in BYTES. */
  277. int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples,
  278. int *frame_size_ptr,
  279. UINT8 *buf, int buf_size)
  280. {
  281. int ret;
  282. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  283. buf, buf_size);
  284. avctx->frame_number++;
  285. return ret;
  286. }
  287. int avcodec_close(AVCodecContext *avctx)
  288. {
  289. if (avctx->codec->close)
  290. avctx->codec->close(avctx);
  291. av_freep(&avctx->priv_data);
  292. avctx->codec = NULL;
  293. return 0;
  294. }
  295. AVCodec *avcodec_find_encoder(enum CodecID id)
  296. {
  297. AVCodec *p;
  298. p = first_avcodec;
  299. while (p) {
  300. if (p->encode != NULL && p->id == id)
  301. return p;
  302. p = p->next;
  303. }
  304. return NULL;
  305. }
  306. AVCodec *avcodec_find_encoder_by_name(const char *name)
  307. {
  308. AVCodec *p;
  309. p = first_avcodec;
  310. while (p) {
  311. if (p->encode != NULL && strcmp(name,p->name) == 0)
  312. return p;
  313. p = p->next;
  314. }
  315. return NULL;
  316. }
  317. AVCodec *avcodec_find_decoder(enum CodecID id)
  318. {
  319. AVCodec *p;
  320. p = first_avcodec;
  321. while (p) {
  322. if (p->decode != NULL && p->id == id)
  323. return p;
  324. p = p->next;
  325. }
  326. return NULL;
  327. }
  328. AVCodec *avcodec_find_decoder_by_name(const char *name)
  329. {
  330. AVCodec *p;
  331. p = first_avcodec;
  332. while (p) {
  333. if (p->decode != NULL && strcmp(name,p->name) == 0)
  334. return p;
  335. p = p->next;
  336. }
  337. return NULL;
  338. }
  339. AVCodec *avcodec_find(enum CodecID id)
  340. {
  341. AVCodec *p;
  342. p = first_avcodec;
  343. while (p) {
  344. if (p->id == id)
  345. return p;
  346. p = p->next;
  347. }
  348. return NULL;
  349. }
  350. const char *pix_fmt_str[] = {
  351. "yuv420p",
  352. "yuv422",
  353. "rgb24",
  354. "bgr24",
  355. "yuv422p",
  356. "yuv444p",
  357. "rgba32",
  358. "bgra32",
  359. "yuv410p",
  360. "yuv411p",
  361. };
  362. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  363. {
  364. const char *codec_name;
  365. AVCodec *p;
  366. char buf1[32];
  367. char channels_str[100];
  368. int bitrate;
  369. if (encode)
  370. p = avcodec_find_encoder(enc->codec_id);
  371. else
  372. p = avcodec_find_decoder(enc->codec_id);
  373. if (p) {
  374. codec_name = p->name;
  375. } else if (enc->codec_name[0] != '\0') {
  376. codec_name = enc->codec_name;
  377. } else {
  378. /* output avi tags */
  379. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  380. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  381. enc->codec_tag & 0xff,
  382. (enc->codec_tag >> 8) & 0xff,
  383. (enc->codec_tag >> 16) & 0xff,
  384. (enc->codec_tag >> 24) & 0xff);
  385. } else {
  386. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  387. }
  388. codec_name = buf1;
  389. }
  390. switch(enc->codec_type) {
  391. case CODEC_TYPE_VIDEO:
  392. snprintf(buf, buf_size,
  393. "Video: %s%s",
  394. codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
  395. if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  396. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  397. ", %s",
  398. pix_fmt_str[enc->pix_fmt]);
  399. }
  400. if (enc->width) {
  401. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  402. ", %dx%d, %0.2f fps",
  403. enc->width, enc->height,
  404. (float)enc->frame_rate / FRAME_RATE_BASE);
  405. }
  406. if (encode) {
  407. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  408. ", q=%d-%d", enc->qmin, enc->qmax);
  409. }
  410. bitrate = enc->bit_rate;
  411. break;
  412. case CODEC_TYPE_AUDIO:
  413. snprintf(buf, buf_size,
  414. "Audio: %s",
  415. codec_name);
  416. switch (enc->channels) {
  417. case 1:
  418. strcpy(channels_str, "mono");
  419. break;
  420. case 2:
  421. strcpy(channels_str, "stereo");
  422. break;
  423. case 6:
  424. strcpy(channels_str, "5:1");
  425. break;
  426. default:
  427. sprintf(channels_str, "%d channels", enc->channels);
  428. break;
  429. }
  430. if (enc->sample_rate) {
  431. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  432. ", %d Hz, %s",
  433. enc->sample_rate,
  434. channels_str);
  435. }
  436. /* for PCM codecs, compute bitrate directly */
  437. switch(enc->codec_id) {
  438. case CODEC_ID_PCM_S16LE:
  439. case CODEC_ID_PCM_S16BE:
  440. case CODEC_ID_PCM_U16LE:
  441. case CODEC_ID_PCM_U16BE:
  442. bitrate = enc->sample_rate * enc->channels * 16;
  443. break;
  444. case CODEC_ID_PCM_S8:
  445. case CODEC_ID_PCM_U8:
  446. case CODEC_ID_PCM_ALAW:
  447. case CODEC_ID_PCM_MULAW:
  448. bitrate = enc->sample_rate * enc->channels * 8;
  449. break;
  450. default:
  451. bitrate = enc->bit_rate;
  452. break;
  453. }
  454. break;
  455. default:
  456. av_abort();
  457. }
  458. if (encode) {
  459. if (enc->flags & CODEC_FLAG_PASS1)
  460. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  461. ", pass 1");
  462. if (enc->flags & CODEC_FLAG_PASS2)
  463. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  464. ", pass 2");
  465. }
  466. if (bitrate != 0) {
  467. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  468. ", %d kb/s", bitrate / 1000);
  469. }
  470. }
  471. /* Picture field are filled with 'ptr' addresses */
  472. void avpicture_fill(AVPicture *picture, UINT8 *ptr,
  473. int pix_fmt, int width, int height)
  474. {
  475. int size;
  476. size = width * height;
  477. switch(pix_fmt) {
  478. case PIX_FMT_YUV420P:
  479. picture->data[0] = ptr;
  480. picture->data[1] = picture->data[0] + size;
  481. picture->data[2] = picture->data[1] + size / 4;
  482. picture->linesize[0] = width;
  483. picture->linesize[1] = width / 2;
  484. picture->linesize[2] = width / 2;
  485. break;
  486. case PIX_FMT_YUV422P:
  487. picture->data[0] = ptr;
  488. picture->data[1] = picture->data[0] + size;
  489. picture->data[2] = picture->data[1] + size / 2;
  490. picture->linesize[0] = width;
  491. picture->linesize[1] = width / 2;
  492. picture->linesize[2] = width / 2;
  493. break;
  494. case PIX_FMT_YUV444P:
  495. picture->data[0] = ptr;
  496. picture->data[1] = picture->data[0] + size;
  497. picture->data[2] = picture->data[1] + size;
  498. picture->linesize[0] = width;
  499. picture->linesize[1] = width;
  500. picture->linesize[2] = width;
  501. break;
  502. case PIX_FMT_RGB24:
  503. case PIX_FMT_BGR24:
  504. picture->data[0] = ptr;
  505. picture->data[1] = NULL;
  506. picture->data[2] = NULL;
  507. picture->linesize[0] = width * 3;
  508. break;
  509. case PIX_FMT_RGBA32:
  510. case PIX_FMT_BGRA32:
  511. picture->data[0] = ptr;
  512. picture->data[1] = NULL;
  513. picture->data[2] = NULL;
  514. picture->linesize[0] = width * 4;
  515. break;
  516. case PIX_FMT_YUV422:
  517. picture->data[0] = ptr;
  518. picture->data[1] = NULL;
  519. picture->data[2] = NULL;
  520. picture->linesize[0] = width * 2;
  521. break;
  522. default:
  523. picture->data[0] = NULL;
  524. picture->data[1] = NULL;
  525. picture->data[2] = NULL;
  526. break;
  527. }
  528. }
  529. int avpicture_get_size(int pix_fmt, int width, int height)
  530. {
  531. int size;
  532. size = width * height;
  533. switch(pix_fmt) {
  534. case PIX_FMT_YUV420P:
  535. size = (size * 3) / 2;
  536. break;
  537. case PIX_FMT_YUV422P:
  538. size = (size * 2);
  539. break;
  540. case PIX_FMT_YUV444P:
  541. size = (size * 3);
  542. break;
  543. case PIX_FMT_RGB24:
  544. case PIX_FMT_BGR24:
  545. size = (size * 3);
  546. break;
  547. case PIX_FMT_RGBA32:
  548. case PIX_FMT_BGRA32:
  549. size = (size * 4);
  550. break;
  551. case PIX_FMT_YUV422:
  552. size = (size * 2);
  553. break;
  554. default:
  555. size = -1;
  556. break;
  557. }
  558. return size;
  559. }
  560. unsigned avcodec_version( void )
  561. {
  562. return LIBAVCODEC_VERSION_INT;
  563. }
  564. unsigned avcodec_build( void )
  565. {
  566. return LIBAVCODEC_BUILD;
  567. }
  568. /* must be called before any other functions */
  569. void avcodec_init(void)
  570. {
  571. static int inited = 0;
  572. if (inited != 0)
  573. return;
  574. inited = 1;
  575. //dsputil_init();
  576. }
  577. /* this can be called after seeking and before trying to decode the next keyframe */
  578. void avcodec_flush_buffers(AVCodecContext *avctx)
  579. {
  580. int i;
  581. MpegEncContext *s = avctx->priv_data;
  582. switch(avctx->codec_id){
  583. case CODEC_ID_MPEG1VIDEO:
  584. case CODEC_ID_H263:
  585. case CODEC_ID_RV10:
  586. case CODEC_ID_MJPEG:
  587. case CODEC_ID_MJPEGB:
  588. case CODEC_ID_MPEG4:
  589. case CODEC_ID_MSMPEG4V1:
  590. case CODEC_ID_MSMPEG4V2:
  591. case CODEC_ID_MSMPEG4V3:
  592. case CODEC_ID_WMV1:
  593. case CODEC_ID_WMV2:
  594. case CODEC_ID_H263P:
  595. case CODEC_ID_H263I:
  596. case CODEC_ID_SVQ1:
  597. for(i=0; i<MAX_PICTURE_COUNT; i++){
  598. if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
  599. || s->picture[i].type == FF_BUFFER_TYPE_USER))
  600. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  601. }
  602. break;
  603. default:
  604. //FIXME
  605. break;
  606. }
  607. }
  608. static int raw_encode_init(AVCodecContext *s)
  609. {
  610. return 0;
  611. }
  612. static int raw_decode_frame(AVCodecContext *avctx,
  613. void *data, int *data_size,
  614. UINT8 *buf, int buf_size)
  615. {
  616. return -1;
  617. }
  618. static int raw_encode_frame(AVCodecContext *avctx,
  619. unsigned char *frame, int buf_size, void *data)
  620. {
  621. return -1;
  622. }
  623. AVCodec rawvideo_codec = {
  624. "rawvideo",
  625. CODEC_TYPE_VIDEO,
  626. CODEC_ID_RAWVIDEO,
  627. 0,
  628. raw_encode_init,
  629. raw_encode_frame,
  630. NULL,
  631. raw_decode_frame,
  632. };