Chinaunix首页 | 论坛 | 博客
  • 博客访问: 85412
  • 博文数量: 14
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 153
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-13 11:26
文章分类

全部博文(14)

文章存档

2015年(2)

2014年(7)

2013年(5)

我的朋友

分类: C/C++

2014-04-22 20:51:42


Tutorial 04: Spawning Threads

Overview

Last time we added audio support by taking advantage of SDL's audio functions. SDL started a thread that made callbacks to a function we defined every time it needed audio. Now we're going to do the same sort of thing with the video display. This makes the code more modular and easier to work with - especially when we want to add syncing. So where do we start?

First we notice that our main function is handling an awful lot: it's running through the event loop, reading in packets, and decoding the video. So what we're going to do is split all those apart: we're going to have a thread that will be responsible for decoding the packets; these packets will then be added to the queue and read by the corresponding audio and video threads. The audio thread we have already set up the way we want it; the video thread will be a little more complicated since we have to display the video ourselves. We will add the actual display code to the main loop. But instead of just displaying video every time we loop, we will integrate the video display into the event loop. The idea is to decode the video, save the resulting frame in another queue, then create a custom event (FF_REFRESH_EVENT) that we add to the event system, then when our event loop sees this event, it will display the next frame in the queue. Here's a handy ASCII art illustration of what is going on:

 ________ audio  _______      _____
|        | pkts |       |    |     | to spkr
| DECODE |----->| AUDIO |--->| SDL |-->
|________|      |_______|    |_____|
    |  video     _______
    |   pkts    |       |
    +---------->| VIDEO |
 ________       |_______|   _______
|       |          |       |       |
| EVENT |          +------>| VIDEO | to mon.
| LOOP  |----------------->| DISP. |-->
|_______|<---FF_REFRESH----|_______|
The main purpose of moving controlling the video display via the event loop is that using an  thread, we can control exactly when the next video frame shows up on the screen. When we finally sync the video in the next tutorial, it will be a simple matter to add the code that will schedule the next video refresh so the right picture is being shown on the screen at the right time.


Simplifying Code

We're also going to clean up the code a bit. We have all this audio and video codec information, and we're going to be adding queues and buffers and who knows what else. All this stuff is for one logical unit, viz. the movie. So we're going to make a large struct that will hold all that information called the VideoState.

typedef struct VideoState {  *pFormatCtx;
  int             videoStream, audioStream;  *audio_st;
  PacketQueue     audioq;
  uint8_t         audio_buf[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2];
  unsigned int    audio_buf_size;
  unsigned int    audio_buf_index;  audio_pkt;
  uint8_t         *audio_pkt_data;
  int             audio_pkt_size;  *video_st;
  PacketQueue     videoq;

  VideoPicture    pictq[VIDEO_PICTURE_QUEUE_SIZE];
  int             pictq_size, pictq_rindex, pictq_windex;  *pictq_mutex;  *pictq_cond;  *parse_tid;  *video_tid;

  char            filename[1024];
  int             quit;
} VideoState;
Here we see a glimpse of what we're going to get to. First we see the basic information - the format context and the indices of the audio and video stream, and the corresponding  objects. Then we can see that we've moved some of those audio buffers into this structure. These (audio_buf, audio_buf_size, etc.) were all for information about audio that was still lying around (or the lack thereof). We've added another queue for the video, and a buffer (which will be used as a queue; we don't need any fancy queueing stuff for this) for the decoded frames (saved as an overlay). The VideoPicture struct is of our own creations (we'll see what's in it when we come to it). We also notice that we've allocated pointers for the two extra threads we will create, and the quit flag and the filename of the movie.


So now we take it all the way back to the main function to see how this changes our program. Let's set up our VideoState struct:

int main(int argc, char *argv[]) {  event;

  VideoState      *is;

  is = (sizeof(VideoState));
() is a nice function that will allocate memory for us and zero it out.


Then we'll initialize our locks for the display buffer (pictq), because since the event loop calls our display function - the display function, remember, will be pulling pre-decoded frames from pictq. At the same time, our video decoder will be putting information into it - we don't know who will get there first. Hopefully you recognize that this is a classic race condition. So we allocate it now before we start any threads. Let's also copy the filename of our movie into our VideoState.

pstrcpy(is->filename, sizeof(is->filename), argv[1]);

is->pictq_mutex = ();
is->pictq_cond = ();
pstrcpy is a function from ffmpeg that does some extra bounds checking beyond strncpy.


Our First Thread

Now let's finally launch our threads and get the real work done:

schedule_refresh(is, 40);

is->parse_tid = (decode_thread, is);
if(!is->parse_tid) { (is);
  return -1;
}
schedule_refresh is a function we will define later. What it basically does is tell the system to push a FF_REFRESH_EVENT after the specified number of milliseconds. This will in turn call the video refresh function when we see it in the event queue. But for now, let's look at().


() does just that - it spawns a new thread that has complete access to all the memory of the original process, and starts the thread running on the function we give it. It will also pass that function user-defined data. In this case, we're calling decode_thread() and with ourVideoState struct attached. The first half of the function has nothing new; it simply does the work of opening the file and finding the index of the audio and video streams. The only thing we do different is save the format context in our big struct. After we've found our stream indices, we call another function that we will define, stream_component_open(). This is a pretty natural way to split things up, and since we do a lot of similar things to set up the video and audio codec, we reuse some code by making this a function.

The stream_component_open() function is where we will find our codec decoder, set up our audio options, save important information to our big struct, and launch our audio and video threads. This is where we would also insert other options, such as forcing the codec instead of autodetecting it and so forth. Here it is:

int stream_component_open(VideoState *is, int stream_index) {  *pFormatCtx = is->pFormatCtx;  *codecCtx;
  AVCodec *codec;  wanted_spec, spec;

  if(stream_index < 0 || stream_index >= pFormatCtx->nb_streams) {
    return -1;
  }

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

  if(codecCtx->codec_type == CODEC_TYPE_AUDIO) {
    // Set audio settings from codec info
    wanted_spec.freq = codecCtx->sample_rate;
    /* .... */
    wanted_spec.callback = audio_callback;
    wanted_spec.userdata = is;
    
    if((&wanted_spec, &spec) < 0) {
      fprintf(stderr, ": %s\n", SDL_GetError());
      return -1;
    }
  }
  codec = (codecCtx->codec_id);
  if(!codec || ((codecCtx, codec) < 0)) {
    fprintf(stderr, "Unsupported codec!\n");
    return -1;
  }

  switch(codecCtx->codec_type) {
  case CODEC_TYPE_AUDIO:
    is->audioStream = stream_index;
    is->audio_st = pFormatCtx->streams[stream_index];
    is->audio_buf_size = 0;
    is->audio_buf_index = 0;
    memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
    packet_queue_init(&is->audioq); (0);
    break;
  case CODEC_TYPE_VIDEO:
    is->videoStream = stream_index;
    is->video_st = pFormatCtx->streams[stream_index];
    
    packet_queue_init(&is->videoq);
    is->video_tid = (video_thread, is);
    break;
  default:
    break;
  }
}
This is pretty much the same as the code we had before, except now it's generalized for audio and video. Notice that instead of aCodecCtx, we've set up our big struct as the userdata for our audio callback. We've also saved the streams themselves as audio_st and video_st. We also have added our video queue and set it up in the same way we set up our audio queue. Most of the point is to launch the video and audio threads. These bits do it:
    (0);
    break;

/* ...... */

    is->video_tid = (video_thread, is);
We remember () from last time, and () is used as in the exact same way as before. We'll get back to ourvideo_thread() function.


Before that, let's go back to the second half of our decode_thread() function. It's basically just a for loop that will read in a packet and put it on the right queue:

  for(;;) {
    if(is->quit) {
      break;
    }
    // seek stuff goes here
    if(is->audioq.size > MAX_AUDIOQ_SIZE ||
       is->videoq.size > MAX_VIDEOQ_SIZE) { (10);
      continue;
    }
    if((is->pFormatCtx, packet) < 0) {
      if((&pFormatCtx->pb) == 0) { (100); /* no error; wait for user input */
	continue;
      } else {
	break;
      }
    }
    // Is this a packet from the video stream?
    if(packet->stream_index == is->videoStream) {
      packet_queue_put(&is->videoq, packet);
    } else if(packet->stream_index == is->audioStream) {
      packet_queue_put(&is->audioq, packet);
    } else { (packet);
    }
  }
Nothing really new here, except that we now have a max size for our audio and video queue, and we've added a function that will check for read errors. The format context has a ByteIOContext struct inside it called pbByteIOContext is the structure that basically keeps all the low-level file information in it.  checks that structure to see if there was some kind of error reading from our file.


After our for loop, we have all the code for waiting for the rest of the program to end or informing it that we've ended. This code is instructive because it shows us how we push events - something we'll have to later to display the video.

  while(!is->quit) { (100);
  }

 fail:
  if(1){  event;
    event.type = FF_QUIT_EVENT;
    event.user.data1 = is; (&event);
  }
  return 0;
We get values for user events by using the SDL constant SDL_USEREVENT. The first user event should be assigned the value SDL_USEREVENT, the next SDL_USEREVENT + 1, and so on. FF_QUIT_EVENT is defined in our program as SDL_USEREVENT + 2. We can also pass user data if we like, too, and here we pass our pointer to the big struct. Finally we call (). In our event loop switch, we just put this by theSDL_QUIT_EVENT section we had before. We'll see our event loop in more detail; for now, just be assured that when we push the FF_QUIT_EVENT, we'll catch it later and raise our quit flag.


Getting the Frame: video_thread

After we have our codec prepared, we start our video thread. This thread reads in packets from the video queue, decodes the video into frames, and then calls a queue_picture function to put the processed frame onto a picture queue:

int video_thread(void *arg) {
  VideoState *is = (VideoState *)arg;  pkt1, *packet = &pkt1;
  int len1, frameFinished;  *pFrame;

  pFrame = ();

  for(;;) {
    if(packet_queue_get(&is->videoq, packet, 1) < 0) {
      // means we quit getting packets
      break;
    }
    // Decode video frame
    len1 = (is->video_st->codec, pFrame, &frameFinished, 
				packet->data, packet->size);

    // Did we get a video frame?
    if(frameFinished) {
      if(queue_picture(is, pFrame) < 0) {
	break;
      }
    } (packet);
  } (pFrame);
  return 0;
}
Most of this function should be familiar by this point. We've moved our  function here, just replaced some of the arguments; for example, we have the  stored in our big struct, so we get our codec from there. We just keep getting packets from our video queue until someone tells us to quit or we encounter an error.


Queueing the Frame

Let's look at the function that stores our decoded frame, pFrame in our picture queue. Since our picture queue is an SDL overlay (presumably to allow the video display function to have as little calculation as possible), we need to convert our frame into that. The data we store in the picture queue is a struct of our making:

typedef struct VideoPicture {  *bmp;
  int width, height; /* source height & width */
  int allocated;
} VideoPicture;
Our big struct has a buffer of these in it where we can store them. However, we need to allocate the  ourselves (notice theallocated flag that will indicate whether we have done so or not).


To use this queue, we have two pointers - the writing index and the reading index. We also keep track of how many actual pictures are in the buffer. To write to the queue, we're going to first wait for our buffer to clear out so we have space to store our VideoPicture. Then we check and see if we have already allocated the overlay at our writing index. If not, we'll have to allocate some space. We also have to reallocate the buffer if the size of the window has changed! However, instead of allocating it here, to avoid locking issues. (I'm still not quite sure why; I believe it's to avoid calling the SDL overlay functions in different threads.)

int queue_picture(VideoState *is,  *pFrame) {

  VideoPicture *vp;
  int dst_pix_fmt;  pict;

  /* wait until we have space for a new pic */ (is->pictq_mutex);
  while(is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE &&
	!is->quit) { (is->pictq_cond, is->pictq_mutex);
  } (is->pictq_mutex);

  if(is->quit)
    return -1;

  // windex is set to 0 initially
  vp = &is->pictq[is->pictq_windex];

  /* allocate or resize the buffer! */
  if(!vp->bmp ||
     vp->width != is->video_st->codec->width ||
     vp->height != is->video_st->codec->height) {  event;

    vp->allocated = 0;
    /* we have to do it in the main thread */
    event.type = FF_ALLOC_EVENT;
    event.user.data1 = is; (&event);

    /* wait until we have a picture allocated */ (is->pictq_mutex);
    while(!vp->allocated && !is->quit) { (is->pictq_cond, is->pictq_mutex);
    } (is->pictq_mutex);
    if(is->quit) {
      return -1;
    }
  }
The event mechanism here is the same one we saw earlier when we wanted to quit. We've defined FF_ALLOC_EVENT as SDL_USEREVENT. We push the event and then wait on the conditional variable for the allocation function to run.


Let's look at how we change our event loop:

for(;;) { (&event);
  switch(event.type) {
/* ... */  
  case FF_ALLOC_EVENT:
    alloc_picture(event.user.data1);
    break;
Remember that event.user.data1 is our big struct. That was simple enough. Let's look at the alloc_picture() function:
void alloc_picture(void *userdata) {

  VideoState *is = (VideoState *)userdata;
  VideoPicture *vp;

  vp = &is->pictq[is->pictq_windex];
  if(vp->bmp) {
    // we already have one make another, bigger/smaller (vp->bmp);
  }
  // Allocate a place to put our YUV image on that screen
  vp->bmp = (is->video_st->codec->width,
				 is->video_st->codec->height,
				 SDL_YV12_OVERLAY,
				 screen);
  vp->width = is->video_st->codec->width;
  vp->height = is->video_st->codec->height; (is->pictq_mutex);
  vp->allocated = 1; (is->pictq_cond); (is->pictq_mutex);
}
You should recognize the  function that we've moved from our main loop to this section. This code should be fairly self-explanitory by now. Remember that we save the width and height in the VideoPicture structure because we need to make sure that our video size doesn't change for some reason.


Okay, we're all settled and we have our YUV overlay allocated and ready to receive a picture. Let's go back to queue_picture and look at the code to copy the frame into the overlay. You should recognize that part of it:

int queue_picture(VideoState *is,  *pFrame) {

  /* Allocate a frame if we need it... */
  /* ... */
  /* We have a place to put our picture on the queue */

  if(vp->bmp) { (vp->bmp);
    
    dst_pix_fmt = PIX_FMT_YUV420P;
    /* point pict at the queue */

    pict.data[0] = vp->bmp->pixels[0];
    pict.data[1] = vp->bmp->pixels[2];
    pict.data[2] = vp->bmp->pixels[1];
    
    pict.linesize[0] = vp->bmp->pitches[0];
    pict.linesize[1] = vp->bmp->pitches[2];
    pict.linesize[2] = vp->bmp->pitches[1];
    
    // Convert the image into YUV format that SDL uses (&pict, dst_pix_fmt,
		( *)pFrame, is->video_st->codec->pix_fmt, 
		is->video_st->codec->width, is->video_st->codec->height); (vp->bmp);
    /* now we inform our display thread that we have a pic ready */
    if(++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE) {
      is->pictq_windex = 0;
    } (is->pictq_mutex);
    is->pictq_size++; (is->pictq_mutex);
  }
  return 0;
}
The majority of this part is simply the code we used earlier to fill the YUV overlay with our frame. The last bit is simply "adding" our value onto the queue. The queue works by adding onto it until it is full, and reading from it as long as there is something on it. Therefore everything depends upon the is->pictq_size value, requiring us to lock it. So what we do here is increment the write pointer (and rollover if necessary), then lock the queue and increase its size. Now our reader will know there is more information on the queue, and if this makes our queue full, our writer will know about it.


Displaying the Video

That's it for our video thread! Now we've wrapped up all the loose threads except for one — remember that we called the schedule_refresh()function way back? Let's see what that actually did:

/* schedule a video refresh in 'delay' ms */
static void schedule_refresh(VideoState *is, int delay) { (delay, sdl_refresh_timer_cb, is);
}
() is an SDL function that simply makes a callback to the user-specfied function after a certain number of milliseconds (and optionally carrying some user data). We're going to use this function to schedule video updates - every time we call this function, it will set the timer, which will trigger an event, which will have our main() function in turn call a function that pulls a frame from our picture queue and displays it! Phew!


But first thing's first. Let's trigger that event. That sends us over to:

static Uint32 sdl_refresh_timer_cb(Uint32 interval, void *opaque) {  event;
  event.type = FF_REFRESH_EVENT;
  event.user.data1 = opaque; (&event);
  return 0; /* 0 means stop timer */
}
Here is the now-familiar event push. FF_REFRESH_EVENT is defined here as SDL_USEREVENT + 1. One thing to notice is that when we return 0, SDL stops the timer so the callback is not made again.


Now we've pushed an FF_REFRESH_EVENT, we need to handle it in our event loop:

for(;;) { (&event);
  switch(event.type) {
  /* ... */
  case FF_REFRESH_EVENT:
    video_refresh_timer(event.user.data1);
    break;
and that sends us to this function, which will actually pull the data from our picture queue:
void video_refresh_timer(void *userdata) {

  VideoState *is = (VideoState *)userdata;
  VideoPicture *vp;
  
  if(is->video_st) {
    if(is->pictq_size == 0) {
      schedule_refresh(is, 1);
    } else {
      vp = &is->pictq[is->pictq_rindex];
      /* Timing code goes here */

      schedule_refresh(is, 80);
      
      /* show the picture! */
      video_display(is);
      
      /* update queue for next picture! */
      if(++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE) {
	is->pictq_rindex = 0;
      } (is->pictq_mutex);
      is->pictq_size--; (is->pictq_cond); (is->pictq_mutex);
    }
  } else {
    schedule_refresh(is, 100);
  }
}
For now, this is a pretty simple function: it pulls from the queue when we have something, sets our timer for when the next video frame should be shown, calls video_display to actually show the video on the screen, then increments the counter on the queue, and decreases its size. You may notice that we don't actually do anything with vp in this function, and here's why: we will. Later. We're going to use it to access timing information when we start syncing the video to the audio. See where it says "timing code here"? In that section, we're going to figure out how soon we should show the next video frame, and then input that value into the schedule_refresh() function. For now we're just putting in a dummy value of 80. Technically, you could guess and check this value, and recompile it for every movie you watch, but 1) it would drift after a while and 2) it's quite silly. We'll come back to it later, though.


We're almost done; we just have one last thing to do: display the video! Here's that video_display function:


void video_display(VideoState *is) { rect; VideoPicture *vp; pict; float aspect_ratio; int w, h, x, y; int i; vp = &is->pictq[is->pictq_rindex]; if(vp->bmp) { if(is->video_st->codec->sample_aspect_ratio.num == 0) { aspect_ratio = 0; } else { aspect_ratio = (is->video_st->codec->sample_aspect_ratio) * is->video_st->codec->width / is->video_st->codec->height; } if(aspect_ratio <= 0.0) { aspect_ratio = (float)is->video_st->codec->width / (float)is->video_st->codec->height; } h = screen->h; w = ((int)rint(h * aspect_ratio)) & -3; if(w > screen->w) { w = screen->w; h = ((int)rint(w / aspect_ratio)) & -3; } x = (screen->w - w) / 2; y = (screen->h - h) / 2; rect.x = x; rect.y = y; rect.w = w; rect.h = h; (vp->bmp, &rect); } }
Since our screen can be of any size (we set ours to 640x480 and there are ways to set it so it is resizable by the user), we need to dynamically figure out how big we want our movie rectangle to be. So first we need to figure out our movie's aspect ratio, which is just the width divided by the height. Some codecs will have an odd sample aspect ratio, which is simply the width/height radio of a single pixel, or sample. Since the height and width values in our codec context are measured in pixels, the actual aspect ratio is equal to the aspect ratio times the sample aspect ratio. Some codecs will show an aspect ratio of 0, and this indicates that each pixel is simply of size 1x1. Then we scale the movie to fit as big in our screen as we can. The& -3 bit-twiddling in there simply rounds the value to the nearest multiple of 4. Then we center the movie, and call ().


So is that it? Are we done? Well, we still have to rewrite the audio code to use the new VideoStruct, but those are trivial changes, and you can look at those in the sample code. The last thing we have to do is to change our callback for ffmpeg's internal "quit" callback function:

VideoState *global_video_state;

int decode_interrupt_cb(void) {
  return (global_video_state && global_video_state->quit);
}
We set global_video_state to the big struct in main().


So that's it! Go ahead and compile it:

gcc -o tutorial04 tutorial04.c -lavutil -lavformat -lavcodec -lz -lm \
`sdl-config --cflags --libs`
and enjoy your unsynced movie! Next time we'll finally build a video player that actually works!


源代码如下(修改部分代码,加入一些注释)

点击(此处)折叠或打开

  1. // tutorial04.c
  2. // A pedagogical video player that will stream through every video frame as fast as it can,
  3. // and play audio (out of sync).
  4. //
  5. // This tutorial was written by Stephen Dranger (dranger@gmail.com).
  6. //
  7. // Code based on FFplay, Copyright (c) 2003 Fabrice Bellard,
  8. // and a tutorial by Martin Bohme (boehme@inb.uni-luebeckREMOVETHIS.de)
  9. // Tested on Gentoo, CVS version 5/01/07 compiled with GCC 4.1.1
  10. //
  11. // Use the Makefile to build all the samples.
  12. //
  13. // Run using
  14. // tutorial04 myvideofile.mpg
  15. //
  16. // to play the video stream on your screen.
  17. //
  18. //
  19. //
  20. //gcc -o tutorial04 tutorial04.c -lavformat -lavcodec -lavutil -lswscale -lz -lm -L/opt/libffmpeg/lib/ -I/opt/libffmpeg/include/ \
  21. -ldl -lpthread -lSDL2 -I/opt/libsdl/include/ -L/opt/libsdl/lib/ -g
  22. //./tutorial04 testvideo.mov


  23. #include <libavcodec/avcodec.h>
  24. #include <libavformat/avformat.h>
  25. #include <libavformat/avio.h>
  26. #include <libswscale/swscale.h>
  27. #include <libavutil/avstring.h>

  28. #include <SDL2/SDL.h>
  29. #include <SDL2/SDL_thread.h>
  30. //#include "SDL_sysvideo.h"

  31. #ifdef __MINGW32__
  32. #undef main /* Prevents SDL from overriding main() */
  33. #endif

  34. #include <stdio.h>
  35. #include <math.h>

  36. #define SDL_AUDIO_BUFFER_SIZE 1024
  37. #define MAX_AUDIO_FRAME_SIZE 192000

  38. #define MAX_AUDIOQ_SIZE (5 * 16 * 1024)
  39. #define MAX_VIDEOQ_SIZE (5 * 256 * 1024)

  40. #define FF_ALLOC_EVENT (SDL_USEREVENT)
  41. #define FF_REFRESH_EVENT (SDL_USEREVENT + 1)
  42. #define FF_QUIT_EVENT (SDL_USEREVENT + 2)

  43. #define VIDEO_PICTURE_QUEUE_SIZE 1

  44. SDL_Window    *screen;
  45. SDL_Renderer    *renderer;
  46. #define PRE_WIDTH 904
  47. #define PRE_HEIGHT 600

  48. typedef struct PacketQueue {
  49.     AVPacketList *first_pkt, *last_pkt;
  50.     int nb_packets;
  51.     int size;
  52.     SDL_mutex *mutex;
  53.     SDL_cond *cond;
  54. } PacketQueue;


  55. typedef struct VideoPicture {
  56.     SDL_Texture *bmp;
  57.     AVFrame *pFrameYUV;
  58.     char *bufpoint;
  59.     int width, height; /* source height & width */
  60.     int allocated;
  61. } VideoPicture;

  62. typedef struct VideoState {
  63.     AVFormatContext *pFormatCtx;
  64.     int videoStream, audioStream;
  65.     AVStream *audio_st;
  66.     PacketQueue audioq;
  67.     uint8_t audio_buf[(MAX_AUDIO_FRAME_SIZE * 3) / 2];
  68.     unsigned int audio_buf_size;
  69.     unsigned int audio_buf_index;
  70.     AVFrame audio_frame;
  71.     AVPacket audio_pkt;
  72.     uint8_t *audio_pkt_data;
  73.     int audio_pkt_size;

  74.     AVStream *video_st;
  75.     PacketQueue videoq;
  76.     VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
  77.     int pictq_size, pictq_rindex, pictq_windex;
  78.     SDL_mutex *pictq_mutex;
  79.     SDL_cond *pictq_cond;
  80.     SDL_Thread *parse_tid;
  81.     SDL_Thread *video_tid;

  82.     char filename[1024];
  83.     int quit;

  84.     AVIOContext *io_context;
  85.     struct SwsContext *sws_ctx;
  86. } VideoState;

  87. /* Since we only have one decoding thread, the Big Struct
  88.    can be global in case we need it. */
  89. VideoState *global_video_state;

  90. void packet_queue_init(PacketQueue *q)
  91. {
  92.     memset(q, 0, sizeof(PacketQueue));
  93.     q->mutex = SDL_CreateMutex();
  94.     q->cond = SDL_CreateCond();
  95. }

  96. int packet_queue_put(PacketQueue *q, AVPacket *pkt)
  97. {
  98.     AVPacketList *pkt1;
  99.     if(av_dup_packet(pkt) < 0) {
  100.         return -1;
  101.     }
  102.     pkt1 = av_malloc(sizeof(AVPacketList));
  103.     if (!pkt1)
  104.         return -1;
  105.     pkt1->pkt = *pkt;
  106.     pkt1->next = NULL;

  107.     SDL_LockMutex(q->mutex);

  108.     if (!q->last_pkt)
  109.         q->first_pkt = pkt1;
  110.     else
  111.         q->last_pkt->next = pkt1;
  112.     q->last_pkt = pkt1;
  113.     q->nb_packets++;
  114.     q->size += pkt1->pkt.size;
  115.     SDL_CondSignal(q->cond);

  116.     SDL_UnlockMutex(q->mutex);
  117.     return 0;
  118. }

  119. static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block)
  120. {
  121.     AVPacketList *pkt1;
  122.     int ret;

  123.     SDL_LockMutex(q->mutex);

  124.     for(;;) {

  125.         if(global_video_state->quit) {
  126.             ret = -1;
  127.             break;
  128.         }

  129.         pkt1 = q->first_pkt;
  130.         if (pkt1) {
  131.             q->first_pkt = pkt1->next;
  132.             if (!q->first_pkt)
  133.                 q->last_pkt = NULL;
  134.             q->nb_packets--;
  135.             q->size -= pkt1->pkt.size;
  136.             *pkt = pkt1->pkt;
  137.             av_free(pkt1);
  138.             ret = 1;
  139.             break;
  140.         } else if (!block) {
  141.             ret = 0;
  142.             break;
  143.         } else {
  144.             SDL_CondWait(q->cond, q->mutex);
  145.         }
  146.     }
  147.     
  148.     SDL_UnlockMutex(q->mutex);
  149.     return ret;
  150. }

  151. int audio_decode_frame(VideoState *is)
  152. {
  153.     int len1, data_size = 0;
  154.     AVPacket *pkt = &is->audio_pkt;

  155.     for(;;) {
  156.         while(is->audio_pkt_size > 0) {
  157.             int got_frame = 0;
  158.             len1 = avcodec_decode_audio4(is->audio_st->codec, &is->audio_frame, &got_frame, pkt);
  159.             if(len1 < 0) {
  160.                 /* if error, skip frame */
  161.                 is->audio_pkt_size = 0;
  162.                 break;
  163.             }
  164.             if (got_frame){
  165.                 data_size =
  166.                 av_samples_get_buffer_size(NULL, is->audio_st->codec->channels,is->audio_frame.nb_samples,is->audio_st->codec->sample_fmt,1);
  167.                 memcpy(is->audio_buf, is->audio_frame.data[0], data_size);
  168.             }
  169.             is->audio_pkt_data += len1;
  170.             is->audio_pkt_size -= len1;
  171.             if(data_size <= 0) {
  172.                 /* No data yet, get more frames */
  173.                 continue;
  174.             }
  175.     
  176.             /* We have data, return it and come back for more later */
  177.             return data_size;
  178.         }
  179.         if(pkt->data)
  180.             av_free_packet(pkt);

  181.         if(is->quit) {
  182.             return -1;
  183.         }
  184.     
  185.         /* next packet */
  186.         if(packet_queue_get(&is->audioq, pkt, 1) < 0) {
  187.             return -1;
  188.         }
  189.     
  190.         is->audio_pkt_data = pkt->data;
  191.         is->audio_pkt_size = pkt->size;
  192.     }
  193. }

  194. void audio_callback(void *userdata, Uint8 *stream, int len)
  195. {
  196.     VideoState *is = (VideoState *)userdata;
  197.     int len1, audio_size;

  198.     while(len > 0) {
  199.         if(is->audio_buf_index >= is->audio_buf_size) {
  200.             /* We have already sent all our data; get more */
  201.             audio_size = audio_decode_frame(is);
  202.             if(audio_size < 0) {
  203.                 /* If error, output silence */
  204.                 is->audio_buf_size = 1024;
  205.                 memset(is->audio_buf, 0, is->audio_buf_size);
  206.             } else {
  207.                 is->audio_buf_size = audio_size;
  208.             }
  209.             is->audio_buf_index = 0;
  210.         }
  211.         len1 = is->audio_buf_size - is->audio_buf_index;
  212.         if(len1 > len)
  213.             len1 = len;
  214.         memcpy(stream, (uint8_t *)is->audio_buf + is->audio_buf_index, len1);
  215.         len -= len1;
  216.         stream += len1;
  217.         is->audio_buf_index += len1;
  218.     }
  219. }

  220. static Uint32 sdl_refresh_timer_cb(Uint32 interval, void *opaque)
  221. {
  222.     SDL_Event event;
  223.     event.type = FF_REFRESH_EVENT;
  224.     event.user.data1 = opaque;
  225.     SDL_PushEvent(&event);
  226.     return 0; /* 0 means stop timer */
  227. }

  228. /* schedule a video refresh in 'delay' ms */
  229. static void schedule_refresh(VideoState *is, int delay)
  230. {
  231.     SDL_AddTimer(delay, sdl_refresh_timer_cb, is);
  232. }

  233. void video_display(VideoState *is)
  234. {
  235.     SDL_Rect rect = {0};
  236.     VideoPicture *vp;
  237.     //AVPicture pict;
  238.     float aspect_ratio;
  239.     int w, h, x, y;
  240.     //int i;
  241.     vp = &is->pictq[is->pictq_rindex];
  242.     if(vp->bmp) {
  243.         if(is->video_st->codec->sample_aspect_ratio.num == 0) {
  244.             aspect_ratio = 0;
  245.         } else {
  246.             aspect_ratio = av_q2d(is->video_st->codec->sample_aspect_ratio) * is->video_st->codec->width / is->video_st->codec->height;
  247.         }
  248.     
  249.         if(aspect_ratio <= 0.0) {
  250.             aspect_ratio = (float)is->video_st->codec->width / (float)is->video_st->codec->height;
  251.         }
  252.     
  253.         //h = screen->h;
  254.         //w = ((int)rint(h * aspect_ratio)) & -3;
  255.         //if(w > screen->w) {
  256.         //    w = screen->w;
  257.         //    h = ((int)rint(w / aspect_ratio)) & -3;
  258.         //}
  259.         //x = (screen->w - w) / 2;
  260.         //y = (screen->h - h) / 2;

  261.         rect.x = 0;
  262.         rect.y = 0;
  263.         //rect.w = w;
  264.         //rect.h = h;
  265.         rect.w = vp->width;
  266.         rect.h = vp->height;
  267.         
  268.         SDL_UpdateTexture(vp->bmp, &rect, vp->pFrameYUV->data[0], vp->pFrameYUV->linesize[0] );
  269.         SDL_RenderClear( renderer );
  270.         SDL_RenderCopy( renderer, vp->bmp, &rect, &rect );
  271.         SDL_RenderPresent( renderer );

  272.         free(vp->bufpoint);
  273.         av_free(vp->pFrameYUV);
  274.         // SDL_DisplayYUVOverlay(vp->bmp, &rect);
  275.     }
  276. }

  277. void video_refresh_timer(void *userdata)
  278. {
  279.     VideoState *is = (VideoState *)userdata;
  280.     // vp is used in later tutorials for synchronization
  281.     //VideoPicture *vp;

  282.     if(is->video_st) {
  283.         if(is->pictq_size == 0) {
  284.             schedule_refresh(is, 1);
  285.         } else {
  286.             //vp = &is->pictq[is->pictq_rindex];
  287.             /* Now, normally here goes a ton of code
  288.              about timing, etc. we're just going to
  289.              guess at a delay for now. You can
  290.              increase and decrease this value and hard code
  291.              the timing - but I don't suggest that ;)
  292.              We'll learn how to do it for real later.
  293.              delay 40ms => fps = 25
  294.             */
  295.             schedule_refresh(is, 40);

  296.             /* show the */
  297.             video_display(is);

  298.             /* update queue for next */
  299.             if(++is->pictq_rindex == VIDEO_PICTURE_QUEUE_SIZE) {
  300.                 is->pictq_rindex = 0;
  301.             }
  302.             SDL_LockMutex(is->pictq_mutex);
  303.             is->pictq_size--;
  304.             SDL_CondSignal(is->pictq_cond);
  305.             SDL_UnlockMutex(is->pictq_mutex);
  306.         }
  307.     } else {
  308.         schedule_refresh(is, 100);
  309.     }
  310. }
  311.       
  312. void alloc_picture(void *userdata)
  313. {
  314.     VideoState *is = (VideoState *)userdata;
  315.     VideoPicture *vp;

  316.     vp = &is->pictq[is->pictq_windex];
  317.     if(vp->bmp) {
  318.         // we already have one make another, bigger/smaller
  319.         SDL_DestroyTexture(vp->bmp);
  320.     }
  321.     
  322.     // Allocate a place to put our YUV image on that screen
  323.     vp->bmp = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_YV12, SDL_TEXTUREACCESS_STREAMING, is->video_st->codec->width, is->video_st->codec->height);
  324.     vp->width = is->video_st->codec->width;
  325.     vp->height = is->video_st->codec->height;

  326.     SDL_LockMutex(is->pictq_mutex);
  327.     vp->allocated = 1;
  328.     SDL_CondSignal(is->pictq_cond);
  329.     SDL_UnlockMutex(is->pictq_mutex);
  330. }

  331. int queue_picture(VideoState *is, AVFrame *pFrame)
  332. {
  333.     VideoPicture *vp;
  334.     SDL_Rect rect;

  335.     /* wait until we have space for a new pic */
  336.     SDL_LockMutex(is->pictq_mutex);
  337.     while(is->pictq_size >= VIDEO_PICTURE_QUEUE_SIZE && !is->quit) {
  338.         SDL_CondWait(is->pictq_cond, is->pictq_mutex);
  339.     }
  340.     SDL_UnlockMutex(is->pictq_mutex);

  341.     if(is->quit)
  342.         return -1;

  343.     // windex is set to 0 initially
  344.     vp = &is->pictq[is->pictq_windex];

  345.     /* allocate or resize the */
  346.     if(!vp->bmp || vp->width != is->video_st->codec->width || vp->height != is->video_st->codec->height) {
  347.         SDL_Event event;

  348.         vp->allocated = 0;
  349.         /* we have to do it in the main thread */
  350.         event.type = FF_ALLOC_EVENT;
  351.         event.user.data1 = is;
  352.         SDL_PushEvent(&event);

  353.         /* wait until we have a picture allocated */
  354.         SDL_LockMutex(is->pictq_mutex);
  355.         while(!vp->allocated && !is->quit) {
  356.             SDL_CondWait(is->pictq_cond, is->pictq_mutex);
  357.         }
  358.         SDL_UnlockMutex(is->pictq_mutex);
  359.         if(is->quit) {
  360.             return -1;
  361.         }
  362.     }
  363.     /* We have a place to put our picture on the queue */

  364.     if(vp->bmp) {
  365.         vp->pFrameYUV = avcodec_alloc_frame();
  366.         // SDL_LockYUVOverlay(vp->bmp);
  367.         int numBytes = avpicture_get_size(PIX_FMT_YUV420P, is->video_st->codec->width, is->video_st->codec->height);
  368.         uint8_t* buffer = (uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
  369.         vp->bufpoint = buffer;
  370.         avpicture_fill((AVPicture *)vp->pFrameYUV, buffer, PIX_FMT_YUV420P, is->video_st->codec->width, is->video_st->codec->height);
  371.         /* point pict at the queue */
  372.         
  373.         // Convert the image into YUV format that SDL uses
  374.         sws_scale(is->sws_ctx,(uint8_t const * const *)pFrame->data,pFrame->linesize,0, is->video_st->codec->height, vp->pFrameYUV->data, vp->pFrameYUV->linesize);

  375.         // SDL_UnlockYUVOverlay(vp->bmp);
  376.         /* now we inform our display thread that we have a pic ready */
  377.         if(++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE) {
  378.             is->pictq_windex = 0;
  379.         }

  380.         SDL_LockMutex(is->pictq_mutex);
  381.         is->pictq_size++;
  382.         SDL_UnlockMutex(is->pictq_mutex);
  383.     }
  384.     
  385.     return 0;
  386. }

  387. int video_thread(void *arg)
  388. {
  389.     VideoState *is = (VideoState *)arg;
  390.     AVPacket pkt1, *packet = &pkt1;
  391.     int frameFinished;
  392.     AVFrame *pFrame;

  393.     pFrame = av_frame_alloc();

  394.     for(;;) {
  395.         if(packet_queue_get(&is->videoq, packet, 1) < 0) {
  396.             // means we quit getting packets
  397.             break;
  398.         }
  399.         // Decode video frame
  400.         avcodec_decode_video2(is->video_st->codec, pFrame, &frameFinished, packet);
  401.         av_free_packet(packet);

  402.         // Did we get a video frame?
  403.         if(frameFinished) {
  404.             if(queue_picture(is, pFrame) < 0) {
  405.                 break;
  406.             }
  407.         }
  408.     }
  409.     
  410.     av_free(pFrame);
  411.     return 0;
  412. }

  413. int stream_component_open(VideoState *is, int stream_index)
  414. {
  415.     AVFormatContext *pFormatCtx = is->pFormatCtx;
  416.     AVCodecContext *codecCtx = NULL;
  417.     AVCodec *codec = NULL;
  418.     AVDictionary *optionsDict = NULL;
  419.     SDL_AudioSpec wanted_spec, spec;

  420.     if(stream_index < 0 || stream_index >= pFormatCtx->nb_streams) {
  421.         return -1;
  422.     }

  423.     // Get a pointer to the codec context for the video stream
  424.     codecCtx = pFormatCtx->streams[stream_index]->codec;

  425.     if(codecCtx->codec_type == AVMEDIA_TYPE_AUDIO) {
  426.         // Set audio settings from codec info
  427.         wanted_spec.freq = codecCtx->sample_rate;
  428.         wanted_spec.format = AUDIO_S16SYS;
  429.         wanted_spec.channels = codecCtx->channels;
  430.         wanted_spec.silence = 0;
  431.         wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
  432.         wanted_spec.callback = audio_callback;
  433.         wanted_spec.userdata = is;

  434.         if(SDL_OpenAudio(&wanted_spec, &spec) < 0) {
  435.             fprintf(stderr, "SDL_OpenAudio: %s\n", SDL_GetError());
  436.             return -1;
  437.         }
  438.     }
  439.     codec = avcodec_find_decoder(codecCtx->codec_id);
  440.     if(!codec || (avcodec_open2(codecCtx, codec, &optionsDict) < 0)) {
  441.         fprintf(stderr, "Unsupported codec!\n");
  442.         return -1;
  443.     }

  444.     switch(codecCtx->codec_type) {
  445.         case AVMEDIA_TYPE_AUDIO:
  446.             is->audioStream = stream_index;
  447.             is->audio_st = pFormatCtx->streams[stream_index];
  448.             is->audio_buf_size = 0;
  449.             is->audio_buf_index = 0;
  450.             memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
  451.             packet_queue_init(&is->audioq);
  452.             SDL_PauseAudio(0);
  453.             break;
  454.         case AVMEDIA_TYPE_VIDEO:
  455.             is->videoStream = stream_index;
  456.             is->video_st = pFormatCtx->streams[stream_index];

  457.             packet_queue_init(&is->videoq);
  458.             is->video_tid = SDL_CreateThread(video_thread, "video_tid", is);
  459.             is->sws_ctx = sws_getContext(is->video_st->codec->width,is->video_st->codec->height,is->video_st->codec->pix_fmt,is->video_st->codec->width,
  460.                             is->video_st->codec->height,PIX_FMT_YUV420P, SWS_BILINEAR, NULL, NULL, NULL);
  461.             break;
  462.         default:
  463.             break;
  464.     }
  465.     
  466.     return 0;
  467. }

  468. int decode_interrupt_cb(void *opaque)
  469. {
  470.     return (global_video_state && global_video_state->quit);
  471. }

  472. int decode_thread(void *arg)
  473. {
  474.     VideoState *is = (VideoState *)arg;
  475.     AVFormatContext *pFormatCtx = NULL;
  476.     AVPacket pkt1, *packet = &pkt1;

  477.     int video_index = -1;
  478.     int audio_index = -1;
  479.     int i;

  480.     AVDictionary *io_dict = NULL;
  481.     AVIOInterruptCB callback;

  482.     is->videoStream=-1;
  483.     is->audioStream=-1;

  484.     global_video_state = is;
  485.     // will interrupt blocking functions if we
  486.     callback.callback = decode_interrupt_cb;
  487.     callback.opaque = is;
  488.     if (avio_open2(&is->io_context, is->filename, 0, &callback, &io_dict)){
  489.         fprintf(stderr, "Unable to open I/O for %s\n", is->filename);
  490.         return -1;
  491.     }

  492.     // Open video file
  493.     if(avformat_open_input(&pFormatCtx, is->filename, NULL, NULL)!=0)
  494.         return -1; // Couldn't open file

  495.     is->pFormatCtx = pFormatCtx;

  496.     // Retrieve stream information
  497.     if(avformat_find_stream_info(pFormatCtx, NULL)<0)
  498.         return -1; // Couldn't find stream information

  499.     // Dump information about file onto standard error
  500.     av_dump_format(pFormatCtx, 0, is->filename, 0);

  501.     // Find the first video stream
  502.     for(i=0; i<pFormatCtx->nb_streams; i++) {
  503.         if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO && video_index < 0) {
  504.             video_index=i;
  505.         }
  506.         if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO && audio_index < 0) {
  507.             audio_index=i;
  508.         }
  509.     }
  510.     if(audio_index >= 0) {
  511.         stream_component_open(is, audio_index);
  512.     }
  513.     if(video_index >= 0) {
  514.         stream_component_open(is, video_index);
  515.     }

  516.     if(is->videoStream < 0 || is->audioStream < 0) {
  517.         fprintf(stderr, "%s: could not open codecs\n", is->filename);
  518.         goto fail;
  519.     }

  520.     // main decode loop
  521.     for(;;) {
  522.         if(is->quit) {
  523.             break;
  524.         }
  525.         // seek stuff goes here
  526.         if(is->audioq.size > MAX_AUDIOQ_SIZE || is->videoq.size > MAX_VIDEOQ_SIZE) {
  527.             SDL_Delay(10);
  528.             continue;
  529.         }
  530.         if(av_read_frame(is->pFormatCtx, packet) < 0) {
  531.             is->quit = 1;
  532.             /*
  533.             if(is->pFormatCtx->pb->error == 0) {
  534.                 SDL_Delay(100); // no error; wait for user input
  535.                 continue;
  536.             } else {
  537.                 break;
  538.             }
  539.             */
  540.         }
  541.     
  542.         // Is this a packet from the video stream?
  543.         if(packet->stream_index == is->videoStream) {
  544.             packet_queue_put(&is->videoq, packet);
  545.         } else if(packet->stream_index == is->audioStream) {
  546.             packet_queue_put(&is->audioq, packet);
  547.         } else {
  548.             av_free_packet(packet);
  549.         }
  550.     }

  551.     /* all done - wait for it */
  552.     while(!is->quit) {
  553.         SDL_Delay(100);
  554.     }

  555. fail:
  556.     if(1){
  557.         SDL_Event event;
  558.         event.type = FF_QUIT_EVENT;
  559.         event.user.data1 = is;
  560.         SDL_PushEvent(&event);
  561.     }
  562.     return 0;
  563. }

  564. int main(int argc, char *argv[])
  565. {
  566.     if(argc < 2) {
  567.         fprintf(stderr, "Usage: test \n");
  568.         return -1;
  569.     }

  570.     SDL_Event event;
  571.     VideoState *is;
  572.       is = av_mallocz(sizeof(VideoState));

  573.     if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
  574.         fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
  575.         return -1;
  576.     }

  577.     // Register all formats and codecs
  578.     av_register_all();
  579.   
  580.     // Make a screen to put our video
  581.       screen = SDL_CreateWindow("Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, PRE_WIDTH, PRE_HEIGHT, SDL_WINDOW_FULLSCREEN_DESKTOP);
  582.     renderer = SDL_CreateRenderer(screen, -1, 0);
  583.     if(!screen) {
  584.         fprintf(stderr, "SDL: could not set video mode - exiting (%s)\n", SDL_GetError());
  585.         return -1;
  586.     }
  587.     
  588.     av_strlcpy(is->filename, argv[1], sizeof(is->filename));
  589.     is->pictq_mutex = SDL_CreateMutex();
  590.     is->pictq_cond = SDL_CreateCond();

  591.     schedule_refresh(is, 40);    //this line aim to send a FF_REFRESH_EVENT signal to main function 40ms later
  592.     is->parse_tid = SDL_CreateThread(decode_thread, "parse_tid", is);
  593.     if(!is->parse_tid) {
  594.         av_free(is);
  595.         return -1;
  596.     }

  597.     for(;;) {
  598.         SDL_WaitEvent(&event);
  599.         switch(event.type) {
  600.             case FF_QUIT_EVENT:
  601.             case SDL_QUIT:
  602.                 is->quit = 1;
  603.                 /*
  604.                 * If the video has finished playing, then both the picture and
  605.                 * audio queues are waiting for more data. Make them stop
  606.                 * waiting and terminate normally.
  607.                 */
  608.                 SDL_CondSignal(is->audioq.cond);
  609.                 SDL_CondSignal(is->videoq.cond);
  610.                 SDL_Quit();
  611.                 return 0;
  612.                 break;
  613.             case FF_ALLOC_EVENT:
  614.                 alloc_picture(event.user.data1);
  615.                 break;
  616.             case FF_REFRESH_EVENT:
  617.                 video_refresh_timer(event.user.data1);
  618.                 break;
  619.             default:
  620.                 break;
  621.         }
  622.     }
  623.     
  624.     av_free(is);
  625.     return 0;
  626. }

阅读(1077) | 评论(0) | 转发(0) |
0

上一篇:ffmpeg - tutorial03

下一篇:ffmpeg - tutorial06

给主人留下些什么吧!~~