當前位置:首頁 » 安卓系統 » ffmpegandroid解碼器

ffmpegandroid解碼器

發布時間: 2023-02-03 20:59:54

❶ 如何在Android用FFmpeg解碼圖像

解決方案:
include/libswscale/jni;
SDL_mutex *mutex;
int allocated;

SDL_Thread *parse_tid: clarck
*/*source height & width*/./
#include "
unsigned int audio_buf_size;
SDL_Renderer *renderer;
uint8_t *audio_pkt_data;
SDL_cond *pictq_cond。

/
int64_t audio_tgt_channel_layout;
#include "include/SDL_events;include/.;
PacketQueue audioq;*
* SDL_Lesson;logger;.h".h"include/
struct SwsContext *sws_ctx.h"/

typedef struct VideoState {
char filename[1024].h"
SDL_cond *cond;
uint8_t *audio_buf;
#include "ffmpeg/
enum AVSampleFormat audio_tgt_fmt;swscale, *last_pkt.h", pictq_windex;
int audio_src_freq.h"

AVFrame* rawdata;
enum AVSampleFormat audio_src_fmt;
int pictq_size;
PacketQueue videoq;
int audio_tgt_freq;
#include <, pictq_rindex;
SDL_Thread *audio_tid;
int audio_tgt_channels;
int size.;ffmpeg/

VideoState *global_video_state.h".c
*
* Created on; 1 second of 48khz 32bit audio

typedef struct PacketQueue {
AVPacketList *first_pkt;ffmpeg/
int videoStream;libavcodec/
AVStream *audio_st創建一個VideoPicture結構體用來保存解碼出來的圖像., audioStream; /;libavutil/android/native_window_jni;
int width;
unsigned int audio_buf_index;
#include <.;SDL: Aug 12../
SDL_mutex *pictq_mutex;

AVStream *video_st;
#include "./libswresample/
} PacketQueue;SDL_thread;./
} VideoPicture;
AVFormatContext *ic;
SDL_Texture *bmp./ffmpeg/
struct SwrContext *swr_ctx.h"
int64_t audio_src_channel_layout,uint8_t;
int nb_packets;include/
#include "avcodec./
SDL_Thread *video_tid;
#include ", 2014
* Author, height;
int audio_src_channels;;avformat;include/
AVFrame *audio_frame;.;
#include ",audio_buf2) [AVCODEC_MAX_AUDIO_FRAME_SIZE * 4];
int audio_pkt_size;
#include "pixfmt;swresample.h>
DECLARE_ALIGNED(16;

typedef struct VideoPicture {
SDL_Window *screen;;

int quit;

#define SDL_AUDIO_BUFFER_SIZE 1024

#define MAX_AUDIO_SIZE (5 * 16 * 1024)
#define MAX_VIDEO_SIZE (5 * 256 * 1024)

#define FF_ALLOC_EVENT (SDL_USEREVENT)
#define FF_REFRESH_EVENT (SDL_USEREVENT + 1)
#define FF_QUIT_EVENT (SDL_USEREVENT + 2)

#define VIDEO_PICTURE_QUEUE_SIZE 1
#define AVCODEC_MAX_AUDIO_FRAME_SIZE 192000 /ffmpeg/
} VideoState;libavformat/
AVPacket audio_pkt.h>

AVIOContext *io_ctx;

VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE].h"
#include "

❷ 如何在Android用FFmpeg解碼圖像

創建一個VideoPicture結構體用來保存解碼出來的圖像。

LOCAL_PATH := $(call my-dir)
###########################
#
# SDL shared library
#
###########################
include $(CLEAR_VARS)
LOCAL_MODULE := SDL2
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES)
LOCAL_SRC_FILES := \
$(subst $(LOCAL_PATH)/,, \
$(wildcard $(LOCAL_PATH)/src/*.c) \
$(wildcard $(LOCAL_PATH)/src/audio/*.c) \
$(wildcard $(LOCAL_PATH)/src/audio/android/*.c) \
$(wildcard $(LOCAL_PATH)/src/audio/mmy/*.c) \
$(LOCAL_PATH)/src/atomic/SDL_atomic.c \
$(LOCAL_PATH)/src/atomic/SDL_spinlock.c.arm \
$(wildcard $(LOCAL_PATH)/src/core/android/*.c) \
$(wildcard $(LOCAL_PATH)/src/cpuinfo/*.c) \
$(wildcard $(LOCAL_PATH)/src/dynapi/*.c) \
$(wildcard $(LOCAL_PATH)/src/events/*.c) \
$(wildcard $(LOCAL_PATH)/src/file/*.c) \
$(wildcard $(LOCAL_PATH)/src/haptic/*.c) \
$(wildcard $(LOCAL_PATH)/src/haptic/mmy/*.c) \
$(wildcard $(LOCAL_PATH)/src/joystick/*.c) \
$(wildcard $(LOCAL_PATH)/src/joystick/android/*.c) \
$(wildcard $(LOCAL_PATH)/src/loadso/dlopen/*.c) \
$(wildcard $(LOCAL_PATH)/src/power/*.c) \
$(wildcard $(LOCAL_PATH)/src/power/android/*.c) \
$(wildcard $(LOCAL_PATH)/src/filesystem/mmy/*.c) \
$(wildcard $(LOCAL_PATH)/src/render/*.c) \
$(wildcard $(LOCAL_PATH)/src/render/*/*.c) \
$(wildcard $(LOCAL_PATH)/src/stdlib/*.c) \
$(wildcard $(LOCAL_PATH)/src/thread/*.c) \
$(wildcard $(LOCAL_PATH)/src/thread/pthread/*.c) \
$(wildcard $(LOCAL_PATH)/src/timer/*.c) \
$(wildcard $(LOCAL_PATH)/src/timer/unix/*.c) \
$(wildcard $(LOCAL_PATH)/src/video/*.c) \
$(wildcard $(LOCAL_PATH)/src/video/android/*.c) \
$(wildcard $(LOCAL_PATH)/src/test/*.c))
LOCAL_CFLAGS += -DGL_GLEXT_PROTOTYPES
LOCAL_LDLIBS := -ldl -lGLESv1_CM -lGLESv2 -llog -landroid
include $(BUILD_SHARED_LIBRARY)
###########################
#
# SDL static library
#
###########################
#LOCAL_MODULE := SDL2_static
#LOCAL_MODULE_FILENAME := libSDL2
#LOCAL_SRC_FILES += $(LOCAL_PATH)/src/main/android/SDL_android_main.c
#LOCAL_LDLIBS :=
#LOCAL_EXPORT_LDLIBS := -Wl,--undefined=Java_org_libsdl_app_SDLActivity_nativeInit -ldl -lGLESv1_CM -lGLESv2 -llog -landroid
#include $(BUILD_STATIC_LIBRARY)

二、參考[原]如何在Android用FFmpeg解碼圖像, 在工程中新建一個ffmpeg文件夾,將與ffmpeg相關的頭文件include進來。ffmpeg文件夾下的Android.mk內容:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := ffmpeg
LOCAL_SRC_FILES := /path/to/build/ffmpeg/libffmpeg.so
include $(PREBUILT_SHARED_LIBRARY)

三、新建player文件夾,用來編寫解碼與顯示文件。player.c文件內容:

/*
* SDL_Lesson.c
*
* Created on: Aug 12, 2014
* Author: clarck
*/
#include <jni.h>
#include <android/native_window_jni.h>
#include "SDL.h"
#include "SDL_thread.h"
#include "SDL_events.h"
#include "../include/logger.h"
#include "../ffmpeg/include/libavcodec/avcodec.h"
#include "../ffmpeg/include/libavformat/avformat.h"
#include "../ffmpeg/include/libavutil/pixfmt.h"
#include "../ffmpeg/include/libswscale/swscale.h"
int main(int argc, char *argv[]) {
char *file_path = argv[1];
LOGI("file_path:%s", file_path);
AVFormatContext *pFormatCtx;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVFrame *pFrame, *pFrameYUV;
AVPacket *packet;
uint8_t *out_buffer;
SDL_Texture *bmp = NULL;
SDL_Window *screen = NULL;
SDL_Rect rect;
SDL_Event event;
static struct SwsContext *img_convert_ctx;
int videoStream, i, numBytes;
int ret, got_picture;
av_register_all();
pFormatCtx = avformat_alloc_context();
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
LOGE("Could not initialize SDL - %s. \n", SDL_GetError());
exit(1);
}
if (avformat_open_input(&pFormatCtx, file_path, NULL, NULL) != 0) {
LOGE("can't open the file. \n");
return -1;
}
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
LOGE("Could't find stream infomation.\n");
return -1;
}
videoStream = 1;
for (i = 0; i < pFormatCtx->nb_streams; i++) {
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStream = i;
}
}
LOGI("videoStream:%d", videoStream);
if (videoStream == -1) {
LOGE("Didn't find a video stream.\n");
return -1;
}
pCodecCtx = pFormatCtx->streams[videoStream]->codec;
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if (pCodec == NULL) {
LOGE("Codec not found.\n");
return -1;
}
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
LOGE("Could not open codec.\n");
return -1;
}
pFrame = av_frame_alloc();
pFrameYUV = av_frame_alloc();
//---------------------------init sdl---------------------------//
screen = SDL_CreateWindow("My Player Window", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, pCodecCtx->width, pCodecCtx->height,
SDL_WINDOW_FULLSCREEN | SDL_WINDOW_OPENGL);
SDL_Renderer *renderer = SDL_CreateRenderer(screen, -1, 0);
bmp = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_YV12,
SDL_TEXTUREACCESS_STREAMING, pCodecCtx->width, pCodecCtx->height);
//-------------------------------------------------------------//

img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height,
pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height,
AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
numBytes = avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width,
pCodecCtx->height);
out_buffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
avpicture_fill((AVPicture *) pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P,
pCodecCtx->width, pCodecCtx->height);
rect.x = 0;
rect.y = 0;
rect.w = pCodecCtx->width;
rect.h = pCodecCtx->height;
int y_size = pCodecCtx->width * pCodecCtx->height;
packet = (AVPacket *) malloc(sizeof(AVPacket));
av_new_packet(packet, y_size);
av_mp_format(pFormatCtx, 0, file_path, 0);
while (av_read_frame(pFormatCtx, packet) >= 0) {
if (packet->stream_index == videoStream) {
ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture,
packet);
if (ret < 0) {
LOGE("decode error.\n");
return -1;
}
LOGI("got_picture:%d", got_picture);
if (got_picture) {
sws_scale(img_convert_ctx,
(uint8_t const * const *) pFrame->data,
pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data,
pFrameYUV->linesize);
////iPitch 計算yuv一行數據占的位元組數
SDL_UpdateTexture(bmp, &rect, pFrameYUV->data[0], pFrameYUV->linesize[0]);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, bmp, &rect, &rect);
SDL_RenderPresent(renderer);
}
SDL_Delay(50);
}
av_free_packet(packet);
SDL_PollEvent(&event);
switch (event.type) {
case SDL_QUIT:
SDL_Quit();
exit(0);
break;
default:
break;
}

❸ 如何在Android用FFmpeg+SDL2.0解碼聲音

一、創建一個VideoPicture結構體用來保存解碼出來的圖像;
二、添加數據隊列的初始化、添加以及讀取的函數;
三、audio_decode_frame():解碼音頻;
四、audio_callback(): 回調函數,向SDL緩沖區填充數據;
五、創建刷新相關的函數;
六、添加顯示函數;
七、分配顯示輸出內存空間;
八、解碼線程,將解碼器,建立音頻線,保存重要信息到數據結構中;
九、編寫Main函數用來調用解碼線程。
知識點延伸:
FFmpeg是一個開源跨的和音頻流方案,屬於自由,採用LGPL或GPL許可證(依據你選擇的組件)。它提供了錄制、轉換以及流化音的完整解決方案。它包含了非常先進的音頻/編解碼庫libavcodec,為了保證高可移植性和編解碼質量,libavcodec里很多codec都是從頭開發的。FFmpeg在Linux下開發,但它同樣也可以在其它操作系統環境中編譯運行。
SDL2.0(Simple DirectMedia Layer)是一套開放源代碼的跨多媒體開發庫,使用C語言寫成。SDL多用於開發游戲、模擬器、媒體播放器等多媒體應用領域。SDL內置了調用OpenGL的函數。SDL提供了數種控制圖像、聲音、輸出入的函數,讓開發者只要用相同或是相似的代碼就可以開發出跨多個(Linux、Windows、Mac OS X等)的應用。

❹ 如何在Android用FFmpeg解碼圖像

這涉及到兩個問題,一個是解碼,另一個是顯示,解碼問題要先交叉編譯ffmpeg,然後參考下面的解碼流程

avcodec_register_all();
/*
########################################
[1]
########################################
*/
av_register_all();
/*
// Open video file
########################################
[2]
########################################
*/
pFormatCtx = avformat_alloc_context();

if(avformat_open_input(&pFormatCtx, filename, NULL, NULL)!=0)
return -1; // Couldn't open file

// Retrieve stream information
/*
########################################
[3]
########################################
*/
if(avformat_find_stream_info(pFormatCtx,NULL)<0)
return -1; // Couldn't find stream information

// Dump information about file onto standard error
// mp_format(pFormatCtx, 0, argv[1], 0);

// Find the first video stream
videoStream=-1;
for(i=0; i<pFormatCtx->nb_streams; i++)
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
videoStream=i;
break;
}
if(videoStream==-1)
return -1; // Didn't find a video stream

// Get a pointer to the codec context for the video stream
pCodecCtx=pFormatCtx->streams[videoStream]->codec;

// Find the decoder for the video stream
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL) {
fprintf(stderr, "Unsupported codec!\n");
return -1; // Codec not found
}
// Open codec
if(avcodec_open2(pCodecCtx, pCodec,NULL)<0)

pFrame=avcodec_alloc_frame();

// Allocate an picture structure
avpicture_alloc(&picture, PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);

// Determine required buffer size and allocate buffer
img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);
if(img_convert_ctx == NULL)
{
fprintf(stderr, "Cannot initialize the conversion context!\n");
exit(1);
}

// Read frames and save first five frames to disk
/*
########################################
[4]
########################################
*/
i=0;
dirtyRegion.set(android::Rect(0x3FFF, 0x3FFF));
while(av_read_frame(pFormatCtx, &packet)>=0) {
// Is this a packet from the video stream?
if(packet.stream_index==videoStream) {
// Decode video frame
avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished,
&packet);

// Did we get a video frame?
if(frameFinished) {
// Convert the image from its native format to RGB

sws_scale(img_convert_ctx,
pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
picture.data, picture.linesize);

}
}

// Free the packet that was allocated by av_read_frame
av_free_packet(&packet);
}
pFrame為解碼後的數據,將它顯示在畫布上,就完成了FFMEPG解碼

❺ (四)Android通過ffmpeg解碼音頻

音頻解碼與視頻解碼的流程大致相同,唯一的區別只有處理幀數據的時候,視頻是像素轉換並顯示,而音頻則是重采樣並播放。
所以基於這一點,在以後做架構的時候,可以將音頻、視頻這兩部分,相同的邏輯放在共同的父類當中,自身子類則各自處理視頻顯示和聲音播放等邏輯。

然後,就是將重采樣後的數據,交給OpenSLES去處理。

OpenSL ES 全稱為: Open Sound Library for Embedded Systems,是一個針對嵌入式系統的開放硬體音頻加速庫,支持音頻的採集和播放,它提供了一套高性能、低延遲的音頻功能實現方法,並且實現了軟硬體音頻性能的跨平台部署,大大降低了上層處理音頻應用的開發難度。

Object 和 Interface 是OpenSL ES 中的兩大基本概念,可以類比為 Java 中的對象和介面。在 OpenSL ES 中, 每個 Object 可以存在一系列的 Interface ,並且為每個對象都提供了一系列的基本操作,如 Realize,GetState,Destroy 等。

重要的一點,只有通過 GetInterface 方法拿到 Object 的 Interface ,才能使用 Object 提供的功能。

這里的例子是播放一個手機里的視頻文件,所以只介紹OpenSLES Audio Player 播放音頻的過程。

音頻播放的大致流程就是這樣,其實還有音頻錄入的功能的,這個以後再介紹。音頻的解碼,大部分都和視頻解碼的流程一致,只要你熟悉OpenGLES的幾個API和流程,基本都能播放成功。

熱點內容
內置存儲卡可以拆嗎 發布:2025-05-18 04:16:35 瀏覽:335
編譯原理課時設置 發布:2025-05-18 04:13:28 瀏覽:378
linux中進入ip地址伺服器 發布:2025-05-18 04:11:21 瀏覽:612
java用什麼軟體寫 發布:2025-05-18 03:56:19 瀏覽:32
linux配置vim編譯c 發布:2025-05-18 03:55:07 瀏覽:107
砸百鬼腳本 發布:2025-05-18 03:53:34 瀏覽:942
安卓手機如何拍視頻和蘋果一樣 發布:2025-05-18 03:40:47 瀏覽:739
為什麼安卓手機連不上蘋果7熱點 發布:2025-05-18 03:40:13 瀏覽:802
網卡訪問 發布:2025-05-18 03:35:04 瀏覽:510
接收和發送伺服器地址 發布:2025-05-18 03:33:48 瀏覽:371